P15903 [TOPC 2025] Move Stone

Description

You are given an $n \times n$ grid. Each cell initially contains some number of stones, such that the total number of stones is exactly $n^2$. In one move, you may take a single stone and move it to any other cell in the same row or the same column. Your goal is to minimize the number of moves needed to make each cell contain exactly one stone.

Input Format

The first line contains an integer $n$, representing the size of the grid. Followed by $n$ lines, the $i$-th of which contains $n$ integers, the $j$-th integer $a_{i,j}$ represents the number of stones in cell $(i, j)$.

Output Format

Output a single integer, the minimum number of moves required to make each cell contain exactly one stone.

Explanation/Hint

- $1 \le n \le 500$ - $0 \le a_{i,j} \le n^2$ - The initial number of stones is exactly equal to the number of cells on the board.