The Jeweller's Game

Time limit: 2s Memory limit: 256MB Input: Output:

Task

John is playing the new hit mobile game: "The Jeweller's Game".

In this game, there is an N×NN \times N board full of different kinds of gems. Let's denote by (r,c)(r, c) the cell located at the rr-th row and cc-th column of the board. Each cell of the board contains a gem. The type of the gem in cell (r,c)(r,c) is represented by a positive integer Gr,cG_{r,c}.

We group the cells according to the following rule. Cells aa and bb are in the same group if and only if there exists a sequence of cells p0,,pkp_0, \ldots, p_k such that:

  • p0=ap_0 = a and pk=bp_k = b, and
  • cells pi1p_{i-1} and pip_i are edge-adjacent and are of the same type for each i=1,,ki = 1, \ldots, k.

Note that every cell belongs to exactly one group.

The player can improve their score by swapping edge-adjacent cells of the board. Depending on whether the two swapped cells are in the same row or in the same column, we call a swap either horizontal or vertical, respectively. If the two swapped gems are of the same type, then the score of the swap is 00.
Otherwise, consider the board after performing the swap: the score is the product of the values of the two swapped cells. The value of a cell is the number of cells in its group (including itself).

There were many similar games in the past.\text{There were many similar games in the past.}

Find the score of all horizontal and vertical swaps on the board!

Input data

The input file consists of:

  • a line containing integer NN.
  • NN lines, the jj-th of which consisting of the NN integers Gj,1,,Gj,NG_{j,1}, \, \ldots, \, G_{j,N}.

Output data

The first NN lines of the output should contain N1N-1 integers each.
On the ii-th line the jj-th number (1iN1 \le i \le N, 1j<N1 \le j < N) should be the score of the swap of cells (i,j)(i, j) and (i,j+1)(i, j+1).

The next N1N-1 lines of the output should contain NN integers each.
The jj-th number on ii-th line (1i<N1 \le i < N, 1jN1 \le j \le N) should be the score of the swap of cells (i,j)(i,j) and (i+1,j)(i+1, j).

Constraints and clarifications

  • 2N1 0002 \le N \le 1 \ 000.
  • 1Gr,c1061 \le G_{r,c} \le 10^6 for each r=1Nr=1\ldots N and c=1Nc=1 \ldots N.
# Points Constraints
1 0 Examples.
2 15 N=2N = 2
3 45 N75N \le 75
4 40 No additional limitations.

Example 1

stdin

3
1 2 1
1 3 2
2 2 2

stdout

2 15 
1 5 
0 0 
0 5 2 
1 4 0

Explanation

In the first sample case, consider the result of the (horizontal) swap of cells (1,2)(1,2) and (1,3)(1,3):

1 1 21 3 22 2 2\textcolor{red}{1 \ 1} \ \textcolor{blue}{2} \\ \textcolor{red}{1} \ 3 \ \textcolor{blue}{2} \\ \textcolor{blue}{2 \ 2 \ 2}

The groups of the two swapped cells are marked with red and blue. So the score of the swap is 35=153 \cdot 5 = 15.

Example 2

stdin

4
2 1 9 1
1 2 1 1
2 1 2 7
2 9 2 1

stdout

4 4 4 
24 12 0 
8 16 1 
3 3 1 
8 12 4 0 
6 30 4 2 
0 1 0 4 

Log in or sign up to be able to send submissions!