Time limit
2s
Memory limit
128 MB
There is an N*M plot of land. Each cell is a rectangular pillar whose height is an integer from 1 to 9, and initially there is no water on top of any pillar.
The outside of the land has height 0 and can absorb an unlimited amount of water. Water flows out if there is a path to the outside through lower heights. Water that is blocked by the surrounding terrain can remain on top of the cells.
Given the terrain, compute the total amount of water that can remain without flowing out.
The first line contains two positive integers N and M. Each of N and M is at most 50.
The next N lines describe the terrain heights. Each line is a string of length M, and every character is a one-digit positive integer from 1 to 9.
Print the total amount of water that can remain.