Task
John is playing the new hit mobile game: "The Jeweller's Game".
In this game, there is an board full of different kinds of gems. Let's denote by the cell located at the -th row and -th column of the board. Each cell of the board contains a gem. The type of the gem in cell is represented by a positive integer .
We group the cells according to the following rule. Cells and are in the same group if and only if there exists a sequence of cells such that:
- and , and
- cells and are edge-adjacent and are of the same type for each .
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 .
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).
Find the score of all horizontal and vertical swaps on the board!
Input data
The input file consists of:
- a line containing integer .
- lines, the -th of which consisting of the integers .
Output data
The first lines of the output should contain integers each.
On the -th line the -th number (, ) should be the score of the swap of cells and .
The next lines of the output should contain integers each.
The -th number on -th line (, ) should be the score of the swap of cells and .
Constraints and clarifications
- .
- for each and .
# | Points | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 15 | |
3 | 45 | |
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 and :
The groups of the two swapped cells are marked with red and blue. So the score of the swap is .
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