Time limit
2s
Memory limit
128 MB
There is a container made of an N by M grid of cells. Each cell has a ground height, and different cells may have different heights. The boundary of the container is also made of cells, and some boundary cells may be lower than cells inside the container.
After pouring enough water into the container, find the maximum total amount of water that can remain without flowing out. Water can flow to lower cells through side-adjacent cells.
The first line contains the number of columns M and the number of rows N. (1 ≤ N, M ≤ 300)
Each of the next N lines contains M natural numbers for one row. Each number is the height of that cell and does not exceed 1,000,000,000.
Print the maximum total amount of water that can remain in the container on the first line.
The answer fits in the int range and may be 0.