#P8217. Counting Cubes

    ID: 21396 Type: Default 1000ms 256MiB

Counting Cubes

Counting Cubes

In this problem, you are given a rectangular grid of size n×m. Each cell (i, j) contains a positive integer A[i,j] (with 1 ≤ A[i,j]) representing the number of cubes stacked in that cell. These stacks of cubes are originally drawn as an isometric ASCII art following a fixed pattern – each individual cube is drawn in exactly the same way (without any rotation) and when two cubes are adjacent (either horizontally, vertically, or front‐to‐back) the front cube might partially cover the one behind.

It is guaranteed that the stacks obey the following monotonicity conditions:

  • 1 ≤ A[i,j] ≤ A[i-1,j] for all valid i > 1,
  • 1 ≤ A[i,j] ≤ A[i,j-1] for all valid j > 1.

Although the original drawing is an ASCII art of cubes, it can be shown that there is a one-to-one correspondence between the drawing and the matrix A. In this problem, rather than processing the drawing, you are directly given the matrix A. Your task is to compute the total number of cubes.

That is, you need to output the sum of all A[i,j] for i=1..n and j=1..m.

inputFormat

The first line contains two integers n and m (1 ≤ n, m ≤ 1000), representing the number of rows and columns of the grid.

Then follow n lines; each line contains m positive integers separated by spaces. The j-th integer in the i-th line represents A[i,j]. It is guaranteed that these numbers satisfy the monotonicity conditions mentioned above.

outputFormat

Output a single integer – the total number of cubes, that is the sum of all A[i,j] over the grid.

sample

1 1
1
1