Matrix Cascade
Contest: Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)
Difficulty rating: 1700
Tags: brute force, constructive algorithms, data structures, dp, greedy, math

There is a matrix of size $$$n \times n$$$ which consists of 0s and 1s. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. The cell at the intersection of the $$$x$$$-th row and the $$$y$$$-th column is denoted as $$$(x, y)$$$.

AquaMoon wants to turn all elements of the matrix to 0s. In one step she can perform the following operation:

- Select an arbitrary cell, let it be $$$(i, j)$$$, then invert the element in $$$(i, j)$$$ and also invert all elements in cells $$$(x, y)$$$ for $$$x > i$$$ and $$$x - i \ge \left|y - j\right|$$$. To invert a value means to change it to the opposite: 0 changes to 1, 1 changes to 0.

Help AquaMoon determine the minimum number of steps she need to perform to turn all elements of the matrix to 0s. We can show that an answer always exists.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 3000$$$).

The $$$i$$$-th of the following $$$n$$$ lines contains a binary string only of characters 0 and 1, of length $$$n$$$.

It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$9\,000\,000$$$.

Output format:
For each test case, print the minimum number of steps.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first test case, we can use the following scheme:

1. perform the operation on the cell $$$(1, 3)$$$.

Clearly, the elements of the initial matrix are not all 0, so at least one operation is required. Thus, $$$1$$$ is the answer.

In the second test case, we use the following scheme:

1. perform the operation on the cell $$$(3, 3)$$$;
2. perform the operation on the cell $$$(1, 1)$$$.

It can be shown that there is no way to convert all elements to 0s in $$$0$$$ or $$$1$$$ steps, so the answer is exactly $$$2$$$.

Example input:
3
5
00100
01110
11111
11111
11111
3
100
110
110
6
010101
111101
011110
000000
111010
001110

Example output:
1
2
15

Editorial:
Firstly, the first row has some elements that are 1s and some elements that are 0s. The elements that are 1 can only be turned into 0 by operating on the corresponding cell an odd number of times, and the elements that are 0 can only be turned into 0 by operating on the corresponding cell an even number of times. Two operations on the same cell are equivalent to no operation, so only one operation is performed on the corresponding cell of the element that is 1 in the first row. Thus, the operation on the first row is deterministic. Subsequent rows are affected by the operations in the first row, so it is sufficient to proceed to consider rows $$$2$$$ to $$$n$$$ in the same way.
Now consider how to quickly deal with the effect of the preceding rows on the following rows. An operation on position $$$(x,y)$$$ will simultaneously invert all the elements from column $$$y-1$$$ to column $$$y+1$$$ in row $$$x+1$$$, and from column $$$y-2$$$ to column $$$y+2$$$ in row $$$x+2$$$, and son on. Thus, the elements being inverted are exactly the portion of the matrix sandwiched between lines passing through $$$(x,y)$$$ with slopes $$$1$$$ and $$$-1$$$. Let $$$b$$$ denote the effect of the line with slope $$$1$$$ from all predecing rows, and let $$$c$$$ denote the effect of the line with slope $$$-1$$$ from all preceding rows. After an operation on $$$(i,j)$$$, $$$b[i+1][j-1]$$$ is marked, and $$$c[i+1][j+2]$$$ is marked. Next, $$$b[i][j]$$$ inherits $$$b[i-1][j+1]$$$, and $$$c[i][j]$$$ inherits $$$c[i-1][j-1]$$$. For the current row, the effect of the previous rows is obtained by maintaining the prefix sums on $$$b$$$ and $$$c$$$. The total complexity is $$$O(n^2)$$$.