Caner: The Way of Water

View as PDF

Submit solution


Points: 1
Time limit: 1.0s
Memory limit: 256M

Problem types

Caner is a mischievous but also very smart child. He loves to create games for himself. In Caner's latest game, he creates lego towers which are of various heights using lego cubes, and then placing them on top of a lego base with a certain length and width. Caner sees the rain falling outside, so he takes his lego structure outside. After that he drops it on a flat surface and starts watching it. After a while, Caner sees that all the spaces in the structure are filled with water. Caner is curious about the amount of water collected in the end of the game, can you help him calculate this?

Each lego cube is \(1\) unit in size.

The base lego size used by Caner is \(M \times N\).

The heights of the legos in a \(1\) unit square area is \(h[x][y]\).

Input

The first line contains two integers, \(M\) and \(N\).

The next \(M\) lines contain \(N\) integers each, describing the lego structure. The integer in the \(i\)-th row and the \(j\)-th column represents the height of the corresponding lego block.

  • \(1 \le M, N \le 3*10^2\)
  • \(0 \le h[i][j] \le 3*10^2\)

Output

Print the amount of water collected inside the structure.

Example

Input 1:

5 6
5 5 6 4 3 4
3 2 5 6 1 3
5 4 3 1 2 4
6 3 4 5 2 4
5 6 2 3 4 1

Output 1:

8

Input 2:

4 5
2 3 1 2 4
2 3 5 3 1
4 2 4 2 2
2 4 3 1 3

Output 2:

1

Image of Input 1