atcoder#ARC135D. [ARC135D] Add to Square

[ARC135D] Add to Square

Score : 700700 points

Problem Statement

We have an H×WH \times W grid, where each square has one integer written on it. For 1iH1\leq i\leq H and 1jW1\leq j\leq W, let Ai,jA_{i,j} denote the integer written on the square at the ii-th row and jj-th column.

You can do the operation below any number of times (possibly zero).

  • Choose integers ii and jj such that 1iH11\leq i\leq H - 1 and 1jW11\leq j\leq W - 1.
  • Choose another integer xx.
  • Add xx to each of Ai,jA_{i,j}, Ai,j+1A_{i,j+1}, Ai+1,jA_{i+1,j}, and Ai+1,j+1A_{i+1,j+1}.

Print the minimum possible value of i=1Hj=1WAi,j\sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| after your operations, and the integers on the grid when that value is achieved.

Constraints

  • 2H,W5002\leq H, W \leq 500
  • Ai,j109|A_{i,j}|\leq 10^9

Input

Input is given from Standard Input from the following format:

HH WW

A1,1A_{1,1} \ldots A1,WA_{1,W}

\vdots

AH,1A_{H,1} \ldots AH,WA_{H,W}

Output

Print H+1H + 1 lines. The 11-st line should contain the value of i=1Hj=1WAi,j\sum_{i=1}^H \sum_{j=1}^W |A_{i,j}|. The 22-nd through (H+1)(H+1)-th lines should contain the integers on the grid, in the following format:

A1,1A_{1,1} \ldots A1,WA_{1,W}

\vdots

AH,1A_{H,1} \ldots AH,WA_{H,W}

If there are multiple solutions, you may print any of them.

2 3
1 2 3
4 5 6
9
0 -3 -1
3 0 2

Here is a sequence of operations that produces the grid in the Sample Output.

  • Do the operation with (i,j,x)=(1,1,1)(i, j, x) = (1, 1, -1).
  • Do the operation with (i,j,x)=(1,2,4)(i, j, x) = (1, 2, -4).

Here, we have $\sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| = 0 + 3 + 1 + 3 + 0 + 2 = 9$.

2 2
1000000000 -1000000000
-1000000000 1000000000
4000000000
2000000000 0
0 2000000000

It is fine if Ai,j>109|A_{i,j}| > 10^9 after your operations.

3 4
0 2 0 -2
-3 -1 2 0
-3 -3 2 2
0
0 0 0 0
0 0 0 0
0 0 0 0