The cloth coasters produced and sold by Aizu Takada City are known for their symmetrical design and great beauty. As part of quality control, Aizu Takada City has installed cameras on the production line to automatically verify that the images obtained by shooting each coaster are symmetrical. Each coaster is represented as a square black and white image of N x N pixels. Each pixel has a value of 0 or 1 corresponding to a white or black image.
This time, the software of the image analysis system will be updated along with the equipment update of the production line. The new system has been devised to reduce the amount of communication data, and data is sent from the camera to the analysis system by the following method.
The information of the first coaster flowing on the line is sent to the system as an N × N pixel image.
For the coaster information on the second and subsequent images, only the difference from the previous image is sent. The difference is given as a set of pixel positions that change from "0 to 1" or "1 to 0".
For C coasters, enter the pixel information of the first image and the difference information of the following C-1 image, and create a program that reports the number of coasters that are vertically symmetrical and symmetrical.
Input
The input is given in the following format.
C N
p11p12 ... p1N
p21p22 ... p2N
::
pN1pN2 ... pNN
diff1
diff2
::
diffC-1
The first line gives the number of coasters C (1 ≤ C ≤ 10000) and the number of pixels N (2 ≤ N ≤ 1000 and N is even) in the vertical and horizontal directions of the image. Lines 2 to N + 1 are given the number pij (pij is 0 or 1) in rows N x columns N representing the pixels of the image of the first coaster.
After the N + 2nd line, the difference diffi representing the information of the 2nd and subsequent coasters is given in the following format.
D
r1 c1
r2 c2
::
rD cD
The number of changed pixels D (0 ≤ D ≤ 100) is given in the first line. The following D rows are given ri and ci (1 ≤ ri, ci ≤ N), which represent the row and column numbers of the changed pixels, respectively. The same position cannot be given more than once in diffi.
Output
The number of coasters that are vertically symmetrical and symmetrical is output on one line.
We need to solve problem: Given C coasters (images of size NxN, N even). First image given fully. Then for each subsequent image, we get diff: D and list of positions that flip (0->1 or 1->0) from previous image. So we need to compute each image and determine if it is "vertically symmetrical and symmetrical"? Wait "vertically symmetrical and symmetrical"? The problem statement says: "reports the number of coasters that are vertically symmetrical and symmetrical." But reading original Japanese maybe? It says: "縦の線対称であり左右対称であるようなコースターの枚数を報告するプログラムを作成してください." Which means: "create a program that reports the number of coasters that are vertically line symmetric and left-right symmetric". Wait "縦の線対称であり左右対称である" could be interpreted as "vertically line-symmetric and left-right symmetric". However, maybe they mean "vertically symmetric" and "horizontally symmetric"? But note: they say "縦の線対称であり左右対称である". The phrase "縦の線対称" means "vertical line symmetry" (i.e., reflection across a vertical axis). And "左右対称" is essentially "left-right symmetric", which might be the same as vertical line symmetry. Actually, "左右対称" literally means left-right symmetric, which is symmetry with respect to a vertical line. So it's redundant? Possibly they want both vertical and horizontal symmetry? But "縦の線対称" might refer to symmetry about a horizontal line? Wait: In Japanese, "縦の線対称" could be ambiguous. "縦" means vertical, so "縦の線" could be a vertical line. So "縦の線対称" would be symmetry about a vertical line. And "左右対称" also is symmetry about a vertical axis. That seems odd: they might be asking for symmetry about both vertical and horizontal axes? Alternatively, maybe "縦の線対称" means symmetry with respect to the vertical center line (i.e., left-right symmetry) and "左右対称" means something else? Or maybe it's a translation error: they want "vertical and horizontal symmetry"? Let's check examples.
Example 1:
C=7, N=8. Images? They give first image as 8 lines:
00100000
00011000
10111101
01100110
01000110
10111101
00011000
00100100
Then diffs: 6 diffs after that? Actually number of diffs = C-1 =6. They list:
2
5 3
1 6
1
6 8
3
6 8
3 3
3 6
2
6 3
6 6
0
2
3 8
6 8
So we need to compute images and count those that are "vertically symmetrical and symmetrical".
Let's check manually the first image. Is it symmetric? For vertical line symmetry (left-right), we need each pixel (i,j) to equal (i, N+1-j). For horizontal line symmetry, (i,j) = (N+1-i, j). For both? The phrase "vertically symmetrical and symmetrical" could mean both vertical and horizontal symmetry? But they'd likely say "vertically and horizontally symmetrical". Yet they say "縦の線対称であり左右対称である". "縦の線対称" could be "symmetric with respect to the vertical axis", and "左右対称" could be "symmetric with respect to the left and right"? Actually "左右対称" is also symmetry about vertical axis. So maybe they want symmetric about both the vertical axis and the diagonal? Unlikely.
Let's search memory: There's a known problem from JOI? Possibly "Aizu" tasks. Could be "Takada City" is from Aizu Online Judge? There is a problem "Cloth Coasters" maybe. I recall a problem about checking symmetry with vertical line and also maybe diagonal? Alternatively, maybe "縦の線対称であり左右対称である" means "vertically symmetric and left-right symmetric" which is the same, so maybe it's just "vertically symmetric". Could be a translation nuance: "縦の線対称" might be "symmetric about a horizontal line"? Wait, in Japanese, when they talk about "縦" meaning vertical, they refer to orientation. But "縦の線" could be a vertical line; "縦線" is vertical line. So "縦の線対称" is symmetry with respect to a vertical line. "左右対称" is left-right symmetry, also about a vertical line. So they are essentially the same. So why mention both? Could be that "縦の線対称" means symmetric with respect to the vertical center line of the square, and "左右対称" means symmetric left and right? Hmm.
Maybe the phrase is "縦の線対称であり, 左右対称である" meaning "vertically line-symmetric and left-right symmetric". That would be tautology. Could be a misinterpretation: Actually "縦の線対称" could be "vertical line symmetry" meaning symmetry with respect to a horizontal line? No, "vertical line" is a line that is vertical. So "vertical line symmetry" means reflect across a vertical line. That's left-right symmetry. So it's the same.
Maybe they meant "vertically symmetric and horizontally symmetric"? The Japanese for horizontal symmetry would be "横の線対称" or "上下対称". Not "左右対称". So not.
Let's look at example 1 output: 3. We can test hypothesis: Count number of images that are symmetric about both vertical and horizontal axes? Let's test first image manually to see if it's symmetric about vertical axis? Compute first row: "00100000". Index columns 1..8. For vertical symmetry: column1 vs column8: 0 vs 0 okay; col2 vs col7: 0 vs 0; col3 vs col6: 1 vs 0 -> mismatch! So not vertically symmetric. So first image would not count if we require vertical symmetry. Horizontal symmetry? Row1 vs row8: row1=00100000, row8=00100100 -> row1 col3=1, row8 col3=1? row8: 0 0 1 0 0 1 0 0? Actually row8: "00100100": positions: 1:0,2:0,3:1,4:0,5:0,6:1,7:0,8:0? Let's parse: "00100100": digits: 0,0,1,0,0,1,0,0. So row1 col3=1, row8 col3=1 okay; col4=0 vs 0; col5=0 vs 0; col6=0 vs 1 mismatch. So not horizontal either. So first image not symmetric in either axis.
So maybe they require something else: maybe "vertically symmetrical and symmetrical" means it's symmetric both along vertical axis and also symmetric along the main diagonal? Or maybe it's "vertically and horizontally symmetric"? But first image doesn't satisfy, so maybe it's not counted.
We need to compute all 7 images and see which count. Let's try to compute the images based on diffs. That would be time-consuming but doable for small N. But we don't have code here, but we can reason about symmetry conditions.
Alternate possibility: The phrase might be "vertically symmetrical and centrally symmetrical"? That is, rotational symmetry of 180 degrees? "左右対称" could be misinterpreted: Actually "左右対称" is left-right symmetry (mirror). "点対称" is point symmetry (180-degree rotation). So not.
Maybe they meant "vertically symmetrical" meaning symmetric with respect to the vertical center line, and "horizontally symmetrical"? But they said "左右対称" which is vertical mirror. So that's two identical terms.
Look at input format: It says "vertically symmetrical and symmetrical" in English translation. The original Japanese probably: "縦の線対称であり左右対称であるような". This is a bit odd. Could be a mis-translation of "vertically and horizontally symmetric"? Actually "縦の線対称" could be "symmetry about a horizontal line"? In Japanese, "縦" means vertical as in the direction, but "縦の線" might be a line that runs vertically, i.e., a vertical line. However, when describing symmetry, sometimes "縦に線対称" means symmetric across a vertical axis. "左右対称" means left-right symmetric. So it's essentially the same.
Maybe the problem expects to check both vertical and horizontal symmetry? But then they'd say "縦の線対称であり横の線対称である". Since they didn't, it's unlikely.
This is a single image. Output: 1. So this image is counted. Let's examine its symmetry:
Rows:
1: 000000
2: 000000
3: 010010
4: 010010
5: 000000
6: 000000
Check vertical symmetry: For each row, check left-right. Row3: 0 1 0 0 1 0? Actually row3: "010010": digits: col1=0, col2=1, col3=0, col4=0, col5=1, col6=0. For vertical symmetry: col1 vs col6: 0=0; col2 vs col5: 1=1; col3 vs col4: 0=0 -> symmetric. Row4 same. Rows 1,2,5,6 all zeros, symmetric. So vertical symmetry holds.
Horizontal symmetry: Row1 vs row6: both 000000; row2 vs row5: 000000; row3 vs row4: both 010010. So horizontal symmetry holds as well. So this image is both vertically and horizontally symmetric. So it's counted.
Now, if requirement was only vertical symmetry, it would also be counted because vertical symmetry holds. But could there be images that are vertically symmetric but not horizontally, yet still counted? We need to see example 1 result (3) to infer.
Let's attempt to compute the 7 images for example 1 and test both vertical and horizontal symmetry. We'll manually compute if possible. But we can try to reason: maybe requirement is "vertically symmetric" only. Then we count images that have vertical symmetry. That might yield count 3. We can test approximate.
Better approach: simulate the images in code quickly mentally? Might be tedious but doable given N=8, C=7, D small.
We'll parse first image as matrix M1[1..8][1..8]. Represent as binary.
First image lines:
Line1: 00100000 -> [0,0,1,0,0,0,0,0]? Actually check: "00100000": characters: index1: '0', 2:'0', 3:'1', 4:'0', 5:'0', 6:'0', 7:'0', 8:'0'? Wait string length 8: positions: 1:0, 2:0, 3:1, 4:0, 5:0, 6:0, 7:0, 8:0? That seems "00100000" has '1' at position 3. But earlier I thought maybe col6 mismatch? Let's write correctly:
Now check vertical symmetry for M1: For each row i, for j=1..N, check M1[i][j] == M1[i][N+1-j].
Row1: j=1 vs 8: 0 vs 0 ok; j=2 vs7:0 vs0; j=3 vs6:1 vs0 -> fail. So not symmetric vertically.
Horizontal symmetry: Check row1 vs row8: row1: [0,0,1,0,0,0,0,0]; row8:[0,0,1,0,0,1,0,0] -> col6: row1=0, row8=1 fail. So not.
Now we process diffs.
We have C-1=6 diffs. Let's list each diff with given D and positions.
First diff (for image2, based on M1):
2
5 3
1 6
So D=2. Flip positions: (5,3) and (1,6). So for image2, we flip those pixels from M1 (i.e., change 0<->1). Note: "only the difference from the previous image is sent. The difference is given as a set of pixel positions that change from '0 to 1' or '1 to 0'." So it's a list of positions that are flipped relative to previous image. So we apply flips.
Let M2 = M1 with flips at (5,3) and (1,6).
M1[5][3] is row5 col3: row5 = "01000110" -> col1=0,col2=1,col3=0,col4=0,col5=0,col6=1,col7=1,col8=0? Wait row5: "01000110": digits: 0,1,0,0,0,1,1,0? Actually check: string "01000110" length 8: index1:0, 2:1, 3:0, 4:0, 5:0, 6:1, 7:1, 8:0? But careful: "01000110": after "0100" then "0110"? Actually let's write: 0-1-0-0-0-1-1-0. So col3 = 0. So flipping gives 1.
M1[1][6] row1 col6: row1 = "00100000" -> col6 = 0 (since col6 is the sixth char: positions: 1:0,2:0,3:1,4:0,5:0,6:0,7:0,8:0). So flipping gives 1.
Now M2: row1 becomes: 00100100? Actually row1 originally: 0,0,1,0,0,0,0,0. Flip col6: from 0 to 1 -> row1: 0,0,1,0,0,1,0,0 = "00100100". Row5 originally: 0,1,0,0,0,1,1,0. Flip col3: from 0 to 1 -> row5: 0,1,1,0,0,1,1,0 = "01100110"? Actually row5 becomes: col1=0,col2=1,col3=1,col4=0,col5=0,col6=1,col7=1,col8=0 => "01100110". All other rows same as M1.
Vertical symmetry: For each row, compare left-right.
Row1: [0,0,1,0,0,1,0,0] -> col1 vs col8:0=0; col2 vs col7:0=0; col3 vs col6:1 vs1; col4 vs col5:0 vs0 -> symmetric. So row1 vertically symmetric.
Row2: [0,0,0,1,1,0,0,0] -> col1-8:0=0; col2-7:0=0; col3-6:0 vs0? col3=0, col6=0 ok; col4-5:1 vs1 -> symmetric.
Row3: [1,0,1,1,1,1,0,1] -> col1 vs col8:1 vs1 ok; col2 vs col7:0 vs0 ok; col3 vs col6:1 vs1 ok; col4 vs col5:1 vs1 ok -> symmetric? Wait check col7=0? row3: [1,0,1,1,1,1,0,1] col7=0, col2=0 matches; col4=1 col5=1 matches. So row3 symmetric.
Row4: [0,1,1,0,0,1,1,0] -> col1 vs col8:0=0; col2 vs col7:1 vs1; col3 vs col6:1 vs1; col4 vs col5:0 vs0 -> symmetric.
Row5: [0,1,1,0,0,1,1,0] -> same as row4? Actually row5 = 0,1,1,0,0,1,1,0 (from flip) yes same as row4. So symmetric.
Row6: [1,0,1,1,1,1,0,1] same as row3 -> symmetric.
Row7: [0,0,0,1,1,0,0,0] same as row2 -> symmetric.
Row8: [0,0,1,0,0,1,0,0] same as row1 -> symmetric.
So M2 is vertically symmetric (each row symmetric). So vertical symmetry holds.
Horizontal symmetry: Check row1 vs row8: row1 = [0,0,1,0,0,1,0,0], row8 = [0,0,1,0,0,1,0,0] identical -> ok.
Row2 vs row7: both [0,0,0,1,1,0,0,0] -> ok.
Row3 vs row6: both [1,0,1,1,1,1,0,1] -> ok.
Row4 vs row5: both [0,1,1,0,0,1,1,0] -> ok.
So M2 also horizontally symmetric. So M2 is both vertically and horizontally symmetric. So it would count under both criteria.
Now second diff (for image3, based on M2):
diff2: given as:
1
6 8
Actually the input after first diff? Let's parse the input exactly:
So after first diff (2 lines of positions), we have next line: "1". That is D=1 for diff2. Then line: "6 8" (position). Then next line: "3" D=3 for diff3. Then lines: "6 8", "3 3", "3 6". Then "2" D=2 for diff4. Then "6 3", "6 6". Then "0" D=0 for diff5. Then "2" D=2 for diff6. Then "3 8", "6 8".
So we have six diffs: diff1 (D=2), diff2 (D=1), diff3 (D=3), diff4 (D=2), diff5 (D=0), diff6 (D=2). Good.
Now we computed M2 from M1 using diff1. Now apply diff2 to M2 to get M3.
diff2: D=1, position (6,8). Flip (6,8) in M2.
M2[6][8] is row6 col8. Row6: same as row3 = [1,0,1,1,1,1,0,1] -> col8 = 1. Flip to 0.
So M3: same as M2 except row6 col8 becomes 0.
Now M3 rows:
Row1: as M2 (0,0,1,0,0,1,0,0)
Row2: (0,0,0,1,1,0,0,0)
Row3: (1,0,1,1,1,1,0,1)
Row4: (0,1,1,0,0,1,1,0)
Row5: (0,1,1,0,0,1,1,0)
Row6: (1,0,1,1,1,1,0,0) (col8 now 0)
Row7: (0,0,0,1,1,0,0,0)
Row8: (0,0,1,0,0,1,0,0)
Check symmetry:
Vertical symmetry: For each row, need left-right.
Row1: symmetric as before.
Row2: symmetric.
Row3: check: row3 = [1,0,1,1,1,1,0,1] col1 vs col8:1 vs1; col2 vs col7:0 vs0; col3 vs col6:1 vs1; col4 vs col5:1 vs1 -> symmetric.
Row4: symmetric.
Row5: symmetric.
Row6: [1,0,1,1,1,1,0,0] check: col1=1, col8=0 -> 1 vs 0 -> not symmetric. So vertical symmetry fails.
Thus M3 is not vertically symmetric.
Horizontal symmetry: check row1 vs row8: row1= [0,0,1,0,0,1,0,0]; row8= same -> ok.
Row2 vs row7: both [0,0,0,1,1,0,0,0] -> ok.
Row3 vs row6: row3= [1,0,1,1,1,1,0,1]; row6= [1,0,1,1,1,1,0,0] -> col8 mismatch (1 vs0) -> fails.
So M3 is not horizontally symmetric either.
Thus M3 is not counted if we require both symmetries? But if we require only vertical symmetry, it fails. So not counted.
Now diff3: D=3, positions: (6,8), (3,3), (3,6). Apply to M3 to get M4. But careful: diffs are applied sequentially, each based on previous image. So M4 = M3 with flips at those three positions.
Check vertical symmetry:
R1: symmetric.
R2: symmetric.
R3: [1,0,0,1,1,0,0,1] check col1 vs col8:1=1; col2 vs col7:0=0; col3 vs col6:0 vs0? col3=0, col6=0 ok; col4 vs col5:1 vs1 -> symmetric? Wait col4=1, col5=1 yes. So R3 symmetric.
R4: symmetric.
R5: symmetric.
R6: [1,0,1,1,1,1,0,1] symmetric? Check col1 vs col8:1=1; col2 vs col7:0=0; col3 vs col6:1 vs1; col4 vs col5:1 vs1 -> symmetric.
R7: symmetric.
R8: symmetric.
So vertical symmetry holds for M4.
Horizontal symmetry:
Check row1 vs row8: R1 vs R8: both [0,0,1,0,0,1,0,0] -> ok.
Row2 vs row7: both [0,0,0,1,1,0,0,0] -> ok.
Row3 vs row6: R3=[1,0,0,1,1,0,0,1]; R6=[1,0,1,1,1,1,0,1]. Compare: col3:0 vs1 mismatch -> fails.
So horizontal symmetry fails. So M4 is vertically symmetric but not horizontally.
Thus if requirement is vertical symmetry only, M4 counts. If requirement is both, M4 does not count.
Now diff4: D=2, positions: (6,3), (6,6). Apply to M4 to get M5.
Check vertical symmetry:
R1 symmetric.
R2 symmetric.
R3: [1,0,0,1,1,0,0,1] symmetric? Check col1 vs col8:1=1; col2 vs col7:0=0; col3 vs col6:0=0; col4 vs col5:1=1 -> symmetric.
R4 symmetric.
R5 symmetric.
R6 same as R3 -> symmetric.
R7 symmetric.
R8 symmetric.
Thus vertical symmetry holds.
Horizontal symmetry:
Row1 vs Row8: same as before -> ok.
Row2 vs Row7: same -> ok.
Row3 vs Row6: both [1,0,0,1,1,0,0,1] -> identical, so ok.
Row4 vs Row5: both [0,1,1,0,0,1,1,0] -> identical, so ok.
Thus M5 is horizontally symmetric as well! So M5 is both vertical and horizontal symmetric.
Thus M5 counts under both criteria.
Now diff5: D=0 (no changes). So M6 = M5 (same as M5). So M6 is also both vertical and horizontal symmetric. So counts.
Now diff6: D=2, positions: (3,8), (6,8). Apply to M6 (which is M5) to get M7.
Check vertical symmetry:
R1 symmetric.
R2 symmetric.
R3: [1,0,0,1,1,0,0,0] check col1 vs col8: 1 vs 0 -> fail. So vertical symmetry fails. So M7 not vertically symmetric.
Horizontal symmetry:
Row1 vs Row8: same -> ok.
Row2 vs Row7: both [0,0,0,1,1,0,0,0] -> ok.
Row3 vs Row6: both [1,0,0,1,1,0,0,0] -> identical, ok.
Row4 vs Row5: identical, ok.
So M7 is horizontally symmetric but not vertically.
Thus M7 not counted under vertical only? Not counted.
Now we have images:
M1: not vertical (nor horizontal) -> not count.
M2: vertical (and horizontal) -> count if vertical only; also both? yes vertical.
M3: not vertical -> no.
M4: vertical -> yes if vertical only.
M5: vertical -> yes.
M6: vertical -> yes.
M7: not vertical -> no.
So count of images with vertical symmetry = M2, M4, M5, M6 = 4. But example output is 3. So not 4.
If count both vertical and horizontal symmetry: M2, M5, M6 = 3. That matches output 3! Check: M2 both, M5 both, M6 both. M4 only vertical, not both; M1 none; M3 none; M7 only horizontal. So only M2, M5, M6 satisfy both symmetries. That yields count 3.
Thus the problem asks for images that are symmetric with respect to both vertical and horizontal axes. In other words, the image must be both vertically and horizontally symmetric. The phrase "縦の線対称であり左右対称である" likely means "vertically line-symmetric and horizontally symmetric"? But they wrote "左右対称" which typically means left-right symmetric (vertical). That seems contradictory. However, based on example, it's both vertical and horizontal symmetry. Could it be that "縦の線対称" means "symmetry about a horizontal line"? Wait, maybe they consider "縦の線" meaning a line that is vertical? Actually, I recall in Japanese, "縦" can mean "vertical" as direction, but "縦の線" is a line that is vertical. But "縦の線対称" could be symmetric with respect to a vertical line. Meanwhile "左右対称" is also symmetric with respect to vertical line. So still conflict.
Maybe they meant "縦の線対称であり, 横の線対称である"? But they wrote "左右対称". Could it be a typo? Let's check original problem statement if available. But given the example, it's clear: we need to count images that are both vertically and horizontally symmetric. The output of 3 matches that interpretation. And example 2 also matches: that image is both vertical and horizontal symmetric, counted as 1.
Now check example 3: Input:
2 2
00
00
4
1 1
1 2
2 1
2 2
So C=2, N=2 even. First image all zeros (2x2):
00
00
Is it symmetric both vertically and horizontally? Check: vertical: each row: row1: [0,0] -> symmetric; row2: [0,0] symmetric. Horizontal: row1 vs row2: both [0,0] -> symmetric. So first image counts.
Now diff: D=4, positions: (1,1),(1,2),(2,1),(2,2). That flips all pixels, so second image becomes all ones:
11
11
Check vertical: each row [1,1] symmetric; horizontal: both rows same -> symmetric. So second image also counts. Output is 2. So both images counted, output 2. This matches both-symmetry requirement. If only vertical, both would also count, so it wouldn't differentiate. So example 3 doesn't help differentiate, but it's consistent.
Thus we conclude: We need to count images that are both vertically and horizontally symmetric.
Now we need to design a solution efficient for constraints: C up to 10000, N up to 1000 (even). D up to 100 per diff. So we can't store full images for each coaster? Possibly we can, but N up to 1000, N^2 = 1e6, times C up to 1e4 would be 1e10, too large. But we only have C up to 10000 and each diff is small (<=100 changes). We need to compute each image and check symmetry without storing all images explicitly? We can maintain current image as an NxN matrix. That's 1e6 cells, which is okay memory (1 million booleans). For each coaster we need to check vertical and horizontal symmetry. But if we do O(N^2) per image, that would be 1e6 * 1e4 = 1e10 too slow. We need to do incremental updates: maintain some representation of symmetry status.
Given that only up to 100 positions change between consecutive images, we can maintain the current image and also maintain some data structures to quickly check if the image is both vertically and horizontally symmetric after each update.
We need to count number of images (including first) that satisfy: For all i, j: a[i][j] == a[i][N+1-j] (vertical mirror) AND a[i][j] == a[N+1-i][j] (horizontal mirror). Since N is even, the mirror indices pair up. Note: Typically for even N, the axis of symmetry is between columns N/2 and N/2+1? Actually for vertical symmetry about the vertical center line, we require that for each row i, a[i][j] = a[i][N+1-j] for all j. That means the matrix can be divided into pairs (j, N+1-j). Since N even, there is no central column; each column has a unique partner. Similarly for rows: (i, N+1-i) are pairs.
Thus the conditions essentially require that the matrix is symmetric under 180-degree rotation? Wait, both vertical and horizontal symmetry together implies also symmetry under 180-degree rotation? Actually vertical + horizontal symmetry implies also symmetry under 180-degree rotation? Let's see: If we reflect vertically then horizontally, we get 180-degree rotation. Since both symmetries hold, the image is invariant under 180-degree rotation. But it also might have quarter-turn symmetry? Not necessarily. But our condition is simply both.
We can think of equivalence classes of positions under the symmetries. The constraints force that all positions in each orbit under the group generated by vertical and horizontal reflections must have the same value. What are the orbits? For a position (i,j), applying vertical reflection gives (i, N+1-j). Applying horizontal gives (N+1-i, j). Combining yields (N+1-i, N+1-j). The group is of size at most 4. However, if (i,j) lies on an axis (i.e., i = N+1-i or j = N+1-j), but N even, so i = N+1-i would imply i = (N+1)/2, but N even => N+1 is odd, so (N+1)/2 is not integer; thus there is no fixed row or column; all rows/columns come in pairs. So each orbit has exactly 4 distinct positions: (i,j), (i, N+1-j), (N+1-i, j), (N+1-i, N+1-j). Provided that these four are distinct. Could some coincide? Since N even, i and N+1-i are distinct because i != N+1-i (as no middle row). Similarly j != N+1-j. Also, could (i,j) equal (i, N+1-j)? Only if j = N+1-j, impossible. So all 4 distinct.
Thus the image is both vertically and horizontally symmetric if and only if for each such quadruple, all four pixels have the same value. That is, the value is constant on each orbit.
We can use this to check symmetry efficiently: we only need to ensure that for each orbit, the four positions are consistent. Initially we can compute for each orbit whether they are all equal. Then when we flip a pixel (change its value), it affects the orbit's consistency. Since D per diff is small, we can update the status of orbits that are impacted by the changed pixels.
So we can maintain a counter of how many orbits are "good" (all four equal) or "bad" (not all equal). The image is fully symmetric if all orbits are good. Since there are (N/2)(N/2) = N^2/4 orbits? Let's compute: Each orbit consists of 4 cells. The total cells N^2, each cell appears in exactly one orbit? Check: The mapping partitions the grid into groups of four: For i from 1 to N/2, for j from 1 to N/2, the quadruple is (i,j), (i, N+1-j), (N+1-i, j), (N+1-i, N+1-j). Indeed, because if we take all i in [1, N/2] and j in [1, N/2], we cover all cells uniquely. Because mapping (i,j) to (N+1-i, N+1-j) flips both, so each cell belongs to exactly one such quadruple defined by the "minimum" indices? Since N even, the set of pairs (i,j) with i <= N/2 and j <= N/2 covers each orbit exactly once. So number of orbits = (N/2)(N/2) = N^2/4.
Thus we can maintain an array representing the state of each orbit. For each orbit, we need to know if its four cells have equal values. We can store the value of the first cell (or representative) and check others? But we need to update efficiently when a cell flips. Flipping a cell will affect the orbit it belongs to. Since each cell belongs to exactly one orbit, we can compute orbit index from (i,j). For a given (i,j), we need to map to the canonical orbit coordinates: let i' = min(i, N+1-i), j' = min(j, N+1-j). Since N even, both i' and j' will be between 1 and N/2. Because the smaller of i and N+1-i is <= N/2. Indeed, for i=1..N, the min(i, N+1-i) ranges from 1 to N/2. So orbit identifier (oi, oj) = (min(i,N+1-i), min(j,N+1-j)). But careful: This mapping might not be unique across all four cells? For a given orbit, all four cells map to the same (min(i), min(j))? Let's test: For (i,j) = (2,3) with N=8, N+1=9. min(2,7)=2; min(3,6)=3. For the other cell (2,6): min(2,7)=2; min(6,3)=3 -> same. (7,3): min(7,2)=2; min(3,6)=3 -> same. (7,6): min(7,2)=2; min(6,3)=3 -> same. So yes, they all map to same (oi,oj). So we can use (oi, oj) as orbit ID.
Thus we can represent each orbit by its coordinates (r,c) where r in 1..N/2, c in 1..N/2.
Now we need to maintain for each orbit: whether the four cells are equal. To check this, we could store the values of the four cells? But we can also compute the condition by comparing the values. Since we have the full image data, we could just look up the four cells each time we need to check the orbit after changes. Since each flip affects only up to 1 cell (but flips may affect multiple cells in different orbits; each flip changes one pixel, which belongs to one orbit). However, a single diff may have up to 100 flips, each affecting one orbit. So we can process flips one by one: for each flip at (i,j), compute the orbit (oi,oj). Flip the pixel value in our stored image. Then we need to recompute the status of that orbit (whether its four cells are equal). Since the flip may change the equality condition, we update a global counter of "good" orbits accordingly. Then after processing all flips for a diff (i.e., after updating to next image), we check if all orbits are good; if yes, increment answer.
But careful: The diff is applied to the previous image to get the next image. So we start with initial image M1. We compute initial status of all orbits (good or bad) and count good orbits. Initially, if all orbits are good, then M1 is symmetric (both axes). Then for each subsequent image, we apply the diff: for each (r,c) in diff, we flip that pixel in the current image. We must update the orbit status for the orbit containing that pixel. However, note: flipping one pixel may affect only that orbit; the other orbits unchanged. So we can maintain an integer goodOrbitsCount = number of orbits where all four cells equal. Initially compute by scanning all orbits (N^2/4) which is up to 250k for N=1000 (since N^2/4 = 250,000). That's fine (O(N^2) once). Then for each flip, we update that one orbit's status: before flip, we check its current status (good or bad). We then flip the pixel. After flip, we recompute the status of that orbit (by looking at its four cells). If status changed from good to bad, decrement goodOrbitsCount; if from bad to good, increment. After processing all flips in the diff, if goodOrbitsCount == totalOrbits, then this image (the new one) is symmetric. Then we proceed to next diff.
Edge Cases: When flipping a pixel, we need to be careful that the flip might affect more than one orbit? No, because each pixel belongs to exactly one orbit, as defined. So only its orbit status changes.
But what about the other cells in that orbit? They are not directly flipped, but their values remain; we need to check equality of all four after the flip. So we need to be able to retrieve the current values of the four cells of the orbit. We can store the image as a 2D array (size N x N) of booleans (or bytes). Flipping a pixel is O(1). Then to recompute the orbit status, we need to read the values of the four cells. That's O(1) per flip. So total time: initial O(N^2) to build image and compute initial goodOrbitsCount. Then for each diff (C-1 up to 9999), each flip (D up to 100) we do O(1) work: flip pixel, update orbit status. So total operations: O(N^2 + total_flips) where total_flips sum over diffs ≤ (C-1)*100 ≤ 1e6. That's fine.
Thus algorithm:
Read C, N.
Read initial image lines as strings. Store in a 0-indexed or 1-indexed 2D array (maybe int grid[N][N]).
Compute totalOrbits = (N/2)*(N/2).
goodCount = 0.
For each orbit (i from 0 to N/2-1? easier with 1-index): Actually we can loop over r = 1..N/2, c = 1..N/2. For each, compute the four coordinates:
a = (r, c)
b = (r, N+1-c)
c1 = (N+1-r, c) (but avoid variable naming)
d = (N+1-r, N+1-c)
Check if grid values at these four positions are equal. If yes, increment goodCount.
Also, we may want to store something like orbitStatus[r][c] = boolean good? But we can compute on the fly when updating; but to know before flip whether orbit was good, we need to know its current status. We could store a boolean array goodOrbit[N/2][N/2] to avoid recomputing from scratch each time? But we can also recompute before flip to get current status, but we need to know before flipping whether it was good to adjust goodCount correctly. However we can:
Given flip at (i,j), compute orbit (oi,oj).
Determine current status of that orbit by checking four values (without flipping yet). Let's call oldGood = (all four equal).
Flip the pixel.
Determine new status of orbit: newGood = (all four equal now).
If oldGood != newGood: adjust goodCount: if newGood true => goodCount++, else goodCount--.
But careful: The flip changes one pixel, so the values of the four cells after flip might not be equal even if they were equal before? Actually if oldGood true, all four were equal. Flipping one of them will make that one different from the others, so newGood will be false (unless all four were equal and flipping one makes it equal? If all four equal, say all 0, flipping one to 1 => three 0, one 1 => not all equal => newGood false). So from good to bad: decrement. If oldGood false, maybe after flip they become equal? Possibly, if three were equal and the fourth was different, flipping the different one to match could make all four equal. Or other patterns. So we adjust accordingly.
Thus we don't need to store orbit status separately, we can compute old status before flip and new status after. However, we must be careful that if two flips in the same diff affect the same orbit, the order matters? Because after first flip, the orbit status changes, and second flip again modifies the same orbit. Our method processes flips sequentially, which is correct because the diff is applied simultaneously? Wait, the diff describes the set of pixels that change from previous image to the next image. The changes are applied simultaneously: the next image is the previous image with those positions toggled. But if we apply them sequentially by flipping one by one, we will get the same final image as long as we toggle each position exactly once, regardless of order, because toggling is commutative (XOR). However, the intermediate orbit status during processing is not the actual status of the previous or next image, but we only care about final status after all flips. Also, adjusting goodCount incrementally using sequential flips might yield wrong intermediate updates if two flips are in the same orbit, because after first flip we update goodCount based on old->new (from previous image's state to state after one flip). Then second flip we compute old status (which is now after first flip) and new status after second flip, and adjust goodCount accordingly. The net effect should be equivalent to directly computing the status after all flips from the original state? Let's verify: Suppose we have an orbit with values v1,v2,v3,v4. We apply toggles at certain positions. Toggling multiple times is like XOR. The final values are v_i ^ t_i where t_i is 1 if position toggled odd number of times. The diff guarantees each position appears at most once, so t_i is either 0 or 1. Sequential updates: start with old status S0 (based on original values). After first toggle at position p, we compute new status S1. Then second toggle at q (maybe same orbit) we compute S2. This yields S2 correctly. And goodCount updates accumulate correctly. So it's fine.
Thus we can process flips sequentially.
However, we must ensure that when we compute old status before first flip, we need the current image state (which is the previous image). That's correct. For subsequent flips within same diff, we use updated state. So sequential works.
Now after processing all flips for a diff, we have the new image state and goodCount representing number of good orbits in this new image. Then if goodCount == totalOrbits, this coaster is symmetric -> increment answer.
We also need to count the first image: after initializing, if goodCount == totalOrbits, answer++.
Then for each diff, after applying flips, check and maybe increment.
Complexities: O(N^2) memory for grid (1e6 booleans, fine). O(N^2) time for init, plus O(total flips) (<= 1e6) time.
One nuance: N up to 1000, even. So N/2 up to 500. totalOrbits up to 250,000. That's fine.
Implementation details:
Use 0-based indexing for easier array handling. But mapping formulas need adjust. Let's use 0-index: rows 0..N-1, columns 0..N-1. Then vertical symmetry condition: grid[i][j] == grid[i][N-1-j]. Horizontal: grid[i][j] == grid[N-1-i][j]. The orbits are groups: (i,j), (i, N-1-j), (N-1-i, j), (N-1-i, N-1-j). For i in [0, N/2 - 1], j in [0, N/2 - 1] because N even, so N/2 is integer. Then these four are distinct and cover all cells. Since N is even, the indices for pairs: for any i, its mirror is N-1-i. When i < N/2, i is in first half, and N-1-i is in second half. So orbit ID: (i, j) where i in 0..N/2-1, j in 0..N/2-1.
For a given cell (r,c) (0-index), its orbit ID: i = min(r, N-1-r); j = min(c, N-1-c). But note that min(r, N-1-r) will range from 0 to N/2-1. So we can compute oi = r < N/2 ? r : N-1-r; similarly for c. But since N even, we can also compute: if r < N/2 then oi=r else oi=N-1-r. Similarly for c.
For computing the four values quickly, we can define a function to check if orbit (oi,oj) is good: get values at (oi,oj), (oi, N-1-oj), (N-1-oi, oj), (N-1-oi, N-1-oj). Wait careful: The orbit ID (oi,oj) is the representative (the one with smallest row and smallest col indices). The four coordinates are exactly: (oi, oj), (oi, N-1-oj), (N-1-oi, oj), (N-1-oi, N-1-oj). Because oi <= N-1-oi (since oi <= N/2-1) and similarly oj <= N-1-oj. So these four are correct.
But we must ensure that (oi,oj) indeed corresponds to the orbit we think: For any cell (r,c), we compute (oi,oj) as described. Then the four positions of that orbit are those four. So to compute old status for a flip at (r,c), we can compute (oi,oj) and then check the four positions.
Implementation steps:
Read C and N.
Allocate grid = vector<vector>(N, vector(N)).
For i=0..N-1: read string line; for j=0..N-1: grid[i][j] = (line[j] == '1').
totalOrbits = (N/2)*(N/2).
goodCount = 0.
For i in 0..N/2-1:
for j in 0..N/2-1:
bool v1 = grid[i][j];
bool v2 = grid[i][N-1-j];
bool v3 = grid[N-1-i][j];
bool v4 = grid[N-1-i][N-1-j];
if (v1 == v2 && v2 == v3 && v3 == v4) // all equal
goodCount++;
// optionally, we might store orbit status if needed, but not required.
ans = 0; if goodCount == totalOrbits: ans = 1 else ans = 0.
For each diff from 1 to C-1:
Read D.
For k=1..D: read r, c (1-indexed). Convert to 0-index: r-1, c-1.
For each flip:
// compute orbit id
int i = r0, j = c0;
int oi = (i < N/2) ? i : (N-1-i);
int oj = (j < N/2) ? j : (N-1-j);
// compute old status
bool v1 = grid[oi][oj];
bool v2 = grid[oi][N-1-oj];
bool v3 = grid[N-1-oi][oj];
bool v4 = grid[N-1-oi][N-1-oj];
bool oldGood = (v1 == v2 && v2 == v3 && v3 == v4);
// flip the pixel at (i,j)
grid[i][j] = !grid[i][j];
// compute new status
v1 = grid[oi][oj]; // but careful: flipping might have changed one of these four. So we need to recompute all four again after flip.
// Actually we can recompute the four values. But note: the four positions include (i,j) potentially, but we just flipped (i,j). However we can simply recompute the same four positions.
v2 = grid[oi][N-1-oj];
v3 = grid[N-1-oi][oj];
v4 = grid[N-1-oi][N-1-oj];
bool newGood = (v1 == v2 && v2 == v3 && v3 == v4);
if (oldGood != newGood) {
if (newGood) goodCount++; else goodCount--;
}
After processing all flips for this diff, if goodCount == totalOrbits: ans++.
Output ans.
But careful: When recomputing v1 after flip, note that the representative cell (oi,oj) might be the flipped cell if the flipped cell is that one. But we correctly get updated value because we read from grid. So okay.
Edge Cases: If D=0, we just check goodCount after no changes (which remains same as previous image) and possibly increment ans if it is symmetric.
Now check with example 1 to see if our method yields 3. We already computed manually: initial goodCount for M1? Let's compute quickly: M1 not both symmetric. So goodCount < totalOrbits. After diff1, we get M2 (both symmetric) so goodCount should be totalOrbits. After diff2 -> M3 not; diff3 -> M4 not; diff4 -> M5 yes; diff5 (0) -> M6 yes; diff6 -> M7 not. So ans counts M2, M5, M6 = 3. Works.
Now we need to ensure that our incremental method correctly tracks goodCount for M4: M3 goodCount not full; after diff3, we applied three flips; after processing them, goodCount should be not full. Then after diff4 (two flips) we get full. Our method should handle multiple flips per diff correctly.
Now let's test with a small case: N=2, both images all zeros then all ones. For N=2, orbits: (0,0) with group: (0,0),(0,1),(1,0),(1,1). So totalOrbits = 1. Initial image all zeros: all four equal -> goodCount=1 -> ans=1. Diff flips all four: we apply four flips sequentially. After each flip, we update goodCount. Let's simulate: start goodCount=1. Flip (1,1) -> oldGood true? Check orbit: values all 0 -> oldGood true. Flip (1,1) -> becomes 1; after flip values: (1,0,0,0) not all equal -> newGood false -> goodCount-- => 0. Next flip (1,2): oldGood false? Compute orbit values: currently [1,0,0,0] not all equal -> oldGood false. After flipping (1,2) (which is (0,1) in 0-index) from 0 to 1: new values [1,1,0,0] -> still not all equal -> newGood false, no change. Next (2,1): flip (1,0) from 0 to 1: values [1,1,1,0] -> still not all equal, no change. Next (2,2): flip (1,1) from 0 to 1: values [1,1,1,1] -> all equal -> newGood true, so goodCount++ => 1. After all flips, goodCount=1. So we correctly identify final image symmetric. So ans++ -> total ans=2. Works.
Now complexity: N up to 1000, grid size 1e6, storing as bool or bits. But bool vector<vector> may be space efficient but could be slow due to bit packing? But 1e6 booleans is fine, 1 MB if bits, but typical vector is bitset; but we might want to use vector<vector> for simplicity and speed. Since we need to flip frequently and read values, using char (0/1) is okay, memory 1e6 bytes ~1 MB, fine. We'll use vector<vector> or vector? But we need to flip individual characters; string might be mutable? In C++, string is mutable, but careful with indexing. I'll use vector<vector> or vector<vector> as vector of vector for clarity.
Also, we need to read many lines; use fast I/O.
Potential pitfalls: When computing old status, we compute the four values from grid. But note: The four positions include the pixel being flipped. If we compute old status before flipping, we get correct old state. Then after flipping, we recompute. However, if there are multiple flips within the same orbit in the same diff, our method works because after first flip, the orbit state is updated, and second flip's old status is computed from the current grid (post first flip). That's correct.
But is there any scenario where the flip at a pixel also affects the orbit status computation because we use the representative (oi,oj) and the four positions are fixed? That's fine.
Now ensure that we correctly compute totalOrbits as (N/2)*(N/2). For N even, it's integer.
Now about input reading: After the first image, we read C-1 diffs. Each diff starts with D (an integer). Then D lines of coordinates. D can be 0, then we just read the integer and no lines. So we need to loop accordingly.
Edge Cases: D can be up to 100, but coordinates are 1-indexed. We'll convert.
Potential optimization: Not needed.
Now double-check with example 2: N=6, initial image all zeros? Actually image:
000000
000000
010010
010010
000000
000000
We need to compute initial goodCount. N=6, orbits: (i=0..2, j=0..2) total 9 orbits. Check each:
Indices: 0-indexed rows 0-5, cols 0-5.
We'll quickly test if all orbits have four equal? For (i=0,j=0): cells (0,0),(0,5),(5,0),(5,5) all 0 -> equal.
(i=0,j=1): (0,1),(0,4),(5,1),(5,4): row0 col1=0? row0 all 0? Actually row0 all 0, row5 all 0 -> all 0 equal.
(i=0,j=2): (0,2),(0,3),(5,2),(5,3): all 0 -> equal.
(i=1,j=0): (1,0),(1,5),(4,0),(4,5): rows1 and4: row1 all 0? row1 is all 0? Actually row2? Wait row indices: 0:000000; 1:000000; 2:010010; 3:010010; 4:000000;5:000000. So row1=0's, row4=0's -> all 0 equal.
(i=1,j=1): (1,1),(1,4),(4,1),(4,4): all 0.
(i=1,j=2): (1,2),(1,3),(4,2),(4,3): all 0.
(i=2,j=0): (2,0),(2,5),(3,0),(3,5): row2 col0=0, row2 col5=0? row2=010010 => col5? Actually row2: indices: 0:0,1:1?, Let's write row2 (index2): "010010" -> positions:0:0,1:1,2:0,3:0,4:1,5:0? Wait "010010": length 6: digits: 0,1,0,0,1,0. So col5=0. row3: "010010" same. So (2,0)=0, (2,5)=0, (3,0)=0, (3,5)=0 -> all 0 equal.
(i=2,j=1): (2,1)=1, (2,4)=1, (3,1)=1, (3,4)=1 -> all 1? Actually (2,4)= row2 col4 = 1? Yes row2 col4=1, (3,4)=1. So all 1 -> equal.
(i=2,j=2): (2,2)=0, (2,3)=0, (3,2)=0, (3,3)=0 -> all 0 equal.
So all 9 orbits are good, goodCount=9, totalOrbits=9 => symmetric -> ans=1. After no further diffs (C=1) -> output 1. Works.
Now example 3: N=2, first all zeros -> goodCount=1 -> ans=1. Then diff with 4 flips -> after all flips goodCount=1 -> ans++ -> output 2. Works.
Thus algorithm is sound.
Now we need to ensure we correctly handle large C up to 10000 and many flips. Implementation straightforward.
Potential memory: grid as vector<vector> of size N x N. We'll use char for 0/1.
Now consider edge case: N=1000, C=10000, total flips up to 1e6. This is fine.
Now we must ensure we read input correctly, especially with large N and many diffs. Use scanf for speed? In C++, using cin with sync off is okay. We'll read strings for rows.
Now we must consider that the diff may contain coordinates that are not in any particular order; but we process sequentially anyway.
Now what about the possibility that the same orbit can be affected by multiple flips? Already handled.
Now confirm the mapping for orbit ID: For given (r,c) 0-index, we compute:
int oi = r;
if (oi >= N/2) oi = N-1 - r; // because N even, N/2 is integer dividing line. Actually if r >= N/2, then the mirror row index is N-1-r which is < N/2. So oi = min(r, N-1-r).
Similarly oj = min(c, N-1-c).
Alternatively, we can compute: oi = (r < N/2) ? r : N-1-r; // works.
But careful: For N even, N/2 is exactly half. Rows 0..N/2-1 are first half, rows N/2..N-1 are second half. For r = N/2, it is exactly the first row of the second half? Actually N even, say N=6, rows: 0,1,2 (first half) and 3,4,5 (second half). So r=3 => N-1-r = 2, min = 2. So oi becomes 2. That's fine. So mapping works for all rows.
Now for orbit checking, we need to get the four positions:
a = (oi, oj)
b = (oi, N-1-oj)
c = (N-1-oi, oj)
d = (N-1-oi, N-1-oj)
But note: These are exactly the orbit positions. However, be careful: (oi, oj) is always the top-left of the four? Because oi <= N-1-oi and oj <= N-1-oj. So it's the representative.
Now when we flip a pixel at (r,c), we compute (oi,oj) as above, then compute the four positions for the orbit. But what if the flip is at a pixel that is not the representative? That's fine; the orbit ID is computed from (r,c) and it gives the same (oi,oj) as for any cell in the orbit. So we correctly identify the orbit.
Now update goodCount: We compute oldGood by checking four cells. That requires reading grid values; after flip we recompute.
Potential race condition: If we have multiple flips within the same orbit in the same diff, and we compute oldGood before each flip, we might double-count changes? Actually the method correctly updates. Let's test scenario: orbit initially all equal (good). Two flips within same orbit: first flip makes it bad (goodCount--), second flip might make it good again (goodCount++). Net effect: goodCount unchanged if final state good. That's correct.
Now we must be careful about reading input: after the first N lines, we read C-1 diffs. Each diff: first integer D, then D lines each with two integers. However, there might be blank lines? Usually not. So we just read sequentially.
Implementation details:
Pseudo-code:
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
code
int C, N;
cin >> C >> N;
vector<string> grid(N);
for (int i = 0; i < N; ++i) {
cin >> grid[i];
// ensure length N
}
int half = N / 2;
int totalOrbits = half * half;
int good = 0;
// Precompute maybe mapping from cell to orbit? Not needed.
for (int i = 0; i < half; ++i) {
int i2 = N-1-i;
for (int j = 0; j < half; ++j) {
int j2 = N-1-j;
char a = grid[i][j];
char b = grid[i][j2];
char c = grid[i2][j];
char d = grid[i2][j2];
if (a == b && b == c && c == d) {
++good;
}
}
}
int ans = (good == totalOrbits) ? 1 : 0;
for (int t = 0; t < C-1; ++t) {
int D;
cin >> D;
for (int k = 0; k < D; ++k) {
int r, c;
cin >> r >> c;
--r; --c; // to 0-index
// compute orbit id
int oi = (r < half) ? r : (N-1-r);
int oj = (c < half) ? c : (N-1-c);
// compute oldGood
int i2 = N-1-oi;
int j2 = N-1-oj;
char a = grid[oi][oj];
char b = grid[oi][j2];
char c_ = grid[i2][oj];
char d = grid[i2][j2];
bool oldGood = (a == b && b == c_ && c_ == d);
// flip
grid[r][c] = (grid[r][c] == '0') ? '1' : '0'; // if stored as char '0'/'1'
// recompute
a = grid[oi][oj];
b = grid[oi][j2];
c_ = grid[i2][oj];
d = grid[i2][j2];
bool newGood = (a == b && b == c_ && c_ == d);
if (oldGood != newGood) {
if (newGood) ++good;
else --good;
}
}
if (good == totalOrbits) ++ans;
}
cout << ans << endl;
return 0;
}
We need to store grid as char '0'/'1' to easily flip. However, checking equality compares chars; fine.
But careful: In initial grid reading, we have strings; grid[i][j] is char '0' or '1'. When we flip, we can do grid[r][c] ^= 1? Since '0' is 48, '1' is 49, toggling by XOR with 1 works: '0' ^ 1 = '1', '1' ^ 1 = '0'. Actually '0' ascii 48 (binary 110000), '1' is 49 (110001), so XOR with 1 toggles the last bit: 48^1=49, 49^1=48. So we can do grid[r][c] ^= 1; That works assuming char is at least 8-bit. Or use if-else. So I'll use grid[r][c] ^= 1; (since '0' and '1' are consecutive). But careful: In C++, char is signed? Still XOR works.
Alternatively, store as int 0/1 and convert input accordingly. Might be simpler to use vector<vector>. But reading strings, we can convert to ints: grid[i][j] = line[j] - '0'. Then flip: grid[r][c] ^= 1. That might be more efficient for comparisons (ints). I'll do that: store as vector<vector> grid(N, vector(N)). During initial reading, for each char, assign 0/1.
Then in orbit check, we compare ints.
Implementation with ints:
grid as vector<vector>.
read line as string s, then for j: grid[i][j] = s[j] - '0'.
flip: grid[r][c] ^= 1 (since 0^1=1,1^1=0).
comparisons: a == b etc.
Thus the code will be simple.
Now test with example.
Edge Cases: D=0: loop for k does nothing, then check good. Good.
Now complexity: O(N^2 + total flips). Good.
Now let's think about possible mistakes: The orbit checking uses the same four positions each time. But note: After flipping, we recompute values from grid. That's fine.
But there is a nuance: When we flip a pixel at (r,c), we compute orbit ID (oi,oj) based on min indices. However, the four positions we then check include (oi,oj), (oi, N-1-oj), (N-1-oi, oj), (N-1-oi, N-1-oj). But the pixel we flipped might be any of these four. So after flip, we correctly update that value because we read from grid.
Now, what about the fact that when we compute oldGood, we use the grid before flipping. That's correct.
Now, is there any chance that the orbit ID computed from (r,c) might be different from the actual set we check? For example, if (r,c) = (0,5) with N=6: r=0, c=5. oi = min(0,5)=0; oj = min(5,0)=0? Wait c=5, N-1-c = 0, so min(5,0)=0. So orbit ID (0,0). That's correct. The four positions are (0,0),(0,5),(5,0),(5,5). The flipped pixel (0,5) is included. Good.
Now consider (r,c) = (3,2) N=6: r=3, N-1-r=2 => oi=2; c=2, N-1-c=3 => min(2,3)=2; orbit (2,2). Four positions: (2,2),(2,3),(3,2),(3,3). Flipped pixel (3,2) is included. Good.
So mapping works.
Now we need to ensure that when we compute oldGood, we do not inadvertently use values that are about to be changed later in the same diff, but that's fine because we process sequentially.
Thus algorithm is correct.
Now consider memory for good orbit status: we don't need to store; just good count.
Now we should think about potential integer overflows? Not.
Now we need to be careful about reading input: There might be whitespace. Using cin >> for ints and strings is fine. The rows are given as strings of length N without spaces. So we can read them using cin >>.
Now we need to be careful that after reading N lines, we read diffs. The diffs lines consist of D then D pairs. There could be extra newlines, but cin handles.
Now test with example 1 using code mentally? Already did.
Now we should also think about initial goodCount computation: For each orbit, we compare four ints. That's O(N^2/4) comparisons, fine.
Now potential optimization: none needed.
Now we need to consider if there is any other symmetry requirement? The problem statement says "vertically symmetrical and symmetrical". But we deduced both axes. Check example 2: image is symmetric about both axes. Example 3: both axes. Example 1: output 3 matches both axes. So conclusion stands.
Thus final solution.
Now I'll write the final answer: Explanation plus code.
But the user asked: "The cloth coasters produced and sold by Aizu Takada City... create a program that reports the number of coasters that are vertically symmetrical and symmetrical." So we need to provide solution.
I think we should output code in C++.
Now we might also discuss alternative approach: maintain differences, but we have solution.
Now produce final answer: Explanation of approach and code.
We'll include comments.
Now let's produce final output. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int C, N;
cin >> C >> N;
// read first image
vector<vector<int>> img(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
string s;
cin >> s;
for (int j = 0; j < N; ++j)
img[i][j] = s[j] - '0';
}
const int half = N / 2;
const int totalOrbits = half * half;
int good = 0;
// initial check: count orbits that are already good
for (int i = 0; i < half; ++i) {
int i2 = N - 1 - i;
for (int j = 0; j < half; ++j) {
int j2 = N - 1 - j;
int a = img[i][j];
int b = img[i][j2];
int c = img[i2][j];
int d = img[i2][j2];
if (a == b && b == c && c == d)
++good;
}
}
int answer = (good == totalOrbits) ? 1 : 0;
// process the remaining C-1 coasters
for (int t = 0; t < C - 1; ++t) {
int D;
cin >> D;
for (int k = 0; k < D; ++k) {
int r, c;
cin >> r >> c;
--r; --c; // to 0‑based
// find the orbit of this pixel
int oi = (r < half) ? r : (N - 1 - r);
int oj = (c < half) ? c : (N - 1 - c);
int i2 = N - 1 - oi;
int j2 = N - 1 - oj;
// current status of the orbit
int a = img[oi][oj];
int b = img[oi][j2];
int c1 = img[i2][oj];
int d = img[i2][j2];
bool oldGood = (a == b && b == c1 && c1 == d);
// flip the pixel
img[r][c] ^= 1;
// new status
a = img[oi][oj];
b = img[oi][j2];
c1 = img[i2][oj];
d = img[i2][j2];
bool newGood = (a == b && b == c1 && c1 == d);
if (oldGood != newGood) {
if (newGood) ++good;
else --good;
}
}
if (good == totalOrbits)
++answer;
}
cout << answer << endl;
return 0;