#P15558. [CCPC 2025 哈尔滨站] 扫雪

[CCPC 2025 哈尔滨站] 扫雪

Problem Description

After a heavy snow, little w needs to clean up his yard so that the uneven snow piles look at least a bit neater. The yard can be seen as an n×mn \times m grid. The cell in row ii and column jj is covered by snow with relative height hi,jh_{i,j}. Little w has the following operations to deal with the snow piles:

  • Choose 1i<n,1jm1 \le i < n, 1 \le j \le m, and push the snow pile at row ii, column jj to the next row. This operation decreases hi,jh_{i,j} by 11 and increases hi+1,jh_{i+1,j} by 11. This operation has no cost.
  • Choose 1in,1j<m1 \le i \le n, 1 \le j < m, and push the snow pile at row ii, column jj to the next column. This operation decreases hi,jh_{i,j} by 11 and increases hi,j+1h_{i,j+1} by 11. This operation has no cost.
  • Choose 1in,1jm1 \le i \le n, 1 \le j \le m, and make snow on the pile at row ii, column jj. This operation increases hi,jh_{i,j} by 11. This operation costs 11.
  • Choose 1in,1jm1 \le i \le n, 1 \le j \le m, and remove snow from the pile at row ii, column jj. This operation decreases hi,jh_{i,j} by 11. This operation costs 11.

Little w wants to perform some operations to make all hi,jh_{i,j} equal to 00, with the minimum total cost. Can you help him compute the minimum cost?

Input Format

The first line contains an integer TT (1T1061 \le T \le 10^6), indicating the number of test cases.

Then each test case is given as follows:

The first line contains two integers n,mn, m (1n,m1031 \le n, m \le 10^3), representing the number of rows and columns of the yard.

The next nn lines describe the grid. Line ii contains mm integers hi,1,hi,2,,hi,mh_{i,1}, h_{i,2}, \ldots, h_{i,m} (109hi,j109-10^9 \le h_{i,j} \le 10^9), where the jj-th integer is the relative snow height in the cell at row ii, column jj.

For all testdata, it is guaranteed that the sum of nmn \cdot m over all test cases does not exceed 10610^6.

Output Format

For each test case, output one integer per line, representing the minimum cost for little w to reach the goal.

3
1 1
5
1 2
1 -1
2 2
-1 0
1 1
5
0
3

Hint

Translated by ChatGPT 5