Limak is a little polar bear.
Today he found something delicious in the snow.
It's a square bar of chocolate with N x N pieces.
Some pieces are special because there are cherries on them.
You might be surprised by cherries on a chocolate but you've never been on the Arctic Circle, have you?
Limak is going to use his claws and break a chocolate into two parts.
He can make a cut between two rows or between two columns.
It means that he can't break any piece!
Limak will eat one part right now, saving the second part for tomorrow.
Cherries are very important to him so he wants to have equal number of cherries today and tomorrow.
Though parts don't have to have equal numbers of pieces of chocolate.
Given description of a chocolate, could you check if Limak can make a cut dividing a chocolate into two parts with equal number of cherries?
Note: It's guaranteed that a chocolate contains at least one cherry.
Input format:
The first line contains one integer number T, denoting number of test cases.
Then T test cases follow, each describing one bar of chocolate.
For each test case the first line contains one integer number N, denoting size of a chocolate.
Each of the next N lines contains a string of size N.
Each character in a string is either # (denoting cherry) or . (empty piece).
In each test case at least one character is #.
Output format:
For each test case output an answer in the single line.
If Limak can break a chocolate according to rules above print YES.
Otherwise, print NO.
Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 1000
SAMPLE INPUT
2
3
##.
4
.##.
.#..
#...
SAMPLE OUTPUT
NO
YES
Explanation
In the first test case there are 8 cherries.
There is no cut dividing a chocolate into two parts with 4 cherries each.
In the second test case we have 8 cherries again.
Limak can break a chocolate between the first and the second row.
Each part has 4 cherries then.
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 are asked: given a square chocolate N x N grid with cherries (#) and empty (.), at least one cherry. Limak can make a single cut: either between two rows (i.e., horizontal cut at some integer position k from 1 to N-1, separating rows 1..k and rows k+1..N) or between two columns (vertical cut at some integer position, separating left and right). This cut divides the chocolate into two parts. He will eat one part, saving the other. He wants the two parts to have equal number of cherries. We need to check if such a cut exists.
In other words, is there a horizontal cut after some row r (1 <= r < N) such that the total number of cherries in rows 1..r equals the total number of cherries in rows r+1..N? Or similarly a vertical cut after some column c such that left part equals right part.
Note: The cut is made between rows or columns, not both; only one cut. Also the cut cannot be diagonal etc. Only a straight line along the grid lines. So we are checking if there is a split of the grid into two rectangular parts (top/bottom or left/right) with equal cherry counts.
Given that the chocolate is square, N up to 1000, T up to 10, we can compute prefix sums and check.
Simplify: Compute total number of cherries C. We need to find if there exists a row index r such that sum of cherries in rows 1..r = C/2. Similarly for column.
Thus we can compute row-wise cumulative sum or column-wise. Since cuts are only along full rows or columns, not arbitrary.
But careful: When we cut horizontally, the two parts are top rectangle (r rows) and bottom rectangle (N-r rows). Both are full width. So the condition is: top cherries = total/2.
Similarly vertical cut: left cherries = total/2.
Hence we need to check if the cumulative sum of cherries across rows (or columns) ever equals exactly half of total.
So straightforward: For each test case, read N and grid. Count total cherries. If total is odd, immediately NO because cannot split equally. If total is even, compute prefix sums over rows: let rowSum[i] be number of cherries in row i. Then compute cumulative sum cumRows = 0; for r from 1 to N-1: cumRows += rowSum[r]; if cumRows == total/2 => YES (horizontal cut works). Also similarly compute cumulative sum over columns: For each column c, compute columnSum[c] = number of cherries in that column. Then cumCols = 0; for c from 1 to N-1: cumCols += columnSum[c]; if cumCols == total/2 => YES.
If none, output NO.
But is there any trick? The input constraints N up to 1000 and grid of size 1000x1000 = 1e6 per test. T up to 10 => up to 1e7 cells. Should be fine with O(N^2) per test.
Edge cases: The cut must be between rows/columns, so if total/2 equals 0? But there is at least one cherry, and total even, total/2 at least 1. So fine.
Check sample:
First test: N=3, grid:
##.
Count #: row1:3, row2:2, row3:3 => total=8 (even). Cumulative rows: after r=1: cum=3 !=4; r=2: cum=5 !=4. So horizontal fails. Columns: col1: #,#,# =>3; col2: #,#,# =>3; col3: #,.,# =>2? Wait row2 col3 is '.' so col3: row1#, row2., row3# =>2. Cumulative columns: after c=1:3!=4; after c=2:6!=4. So NO.
Second test: N=4. Let's compute quickly: grid:
(4)
.##. (2? actually .##. => row2: positions: col1 ., col2 #, col3 #, col4 . => 2)
.#.. ( row3: . # . . =>1)
#... ( row4: # . . . =>1)
Total = 4+2+1+1=8 (even). Cumulative rows: r=1:4 !=4? Actually 4 equals 4! So after row1 cum =4 = total/2 => YES. So horizontal cut between row1 and row2 works. So answer YES.
Hence straightforward.
But wait: Are there any other considerations? The problem statement says "He can make a cut between two rows or between two columns. It means that he can't break any piece!" So indeed only cuts along whole grid lines.
Thus solution is simple.
Complexities: O(N^2) per test case to compute row sums and column sums.
Implementation details:
We'll read T. For each test case: read N (integer). Then read N lines, each a string of length N. We'll compute total, rowSum array, colSum array (size N). For each row i, for each column j: if grid[i][j] == '#', increment rowSum[i], colSum[j], total++. After reading all, if total%2==1 => print NO; else: compute half = total/2.
Check horizontal: cum = 0; for i from 0 to N-2 (0-indexed rows): cum += rowSum[i]; if cum == half => YES (break). Else continue.
If not found, check vertical: cum = 0; for j from 0 to N-2: cum += colSum[j]; if cum == half => YES (break). If still not, NO.
Edge Cases: Could there be a scenario where both parts have equal cherries but cut is not exactly at cumulative half? Actually by definition, the cut splits the grid into two rectangles. For horizontal cut after row r, the top part includes rows 0..r (if we 0-index) and bottom includes r+1..N-1. So the top cherries are sum of rows 0..r. So it must be exactly cumulative sum of first r+1 rows? Wait careful: If we define r as cut after row r (meaning rows 0 to r-1 are top, rows r to N-1 are bottom if cut between row r-1 and row r). Typically: If we number rows from 1 to N. Cut after row k means top part: rows 1..k; bottom: rows k+1..N. So cumulative sum after processing k rows = sum rows 1..k. So when iterating r from 1 to N-1, we check sum of first r rows. In 0-index, we can iterate i from 0 to N-2, cum includes rowSum[i] as we go. After adding row i (0-index), we have included rows 0..i, which correspond to first i+1 rows. So condition cum == half after i (0-index) corresponds to cut after row i+1 (1-index). That's correct.
Similarly for columns.
Thus algorithm stands.
Potential pitfalls: Input reading large lines, use sys.stdin.
But wait: The constraints: N up to 1000, T up to 10. So total characters up to 10 * 1e6 = 10 million, fine.
But there is nuance: The problem statement: "He can make a cut between two rows or between two columns. It means that he can't break any piece!" That's clear.
Thus solution is simple.
However, we must consider if there is any trick such as both parts must be contiguous? They are contiguous by definition. No other restrictions.
Thus answer.
But I want to double-check with some more examples:
Case: N=2, grid:
#.
.# total cherries = 2 (even). Row sums: row1:1, row2:1. cum after row1 =1 = half => YES (horizontal cut). So works.
Case: N=2, grid:
.. total=2, row sums: row1=2, row2=0. cum after row1=2 !=1, no horizontal; col sums: col1=1 (row1 #, row2 .) =>1; col2=1 =>1. cum after col1=1=half => YES vertical.
Case: N=3, total odd=5? But there is at least one cherry, and odd total always NO.
But is there possibility that cut is vertical and half sum appears as cumulative sum over columns? Yes.
Implementation straightforward.
Edge: N=1000, all cherries (#) => total = 1e6, half = 500000. Cumulative over rows: each row has 1000 cherries. cum after r rows = r*1000. For half to be integer, need r = 500 => works. So YES.
If total even but no cumulative sum exactly equals half, then NO.
But could there be scenario where we can cut both horizontally and vertically? Not needed.
Thus final answer.
Time Complexity: O(T * N^2).
But maybe we can do it in O(N^2) with scanning row sums and column sums.
Note: The problem might be tricky if we misinterpret "cut between two rows or between two columns". Could there be cut that splits into two parts that are not necessarily top/bottom or left/right? For instance, if cut between rows, the two parts are the top part and bottom part. If cut between columns, left and right. That's it.
Thus done.
Let's produce the solution.
I'll write in Python.
Pseudo:
def solve():
import sys
data = sys.stdin.read().splitlines()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
rowSum = [0]*n
colSum = [0]*n
total = 0
for i in range(n):
line = next(it).strip()
# line length should be n
for j,ch in enumerate(line):
if ch == '#':
total += 1
rowSum[i] += 1
colSum[j] += 1
if total % 2 == 1:
out.append("NO")
continue
half = total // 2
possible = False
cum = 0
for i in range(n-1):
cum += rowSum[i]
if cum == half:
possible = True
break
if not possible:
cum = 0
for j in range(n-1):
cum += colSum[j]
if cum == half:
possible = True
break
out.append("YES" if possible else "NO")
sys.stdout.write("\n".join(out))
Test with sample.
Given sample input:
2
3
##.
4
.##.
.#..
#...
Our code should produce:
NO
YES
Works.
Thus final answer.
But ensure that reading lines using .splitlines() and iter works.
Alternatively we can use sys.stdin.readline.
That's fine.
Now produce final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().splitlines()
it = iter(data)
t = int(next(it))
out_lines = []
code
for _ in range(t):
n = int(next(it))
row_sum = [0] * n
col_sum = [0] * n
total = 0
for i in range(n):
line = next(it).strip()
# line length is exactly n
for j, ch in enumerate(line):
if ch == '#':
total += 1
row_sum[i] += 1
col_sum[j] += 1
if total % 2 == 1:
out_lines.append("NO")
continue
half = total // 2
possible = False
# try horizontal cuts
cum = 0
for i in range(n - 1):
cum += row_sum[i]
if cum == half:
possible = True
break
# if not, try vertical cuts
if not possible:
cum = 0
for j in range(n - 1):
cum += col_sum[j]
if cum == half:
possible = True
break
out_lines.append("YES" if possible else "NO")
sys.stdout.write("\n".join(out_lines))