Rainwater on a Terrain
A 2-D height map is given. After heavy rain, water pools in basins but drains off the grid's border. Print the total volume of trapped water.
Input: the first line has N and M; the next N lines form the height grid.
Output: the total trapped water volume.
Constraints:
- 1 ≤ N, M ≤ 500
- 0 ≤ height ≤ 104
Sample Tests
Input 1
3 3 5 5 5 5 1 5 5 5 5
Output 1
4
Input 2
1 5 1 2 3 4 5
Output 2
0
Discussion (opens after you solve — no spoilers before)
No comments yet.