Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers.
For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8 + 3 + 6 + 7 = 24, sum of its row numbers equals 9 + 5 + 3 + 2 = 19, and 24 > 19.
Input
The first line contains an integer n (1 ≤ n ≤ 30). Each of the following n lines contain n space-separated integers. The j-th number on the i-th line represents the number on the square that belongs to the j-th column and the i-th row on the board. All number on the board are integers from 1 to 100.
Output
Print the single number — the number of the winning squares.
Examples
Input
1
1
Output
0
Input
2
1 2
3 4
Output
2
Input
4
5 7 8 4
9 5 3 2
1 6 6 4
9 5 7 3
Output
6
Note
In the first example two upper squares are winning.
In the third example three left squares in the both middle rows are winning:
code
5 7 8 4
9 5 3 2
1 6 6 4
9 5 7 3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We have a problem about counting squares (cells) in an n x n grid where the sum of its column is strictly greater than the sum of its row. We're given n <= 30, numbers from 1 to 100. So we can just compute row sums and column sums, then for each cell (i, j) compare col_sum[j] and row_sum[i]. Count those with col_sum[j] > row_sum[i].
But wait: The description says "Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square)." So indeed column sum for column j is sum over all rows i of a[i][j]. Row sum for row i is sum over all columns j of a[i][j]. Then a cell (i,j) is winning if col_sum[j] > row_sum[i]. Simple.
Third example: Input 4x4 given yields output 6. We can test manually later.
Thus solution is straightforward. We'll read n, then read the matrix, compute row_sums array of size n, col_sums array of size n. Then loop over i, j and count if col_sums[j] > row_sums[i].
Complexity O(n^2). Since n <= 30 trivial.
But careful: The first example: n=1, matrix [[1]] output 0. row sum = 1, col sum =1 -> not strictly greater -> 0. Good.
Edge cases: All numbers equal? For n=2, all 1's: row sums 2, col sums 2, so no winning squares. n=3, maybe some.
Thus answer is simple.
I will write code accordingly.
But wait: The note says: "In the first example two upper squares are winning." That note appears under Input 2? Actually note says: "In the first example two upper squares are winning." That might refer to the second input? Possibly misordered? Let's see: They said "Input 2 ... Output 2". Then they added note: "In the first example two upper squares are winning." Actually the note likely corresponds to the second input (the 2x2) because they refer to first example? It's confusing but irrelevant.
We'll write a solution.
But is there any trick? The problem statement says "To understand who has won, they need to count the number of winning squares." It's just counting squares where column sum > row sum.
Given constraints small, we can even compute on the fly without storing matrix: read row by row, accumulate row sums and column sums. While reading we can store each row's numbers in a temporary list if we need to later count. But we can also compute row sums directly and column sums by adding to each column. Then after reading we need to know row sums for each row and column sums for each column. Then count.
But note: For each cell we need to compare. We could just compute count by nested loops referencing row_sums[i] and col_sums[j] after we have full matrix. However we need the matrix to know the relationship? Actually we don't need the matrix after sums are computed, because the winning condition does not depend on the cell value itself; it's purely based on the row and column sums. So we could compute row_sums and col_sums, then iterate over all i,j and compare.
Thus we don't need to store the whole matrix; we can just read row by row, fill row_sums[i] and accumulate col_sums[j] from each row. But then after reading, we have the row sums and column sums. Then we can simply count: for i in range(n): for j in range(n): if col_sums[j] > row_sums[i]: count++. That does not require the matrix values. So we can avoid storing matrix.
However, caution: Is there any scenario where the cell itself might be needed? The condition says "calculate the sum of all numbers on the squares that share this column (including the given square)" - that's just column sum; similarly row sum includes the cell as well. So it's fine.
Thus code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
row_sums = [0]*n
col_sums = [0]*n
for i in range(n):
for j in range(n):
val = int(next(it))
row_sums[i] += val
col_sums[j] += val
count = 0
for i in range(n):
for j in range(n):
if col_sums[j] > row_sums[i]:
count += 1
print(count)
But the problem is quite straightforward. Possibly there is a trick where you must be careful about the indexing (i for rows, j for columns) but it's fine.
Thus final code.
I'll include a main guard.
We must note that input reading might be large? n <= 30, so at most 900 numbers, trivial.
Alternatively, we could read line by line.
I'll provide the solution.
Now output as per spec.
Thus final.
```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
row_sum = [0] * n
col_sum = [0] * n
code
# Read matrix and accumulate sums
for i in range(n):
for j in range(n):
val = int(next(it))
row_sum[i] += val
col_sum[j] += val
# Count winning squares
win_cnt = 0
for i in range(n):
for j in range(n):
if col_sum[j] > row_sum[i]:
win_cnt += 1
print(win_cnt)