QOJ.ac

QOJ

时间限制: 2.0 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18606. Generalized Chess

统计

Mikhail decided to learn how to play generalized chess. For this, he prepared a chessboard of size $n \times n$ cells. Each cell at the intersection of row $i$ and column $j$ is colored with color $a_{ij}$.

Mikhail is a beginner player and might have colored the board incorrectly. Therefore, some cells on the board may need to be repainted. The board is considered correctly colored if two conditions are met:

  • The cells on the board are colored with no more than two distinct colors.
  • There are no adjacent cells (sharing a side) colored with the same color.

Mikhail realized that playing on a large board would be too difficult for him. Therefore, he might cut out a smaller board from his board, leaving a rectangular area consisting of the first $r$ rows and the first $c$ columns, and color only this area correctly. For each pair of numbers $(r, c)$ where $1 \le r \le n$ and $1 \le c \le n$, calculate the value $b_{rc}$ - the minimum number of cells that need to be repainted so that the rectangular area of the first $r$ rows and the first $c$ columns is colored correctly.

Input

The first line contains an integer $n$ ($1 \le n \le 400$) - the size of the board. The next $n$ lines describe the board. The $i$-th of these lines contains $n$ integers $a_{i1}, \dots, a_{in}$ ($1 \le a_{ij} \le 10^9$) - the colors of the cells in the $i$-th row.

Output

Output $n$ lines, where the $i$-th line should contain $n$ integers $b_{i1}, \dots, b_{in}$.

Subtasks

Subtask Score Additional Constraints
1 11 $n \le 50$
2 22 $n \le 200$
3 8 $a_{ij} \le 2$
4 17 $a_{ij} \le 10$
5 15 $a_{ij} \le 100$
6 7 $a_{ij} \le 10^4$
7 20 -

Examples

Input 1

2
7 7
7 7

Output 1

0 1
1 2

Input 2

3
1 1 2
2 4 4
3 1 2

Output 2

0 1 1
0 2 4
1 3 5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.