We need to analyze the problem: given target matrix A (0/1) of size n x m, we can perform operations on B (initially all zeros) that set a 2x2 submatrix to all ones (i.e., set the four cells to 1). Operations can be applied any number of times, and they can overlap. Overwriting a cell multiple times is fine; it will be 1 after first set. We need to determine if there exists a sequence of operations such that final B equals A. And if yes, output any valid sequence (0 ≤ k ≤ 2500). n,m ≤ 50, so total cells ≤2500, and the bound on k is 2500. So we can afford up to O(n*m) operations.
We need to think about constraints: Operations only set cells to 1. So once a cell becomes 1, it stays 1 (no way to set back to 0). Therefore, for B to equal A, we must never set any cell that should be 0 in A to 1. Because if we set it, it becomes 1 permanently; we cannot revert. So the operations must be applied only in such a way that after all operations, exactly those cells that should be 1 are set, and the ones that should be 0 remain 0. Since operations set all four cells in a 2x2 block to 1, if any of those four cells should be 0 in A, we cannot perform that operation because it would set that cell to 1 (making it 1 when it should be 0). However, note that we might have a situation where a cell is 0 in A but could be set by an operation and later changed? No, operations only set to 1, can't unset. So any operation that would cause a cell that should be 0 to become 1 is forbidden. Thus the condition for a possible operation (x, y) is: all of A[x][y], A[x][y+1], A[x+1][y], A[x+1][y+1] must be 1. In other words, the operation can be applied only on 2x2 blocks that are entirely 1's in A.
Now, the question: can we obtain the entire A matrix by applying a set of such operations (each on a 2x2 all-1 block in A), such that all cells that are 1 in A are covered by at least one operation, and no cell that is 0 in A is ever covered? But careful: The process is sequential; we apply operations. But if we apply only operations that correspond to all-1 blocks, we will never set a 0 cell. However, there is a nuance: after we apply some operations, B becomes 1 for some cells. But we don't care about intermediate states as long as final B = A. But we cannot set a 0 cell even temporarily? Actually we could set a 0 cell temporarily if later we could revert it to 0? But there's no operation to revert. So once a 0 cell is set to 1, it remains 1, making final B not equal A. So we must never set any cell that is 0 in A. So indeed each operation must be such that all four cells are 1 in A.
Thus a necessary condition: For each operation we use, the 2x2 block must consist entirely of ones in A. But is it sufficient to apply all such possible operations? Possibly not, because applying all such operations may set many cells to 1, but could it set all ones of A? But if there is a cell that is 1 in A but not covered by any all-1 2x2 block? Then we would not be able to set it to 1 via any operation without also setting a neighboring 0? However, maybe we can set it by overlapping operations that eventually produce a 1 in that cell without setting adjacent 0s? But any operation that sets a cell also sets three other cells. So if a cell is 1 but it cannot be part of any 2x2 block entirely ones (i.e., for every adjacent 2x2 that includes that cell, at least one of the other three is 0), then any operation that would set that cell would also set at least one cell that is 0 in A, which is not allowed. Therefore, such a cell cannot be set without violating. But maybe we could set it using a combination of operations that "fix" the 0? No, can't revert. So any operation that touches a 0 cell is disallowed. So indeed, each operation we use must be a 2x2 all-1 block. Therefore, every cell that is 1 in A must be covered by at least one such operation? Actually not necessarily: if a cell is 1, it could be set by an operation that also sets other cells that are also 1. But if there is a 1 cell that is isolated such that no all-1 2x2 block contains it, then we cannot set it via any allowed operation. So then impossible.
But is there a scenario where we could set a cell without using an operation that exactly covers a 2x2 all-1 block but by using multiple operations that together set only the desired cells? For example, suppose we have a cell at (i,j)=1, but its neighbors are such that any 2x2 block that includes (i,j) contains a 0. Could we set (i,j) by overlapping operations that each set some other cells but eventually (i,j) becomes 1? But any operation that sets (i,j) will set the other three cells of that block to 1. To avoid violating A, those other three cells must also be 1 in A (since they'll become 1). So they must be 1. That means the entire block must be all 1s. So any operation that directly sets (i,j) must be an all-1 block. If there is no such block, we cannot set (i,j) directly. Could we set (i,j) indirectly? Suppose we have operations that set other cells but not (i,j), and then maybe later an operation that sets (i,j) but some of the other three cells were already set to 1 (by previous operations) and the ones that are 0 might be okay? No, the operation will still set those cells to 1 regardless of previous state. So if any of those three cells is 0 in A, it will become 1, causing violation. So the operation is only allowed if all four cells are 1 in A. So again need an all-1 block.
Thus the necessary and sufficient condition for possibility is: For every cell A[i][j]=1, there exists some 2x2 block (with top-left corner at (i-1,j-1), (i-1,j), (i,j-1), or (i,j) that fits within bounds) such that the entire block is all ones. But is that sufficient? If we take the set of all all-1 2x2 blocks and apply all of them, will we get exactly A? Let's test. Applying all operations for which the 2x2 block is all ones will set all those four cells to 1. This may set many 1's. However, could it also set some cell that is 0 in A? By definition, we only apply operations on all-1 blocks, so any cell set by such an operation is part of an all-1 block, thus it is 1 in A. So it will not set any 0 to 1. But what about cells that are 1 in A but are not covered by any all-1 block? According to the condition, there should be none. So then all 1 cells become set. So final B would be A? Possibly yes: every cell that is 1 in A is covered by at least one all-1 block (since the condition says for each 1 cell, there exists some block containing it that is all ones). But wait: It's possible that a 1 cell is covered by an all-1 block but the block includes other cells that are 1 as well. So all 1 cells will be set. And no 0 cell is set because operations only set cells that are within all-1 blocks, which are 1 in A. Therefore final B = A. So condition seems sufficient.
But careful: Could there be a scenario where some cell is 1, but it's only covered by an all-1 block that we might not need to apply because we may apply other blocks to cover it? But if we apply all all-1 blocks, we certainly cover it. However, there might be a catch: Applying all all-1 blocks might produce a superset of A? No, because each block is all ones, so every cell set is 1 in A. So the union of all these blocks is a subset of the set of 1's in A. Actually each block is a subset of the 1's (since all cells in block are 1). The union of these subsets is a subset of the set of 1's. But we need the union to be exactly all 1's. So the necessary condition is that every 1 cell belongs to at least one all-1 2x2 block. That's what we described. So if that holds, then applying all all-1 blocks yields exactly the set of 1's. But could there be a 1 cell that is in the union of all all-1 blocks but still not all 1's? That is the opposite: we need the union to be all 1's. If there is a 1 cell not in any all-1 block, then union is missing it, so final B would have 0 there. So we need coverage.
Thus problem reduces to: Check if every cell with value 1 is part of at least one 2x2 submatrix that consists entirely of 1's. If yes, then output all such submatrices (i.e., all positions (i,j) with 1 ≤ i < n, 1 ≤ j < m, such that A[i][j]==1 && A[i][j+1]==1 && A[i+1][j]==1 && A[i+1][j+1]==1). That set of operations will produce A. The number of operations k would be the number of such 2x2 all-1 blocks, which could be up to (n-1)(m-1) ≤ 4949=2401, which is within 2500. So we can simply output all of them. However, we need to ensure that applying all these operations indeed yields exactly A. But does it? Consider a scenario: A has a 1 cell that is part of an all-1 block, but maybe there is also a 0 cell that is part of some all-1 block? That can't happen because if a cell is 0, then any 2x2 block containing it cannot be all ones, because that cell is 0. So any all-1 block contains only 1's. So indeed only 1 cells are set. So final B = union of all those blocks = all cells that are in at least one all-1 block. So we need to verify that this union equals the set of all 1 cells. That's exactly the coverage condition.
But wait: Consider edge cells like those on the last row or last column? For a cell in last row (row n) or last column (col m), it can only be part of 2x2 blocks where the block includes it. For a cell (n, j) with 1 ≤ j ≤ m, it can be covered by blocks with top-left (n-1, j-1) if j>1, or (n-1, j) if j<m. So the condition must hold for all 1 cells, including those on edges. So we need to check for each cell (i,j) with A[i][j]=1, whether there exists some adjacent 2x2 block (i-1,j-1), (i-1,j), (i,j-1), or (i,j) that is within bounds and all ones.
But is there any subtlety? Let's test with the examples:
Example 1:
3 3
1 1 1
1 1 1
0 1 1
Check cells:
(1,1)=1: can be covered by block (1,1) (top-left). Block (1,1): A[1][1]=1,A[1][2]=1,A[2][1]=1,A[2][2]=1 => all ones. So covered.
(1,2)=1: blocks: (1,1) includes it, (1,2) (top-left at 1,2?) But (1,2) block would involve column 3, which exists: A[1][2]=1,A[1][3]=1,A[2][2]=1,A[2][3]=1 => all ones? A[1][3]=1, A[2][3]=1? Actually A[2][3] is at row2 col3: from matrix: row2: 1 1 1 -> yes 1. So block (1,2) all ones. So covered.
(1,3)=1: blocks: (1,2) includes it, also (1,3) is not valid because m=3 so j< m => (1,3) invalid. So covered.
(2,1)=1: blocks: (1,1) includes it, (2,1) would be (2,1) top-left? But i< n, i=2, n=3 => valid, check block (2,1): cells (2,1)=1,(2,2)=1,(3,1)=0,(3,2)=1 => not all ones because (3,1)=0. So (2,1) is covered by (1,1) already. Good.
(2,2)=1: blocks: (1,1), (1,2), (2,1), (2,2). Check which are all ones: (1,1) all ones; (1,2) all ones; (2,1) not all ones because (3,1)=0; (2,2) cells: (2,2)=1,(2,3)=1,(3,2)=1,(3,3)=1 => all ones? (3,2)=1, (3,3)=1 => yes. So covered.
(2,3)=1: blocks: (1,2) includes it, (2,2) includes it, both all ones. Covered.
(3,1)=0: ignore.
(3,2)=1: blocks: (2,1) not all ones, (2,2) includes it => all ones. So covered.
(3,3)=1: blocks: (2,2) includes it, (2,3) invalid, so covered.
So all 1 cells are covered. According to condition, possible. Output all all-1 blocks: Let's list all top-left positions where the 2x2 is all ones:
Check (1,1): yes.
(1,2): yes.
(2,1): no (due to (3,1)=0).
(2,2): yes.
Thus three operations: (1,1),(1,2),(2,2). That's exactly the sample output (they had 3 operations: 1 1, 1 2, 2 2). So matches.
Example 2:
3 3
1 0 1
1 0 1
0 0 0
Let's check 1 cells: (1,1)=1: block (1,1) includes (1,1). Check block (1,1): cells (1,1)=1, (1,2)=0 => not all ones. So not covered. Are there other blocks covering (1,1)? Possibly (1,0) invalid, (0,1) invalid, (0,0) invalid. So no block. So impossible. Indeed output -1.
Example 3: all zeros. There are no 1 cells, so condition vacuously true (since there are no 1 cells to cover). Applying all all-1 blocks? But there are no all-1 blocks because all are zeros. So we output 0 operations. This matches.
Thus the solution seems straightforward: For each cell (i,j) that is 1, we need to check if it belongs to some all-1 2x2 block. But we can also compute coverage by checking all possible 2x2 blocks and marking cells covered. Then compare with A's ones. However, be careful: For a cell to be covered, it's enough that there is at least one all-1 block containing it. But could there be a scenario where after applying all all-1 blocks, some 1 cell gets set? Yes, because it's covered. So we just need to ensure that all 1 cells are covered by at least one all-1 block.
But wait, what about a 1 cell that is part of some all-1 block, but that all-1 block includes a cell that is 0? That can't happen because then it's not all-1 block. So all-1 blocks are defined as those where all four are 1. So if a 1 cell is covered by an all-1 block, that block's other cells are also 1, so they are also covered. That's fine.
Thus algorithm:
- Read n,m and A.
- Initialize a boolean matrix 'covered' of size n x m all false.
- Iterate over all possible top-left positions (i,j) where 1 <= i <= n-1, 1 <= j <= m-1 (using 1-indexed). If A[i][j]==1 && A[i][j+1]==1 && A[i+1][j]==1 && A[i+1][j+1]==1, then mark these four cells as covered (set covered true). Also store the operation (i,j) in a list.
- After scanning, check for every cell (i,j) where A[i][j]==1, if covered[i][j] is true. If any such cell is not covered, output -1.
- Otherwise, output k = number of operations in list, then each operation line.
But is it possible that even if all 1 cells are covered, applying all all-1 blocks might also set some cell to 1 that should be 0? As argued, no, because each operation sets only cells that are in that block, and since the block is all ones, those cells are 1 in A. So they are supposed to be 1. So final B will have 1 exactly at cells that are in at least one all-1 block. But we must ensure that these are exactly all 1 cells. The condition that every 1 cell is covered ensures that all 1 cells are set. But is it possible that some cell that is 1 in A is not covered? That's the condition we check. So final B will have all 1 cells set. However, could there be a cell that is 1 in A but is covered by an all-1 block? That's what we ensure. So final B equals A. So it's correct.
But wait: Are there cases where a cell is 1 and is covered, but after applying operations, some 0 cell might accidentally become 1 due to overlapping? No, because all operations only touch cells that are part of all-1 blocks, which are all 1's. So no 0 cell is touched. So final B is subset of 1's. But we need final B = A, meaning also that there is no extra 1 that is not in A. Since we only set cells that are 1 in A, that holds. So okay.
However, consider a scenario: A has a 1 cell that is covered by an all-1 block, but also there exists another all-1 block that covers a cell that is 0? Impossible because the other block would contain a 0, so it wouldn't be all-1.
Thus solution is straightforward.
But wait: There might be a nuance: The problem statement says we can perform operations "choose any submatrix of B having size 2x2, and replace every element in the chosen submatrix with 1." There's no restriction that we cannot choose a submatrix that includes cells that are already 1. So we can apply them in any order. So our approach of applying all all-1 blocks works.
One more nuance: The bound k ≤ 2500. The number of all-1 blocks could be up to (n-1)(m-1) = up to 4949=2401, which is within 2500. So we can output all.
But what about the possibility that we might need to apply some all-1 blocks more than once? Not needed; once is enough.
Thus solution appears simple.
But let's double-check edge cases:
-
n=2, m=2: then only one possible block (1,1). If both cells are 1, then block all ones -> we can apply operation and get A. If any cell is 0, then block is not all ones; need to check coverage: if A[1][1]=1, it's only covered by block (1,1) which is not all-ones (since there is a 0), so not covered => impossible. That matches: for a 2x2 matrix, if any cell is 0, impossible because any operation would set all four to 1, which would make that 0 become 1. But could we do zero operations? If A is all zeros, we can do 0 operations and B stays zeros, so possible. If A is something like [[1,0],[0,0]]: The 1 at (1,1) can only be set via operation (1,1) which also sets the other three to 1, causing mismatch. So impossible, correct.
-
n=2, m=3: possible blocks: (1,1) and (1,2). For each cell that is 1, we need to see if it's in at least one all-1 block. For example, A = [[1,1,0],[1,1,0]]. Then blocks: (1,1) all ones? Check: (1,1)=1,(1,2)=1,(2,1)=1,(2,2)=1 => yes. (1,2): (1,2)=1,(1,3)=0,(2,2)=1,(2,3)=0 => not all ones. So only block (1,1) applied. Covered cells: (1,1),(1,2),(2,1),(2,2). That's all 1's? A has 1's at (1,1),(1,2),(2,1),(2,2) -> yes all covered. So output (1,1). Is that correct? Let's see: After operation (1,1), B becomes [[1,1,0],[1,1,0]] which matches A. So possible.
Now consider A = [[1,0,1],[1,0,1]]: 1's at (1,1),(1,3),(2,1),(2,3). For (1,1): block (1,1) would need (1,2)=1 but it's 0 so not all ones. So not covered. (1,3): blocks that contain it: (1,2) only (since top-left at (1,2) block covers (1,2),(1,3),(2,2),(2,3)). But (2,2) is 0, so not all ones. So not covered. So impossible. Indeed we cannot set those corner 1's without setting the zeros. So -1.
Now consider a trickier case: A 3x3 all ones except center 0? e.g.,
1 1 1
1 0 1
1 1 1
Check coverage: Cell (2,2)=0, ignore. But check each 1 cell: (1,1) is covered by block (1,1)? That block includes (1,1),(1,2),(2,1),(2,2) but (2,2)=0 -> not all ones. (1,1) also possibly covered by other blocks? Blocks that include (1,1): only (1,1). So not covered. So impossible. Indeed, to set (1,1) you'd need block (1,1), but that would set (2,2) to 1, causing error. So impossible. Good.
Now consider scenario where some 1 cell is on the edge and might be covered by a block that extends beyond? No, blocks are within bounds only (i < n, j < m). So for last row, only blocks with i = n-1 can cover it. So we need to check.
Thus solution: collect all all-1 blocks, mark covered cells, verify coverage, output list.
But is the coverage condition truly sufficient? Let's think deeper: Suppose all 1 cells are covered by some all-1 block. Then applying all such blocks yields a B that has 1 at every cell that belongs to any all-1 block. But is it possible that a cell belongs to an all-1 block but is 0 in A? No, because an all-1 block by definition consists of cells that are 1 in A. So it cannot happen. So B's ones are exactly the union of those blocks. Since each 1 cell is in at least one block, union includes all 1 cells. So B = A.
So algorithm is correct.
Now we should also consider that the number of operations is limited to 2500, which holds. There's no requirement to minimize k, so we can output all valid blocks.
But wait: There is an edge case: Suppose there are no 1 cells, but there exists some all-1 block? That's impossible because all-1 block requires all four cells to be 1, so there would be 1 cells. So if A is all zeros, there are no all-1 blocks, so we output 0 operations. Good.
Thus implement.
But is there any subtlety about zero-indexing? The problem uses 1-indexed coordinates for operations: x and y (1 ≤ x < n, 1 ≤ y < m). So we should output coordinates as 1-indexed. Input likely uses 1-indexed rows and columns? Typically they specify rows from 1 to n, columns from 1 to m. In description: "choose two integers x and y such that 1 ≤ x < n and 1 ≤ y < m, and then set B_{x, y}, B_{x, y+1}, B_{x+1, y}, B_{x+1, y+1} to 1." So output accordingly.
Implementation details:
- Use 0-indexing for internal arrays but convert to 1-indexed when outputting. Or just keep 1-indexed arrays. We'll read n,m. Then read A as int matrix size [n+1][m+1] (1-indexed). Then iterate i from 1 to n-1, j from 1 to m-1. Check if A[i][j]==1 && A[i][j+1]==1 && A[i+1][j]==1 && A[i+1][j+1]==1. If yes, add (i,j) to list and mark covered[i][j] etc. But we can also compute coverage later without marking? But we need to check that every 1 cell is covered. So we need a boolean covered array initially false. For each valid block, set covered for those four cells to true.
After scanning all blocks, iterate over all cells (i,j): if A[i][j]==1 and not covered[i][j] then print -1 and exit. Else print list.
Time complexity O(n*m). Memory trivial.
Now let's test some scenarios manually to confirm.
Case: n=3,m=3, A:
1 1 1
1 1 0
1 1 1? Actually let's construct:
Row1: 1 1 1
Row2: 1 1 0
Row3: 1 1 1
Check 1 cells: (2,3)=0, (3,3)=1. For (3,3): blocks that include (3,3): top-left (2,2) block covers rows 2-3, cols2-3: (2,2)=1, (2,3)=0, (3,2)=1, (3,3)=1 -> not all ones. So (3,3) not covered. So impossible. Indeed, to set (3,3) need block (2,2) which would set (2,3) to 1, causing error. So -1.
Case: n=3,m=3, A:
1 1 0
1 1 1
0 1 1? Let's check coverage:
Rows:
1: 1 1 0
2: 1 1 1
3: 0 1 1
Check (1,1): block (1,1) includes (1,1),(1,2),(2,1),(2,2) all ones? (1,1)=1,(1,2)=1,(2,1)=1,(2,2)=1 -> yes, covered.
(1,2): covered by (1,1) or (1,2)? (1,2) block: (1,2)=1,(1,3)=0,(2,2)=1,(2,3)=1 => not all ones. But (1,2) is covered by (1,1) anyway.
(2,1): covered by (1,1).
(2,2): covered.
(2,3): check blocks: (1,2) not all ones; (2,2) block: (2,2)=1,(2,3)=1,(3,2)=1,(3,3)=1 -> all ones? (3,2)=1,(3,3)=1 => yes, so covered.
(3,2): covered by (2,2).
(3,3): covered by (2,2).
(1,3)=0, (3,1)=0 ignored.
Thus all ones covered. So apply all all-1 blocks: (1,1) and (2,2). That yields? (1,1) sets (1,1),(1,2),(2,1),(2,2) to 1. (2,2) sets (2,2),(2,3),(3,2),(3,3) to 1. Combined: (1,1)=1,(1,2)=1,(2,1)=1,(2,2)=1,(2,3)=1,(3,2)=1,(3,3)=1. That's exactly A. So works.
Thus algorithm seems fine.
But wait: Could there be a scenario where a 1 cell is covered by an all-1 block, but the block includes a cell that is 0? That's impossible by definition. So all good.
Now let's consider if the problem might have a twist: The operations are performed sequentially on B, and we could potentially use an operation that is not an all-1 block initially, because after previous operations some cells that were 0 in A may have been set to 1 (but we don't want that). However, we cannot set them because final B must equal A, so any cell that is 0 in A must remain 0 throughout. So any operation that sets a 0 cell is prohibited, regardless of intermediate states. So any operation we perform must have all four cells 0? No, we must ensure that after performing it, no cell that should be 0 becomes 1. Since operations only set to 1, it's safe if a cell that is already 1 (by previous operations) is set again. But if a cell is 0 in final A, it must never be set. So at the time of performing an operation, if that cell is currently 0 (and should be 0), setting it to 1 would be final error. So we cannot ever set a cell that should be 0. Therefore, each operation's four cells must all be cells that are 1 in A. But could we set a cell that is 1 in A but is currently 0 (not yet set) via an operation that also sets a cell that is 0 in A but we plan to later turn it back to 0? Not possible. So indeed each operation's block must consist entirely of cells that are 1 in A. So necessary.
Thus our approach is correct.
But wait: Might there be a scenario where we can use operations that are not all-1 blocks at the time of application but because some cells are already set, the block includes cells that are 1 in A but also some cells that are 0 in A but are already set to 1 from previous operations? That would be wrong because those cells should be 0, so they would be incorrectly set. So still not allowed.
Thus the condition is solid.
Therefore the solution: find all top-left positions (i,j) such that the 2x2 submatrix of A at (i,j) has all ones. For each such operation, record it. Then check if every 1 in A is covered by at least one such operation. If yes, output all recorded operations. If not, output -1.
Now we need to be careful about index bounds: i from 1 to n-1, j from 1 to m-1.
Potential pitfalls: The input numbers are separated by spaces; we can read them as ints.
Complexities: n,m up to 50, so at most 2401 operations, which is ≤2500. So fine.
Edge case: If there is at least one 1 cell, but no all-1 block exists, then impossible. For example, a single 1 in a corner with n,m>2? Suppose n=3,m=3, with only A[1][1]=1, others 0. Then the cell (1,1) is only in block (1,1) which would have other cells 0, so not all ones. So not covered. So impossible. That matches intuition: we cannot set a single cell without setting its neighbors.
But what about a 1 in interior but with no all-ones block? E.g., a solitary 1 surrounded by zeros. Then any block containing it includes zeros, so impossible.
Thus the condition essentially says that the set of ones must be a union of 2x2 squares (overlap allowed). That is a known property. It also implies that any 1 must have at least one neighboring 1 in each direction? Actually for a cell to be covered, it needs a 2x2 block of ones that includes it. For a cell not on the bottom/right edges, it could be covered as top-left of a block, meaning its right, bottom, and bottom-right neighbors must be 1. Or as top-right (requires left, bottom-left, bottom), etc. So the condition imposes constraints on the pattern of ones. For example, a 1 at (i,j) could be covered by:
- As top-left: require (i,j+1), (i+1,j), (i+1,j+1) = 1.
- As top-right: require (i, j-1), (i+1, j-1), (i+1, j) = 1 (i.e., block at (i, j-1) all ones)
- As bottom-left: require (i-1, j), (i-1, j+1), (i, j+1) = 1 (block at (i-1, j))
- As bottom-right: require (i-1, j-1), (i-1, j), (i, j-1) = 1 (block at (i-1, j-1))
So at least one of these four configurations must be fully ones.
Thus we can check by scanning all blocks and marking coverage.
Now, one more nuance: In some puzzles, you could achieve a final configuration even if some 1 cells are not directly covered by an all-ones block, by using a combination of operations that together set them without setting zeros. But as argued, any operation that sets a 1 cell also sets its three neighbors. So if any of those neighbors is 0 in A, then that operation can't be used. However, could we set a 1 cell by multiple operations where each operation sets a subset of the block? For example, suppose we have a cell that is 1, and we want to set it without ever simultaneously setting a particular neighbor that should be 0. But any operation that sets the cell also sets the neighbor. So if we perform that operation, that neighbor becomes 1. Unless we could later "unset" it, which we can't. So no.
Thus condition is necessary and sufficient.
Let's try to formalize with set covering. Since operations are monotonic (only turn 0 to 1, never back), the set of cells turned on is exactly the union of the chosen 2x2 squares. So B final is the union of chosen squares. We need B = A. So A must be exactly the union of some set of 2x2 squares each contained in A (since if a square is not contained in A, then it contains a 0 cell, which would appear in B, making B not equal A). Therefore each chosen square must be a subset of A (i.e., all its cells are 1). Conversely, if we take all such squares (the set of all 2x2 squares that are subsets of A), their union is some set U ⊆ A. We need U = A. That's exactly the condition. And we can choose exactly those squares, which yields U = A. So always possible if and only if U = A.
Thus algorithm: find all 2x2 squares that are all 1's, output them. If any cell of A that is 1 is not in any such square, then impossible.
Implementation: we need to be careful about indexing: i from 1 to n-1, j from 1 to m-1. For each, if all four cells are 1, then add to answer list.
Then check: For each cell (i,j) with A[i][j] = 1, we need to see if it is covered by at least one such square. We could either compute a boolean covered array as described, or we can check for each 1 cell whether there exists a square covering it. Since n,m small, either fine. Simpler: while adding squares, mark covered true for those four cells. Then after iterating all squares, check all cells. If any cell with A[i][j] = 1 and not covered, output -1. Else output answers.
But note: The coverage condition should be checked after marking all squares. However, is it possible that a 1 cell is covered by a square that we didn't include because we only included squares that are all 1? That's the same: all squares we include are exactly all squares that are all 1. So if a 1 cell is covered by some all-1 square, then we will include that square and mark it covered. So our covered array will reflect coverage by all possible all-1 squares. So checking after marking is sufficient.
Thus solution is robust.
Now we need to verify the maximum number of operations: (n-1)(m-1) ≤ 4949 = 2401. That's ≤ 2500, so we can output all. But is there any chance that we might need to output more than 2500? Not with our method, because we output at most that many. The problem says 0 ≤ k ≤ 2500 should hold. So it's safe.
Now confirm with given examples:
Example1: as described, we would find blocks (1,1),(1,2),(2,2) and output them.
Example2: find any all-1 blocks? Check all: (1,1) fails because (1,2)=0, (1,2) fails because (1,3)=1? Actually (1,2): cells (1,2)=0, (1,3)=1, (2,2)=0, (2,3)=1 => fails. (2,1): (2,1)=1,(2,2)=0,... fails. (2,2): (2,2)=0,... fails. So none. Mark covered all false. Then check 1 cells: many 1's exist, none covered. So print -1.
Example3: all zeros, so no blocks, covered all false, but also no 1 cells, so condition holds, output 0.
Thus matches.
Now, there is a nuance: Could it be that some 1 cell is covered but we don't need to output all squares? We output all squares. That might set some cells that are 1 multiple times but that's okay. No extra cells.
Now, is there a scenario where we output all squares, but the union is a proper subset of A? That would happen if some 1 cell is not covered by any all-1 square. Then we would detect that and output -1. So okay.
Thus the solution is simple.
But before finalizing, let's test some more corner cases to ensure coverage detection works for edges.
Case: n=2, m=3, A = [[1,1,1],[1,1,1]] (all ones). All 1 cells. Blocks: (1,1) all ones? Yes. (1,2) all ones? (1,2)=1,(1,3)=1,(2,2)=1,(2,3)=1 => yes. So operations: (1,1) and (1,2). Covered cells: (1,1),(1,2),(2,1),(2,2) from first; (1,2),(1,3),(2,2),(2,3) from second. Union = all cells. So output 2 operations. Works.
Case: n=2, m=3, A = [[1,0,1],[1,0,1]] we saw fails. So -1.
Case: n=3, m=2 (rotate). Example:
1 1
1 1
0 0
Check cells: (1,1),(1,2),(2,1),(2,2) are ones; (3,1),(3,2) zeros. Blocks: only possible top-left positions: i=1..2? Actually i < n => i max 2, j < m => m=2 so j max 1. So blocks: (1,1) and (2,1). Check (1,1): all ones? includes (1,1),(1,2),(2,1),(2,2) => all ones, yes. (2,1): includes (2,1),(2,2),(3,1),(3,2) => (3,1)=0,(3,2)=0 => not all ones. So only (1,1). Mark covered cells: (1,1),(1,2),(2,1),(2,2). Check 1 cells: all are covered. So output (1,1). After operation, B becomes ones at those four cells, zeros elsewhere, matches A. Good.
Case: n=3,m=2, A = [[1,0],[1,1],[1,0]]? Actually matrix:
1 0
1 1
1 0
Check ones: (1,1), (2,1), (2,2), (3,1). Blocks: (1,1) covers (1,1),(1,2),(2,1),(2,2) but (1,2)=0 so not all ones. (2,1) covers (2,1),(2,2),(3,1),(3,2): (3,2)=0 so not all ones. So no blocks. But we have 1 cells not covered: (1,1) not covered? Could be covered by (1,1) if block all ones, but it's not. So impossible. Indeed, to set (1,1) need block (1,1) which would set (1,2) to 1, which is 0. So impossible.
Case: A has a 1 at (n,m) corner. Check coverage: For (n,m) to be covered, must have block at (n-1,m-1) be all ones. So it requires A[n-1][m-1]=1, A[n-1][m]=1, A[n][m-1]=1, and A[n][m]=1. So corner ones need a 2x2 block covering them.
Now consider a more subtle pattern: Suppose we have a 1 cell that is covered by an all-1 block, but that block contains a 1 that is not covered by any block? Actually if block is all ones, then all four cells are 1. But each of those four cells might be covered by that block itself. So if a block is all ones, then all four cells are covered by that block. So if a cell is in an all-1 block, it is covered. So condition for a 1 cell is: it belongs to at least one all-1 block. So if we have an all-1 block, all four cells are covered. Therefore, the condition essentially requires that the set of 1's can be covered by a collection of 2x2 blocks, but more specifically that every 1 is in at least one all-1 block. This is equivalent to requiring that for every 1 cell, there is a 2x2 all-1 block that includes it.
Now, is it possible that a 1 cell is covered by an all-1 block but that all-1 block is not included in our list because we might not need it? But we will include all all-1 blocks. That yields coverage of all such cells. So condition is sufficient.
Now, think about a scenario where we have overlapping all-1 blocks that together cover all 1 cells, but there might be a 1 cell that is not in any all-1 block but can be covered by the union of partial coverage from multiple blocks? No, because to become 1, the cell must be set by some operation. Each operation sets a 2x2 block. So the cell must be in some operation's block. That block must be applied. But if the cell is in a block that we apply, that block must be all ones? Not necessarily: Could we apply a block that contains a 0 cell if that 0 cell is already set to 1 from previous operations? Wait, but the 0 cell should be 0 in final A. If we apply a block that contains a cell that is 0 in A but we previously set it to 1 (erroneously), then final would have that cell as 1, not 0. So we cannot ever set a 0 cell. But what if a cell that is 0 in A is initially 0, and we never set it? Then if we apply a block that includes that cell, it would set it to 1, making it 1, which is wrong. So we cannot apply any block that contains a 0 cell, regardless of whether that 0 cell is already 1 due to previous operations (it shouldn't be). So any operation we apply must have all four cells being 1 in A. So it must be an all-1 block. So indeed, only all-1 blocks can be used.
Thus condition stands.
So solution is correct.
Implementation details: We'll use Python for simplicity.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
# Convert to 1-indexed maybe: we can just use 0-index but adjust indices for output.
# Let's use 0-index internally for arrays, but operations output 1-indexed.
covered = [[False]*m for _ in range(n)]
ops = []
for i in range(n-1):
for j in range(m-1):
if A[i][j]==1 and A[i][j+1]==1 and A[i+1][j]==1 and A[i+1][j+1]==1:
# mark covered
covered[i][j] = True
covered[i][j+1] = True
covered[i+1][j] = True
covered[i+1][j+1] = True
ops.append((i+1, j+1)) # 1-indexed
# Check all cells
for i in range(n):
for j in range(m):
if A[i][j]==1 and not covered[i][j]:
print(-1)
return
# Output
print(len(ops))
for x,y in ops:
print(x, y)
We need to ensure that we don't double-mark covered, but boolean is fine.
Edge case: Suppose there is a 1 cell that is covered by multiple blocks; that's fine.
Now, is there any scenario where a 1 cell is covered but we still get -1? No.
Let's test with a custom example: n=4,m=4, A is all ones. Then all 2x2 blocks are all ones. Number of ops = (3)*(3)=9, which is <=2500? 9 ≤ 2500. Output 9 ops. Then B becomes all ones. Works.
Now consider n=50,m=50 all ones. Number of ops = 49*49 = 2401. Within 2500. Works.
Now, is there any hidden trap: The problem statement says "0 ≤ k ≤ 2500 should hold." So outputting up to 2401 is fine. But if n=50,m=50 and the matrix is such that there are many all-1 blocks, it's okay. If n=50,m=50 and there are many ones but not all, maximum possible blocks is still (n-1)*(m-1) = 2401. So k will be ≤ 2401.
Now, could there be a scenario where some 1 cell is not covered but we might still achieve A by not using all all-1 blocks? Wait, we argued that any operation must be an all-1 block. So the union of all operations we apply is a subset of the union of all all-1 blocks (since each operation is an all-1 block). So the set of cells set is a subset of the union of all all-1 blocks. If a 1 cell is not in any all-1 block, it cannot be set by any operation, so impossible. So condition is necessary. So the check is correct.
Thus solution appears straightforward.
But is there any scenario where a 1 cell is not in any all-1 block, but could be set by a combination of operations that are not all-1 blocks individually? No, because each operation must be an all-1 block to avoid setting a 0. So the union of operations is a subset of the union of all all-1 blocks. So can't set any cell outside.
Therefore solution is to compute the set S of positions (i,j) such that the 2x2 square at (i,j) is all ones. Then check if every 1 in A belongs to at least one such square. If yes, output all S. Else -1.
We might also consider that we might not need to output all S; we could output any subset that covers all 1's. But outputting all S is simpler and also within limits.
But wait: Could outputting all S cause a problem? No, because extra operations just set cells that are already 1 (since they are within all-1 blocks) and don't affect final state. So it's fine.
Now, I'll double-check reasoning via alternative perspective: The problem can be seen as: We have target matrix A. We can perform operations that set a 2x2 submatrix to 1. Because operations only increase values, the final B is the OR of the selected submatrices. So we need to find a set of submatrices whose union is exactly the set of 1's in A, and each submatrix must be contained within the set of 1's (otherwise it would add extra 1's). So we need to cover the 1's with 2x2 squares that are entirely contained in the set of 1's. This is exactly the classic problem of covering a binary matrix with 2x2 "dominoes"? It's like covering the 1's with 2x2 tiles. If every 1 is covered by some such tile (i.e., each 1 belongs to a 2x2 all-ones block), then we can take all such tiles (or a subset) to cover all 1's. But note: Taking all tiles might over-cover? Over-cover meaning covering cells that are 1 anyway, so no problem. But does the union of all all-ones tiles exactly equal the set of 1's? Not necessarily; it could be a proper subset if some 1's are not in any all-ones tile, then union is smaller. But if every 1 is in at least one all-ones tile, then the union of all all-ones tiles includes all 1's. So union = superset? Actually union of all all-ones tiles is exactly the set of cells that belong to at least one all-ones tile. If every 1 belongs to at least one all-ones tile, then union contains all 1's. Also, any cell in the union is in some all-ones tile, so it's a 1 in A (since the tile is all ones). So union = set of 1's that are in at least one all-ones tile, which by hypothesis is all 1's. So union = set of all 1's. So it's exact.
Thus solution is correct.
Now, is there any scenario where we have a 1 cell that belongs to an all-ones tile, but that tile includes a cell that is not in any tile? Actually that cell is in that tile, so it's covered as well. So it's fine.
Thus final solution.
But let's double-check the coverage check: Suppose we have a 1 cell that is covered by an all-ones tile but we didn't mark it because we only marked cells from all-ones tiles. That's fine.
Now, implement.
Edge Cases and pitfalls:
-
Input format: numbers may be separated by spaces. Use split.
-
Output: If we output 0 operations, just print 0 and no further lines. That's allowed.
-
The indices: 1-indexed for output. In the internal loops, i from 0 to n-2, j from 0 to m-2 for top-left. When we add operation, we append (i+1, j+1).
-
When marking covered, we need to mark all four cells. For clarity, we can do covered[i][j] = True, etc.
-
After reading A, we might store as list of lists of ints.
Now, test with a more complex scenario to ensure correctness.
Consider n=4,m=4, A =
1 1 0 0
1 1 0 0
0 0 1 1
0 0 1 1
This is two separate 2x2 blocks of ones in top-left and bottom-right. Check each 1:
Top-left block: cells (1,1),(1,2),(2,1),(2,2) are all ones, and they are covered by the block (1,1) which is all ones. Bottom-right block: cells (3,3),(3,4),(4,3),(4,4) are ones; they are covered by block (3,3) (since i from 1 to 3? Actually indices: i=3, j=3? i < n => i=3 works if n=4? i index 3 (0-index) is 3? Actually n=4, indices 0,1,2,3. i up to 2 (since i+1<=3). So block with top-left (2,2) in 0-index corresponds to rows 2,3 and cols 2,3. That block includes (2,2)? Wait careful: In zero-index, rows 0,1,2,3. Bottom-right block: rows 2 and 3, cols 2 and 3. So top-left (2,2) in zero-index. That block is all ones. So operations: (1,1) and (3,3) in 1-index? Actually (2,2) zero-index => (3,3) 1-index? Let's compute: zero-index (i,j) corresponds to 1-index (i+1,j+1). So (2,2) zero-index -> (3,3). So output (1,1) and (3,3). Then covered: top-left 2x2 and bottom-right 2x2. All 1's covered. So works.
Now consider a case where 1's form a plus shape?
n=3,m=3:
1 1 1
1 1 1
1 1 1
But all ones, all covered. Many blocks. Output all 4 blocks: (1,1),(1,2),(2,1),(2,2). That's 4 ops. After applying all, all cells become 1. Works.
Now consider n=3,m=4:
A:
1 1 1 0
1 1 1 0
0 0 0 0
Check ones: row1 col1-3, row2 col1-3. For (1,3) cell, blocks: (1,2) includes (1,2),(1,3),(2,2),(2,3). All those are ones? (1,2)=1,(1,3)=1,(2,2)=1,(2,3)=1 => yes. So covered. Similarly, (2,3) covered by (1,2) or (2,2)? (2,2) includes rows 2-3, cols 2-3: (2,2)=1,(2,3)=1,(3,2)=0,(3,3)=0 => not all ones. So (2,3) is covered by (1,2). (1,1) covered by (1,1). So all ones covered. So possible.
Now consider a case where ones form a "staircase":
n=3,m=3:
1 0 0
1 1 0
0 1 1
Check coverage: (1,1)=1: only block (1,1) includes (1,1),(1,2),(2,1),(2,2). But (1,2)=0, so not covered. So impossible. Indeed to set (1,1) need block that sets (1,2) to 1, which is 0.
Now consider a case where ones are all except a hole inside that is isolated but still each 1 is covered? Example:
1 1 1 1
1 0 0 1
1 1 1 1
n=3,m=4. Check coverage: For cell (2,1)=1: blocks: (1,1) includes (1,1),(1,2),(2,1),(2,2). (2,2)=0 so not all ones. (2,1) also block (2,1) includes rows2-3, cols1-2: (2,1)=1,(2,2)=0,(3,1)=1,(3,2)=1 => not all ones. So (2,1) not covered. So impossible. Indeed, to set the left column middle row, would need to set (2,2) or (3,2) etc. So impossible.
Thus condition works.
Now, confirm that there is no situation where the union of all all-ones blocks is strictly larger than the set of ones? No, because each block is contained in ones, so union is subset of ones. So union cannot be larger. So final B will be exactly the union. So to equal A, we need union = A. So the condition is exactly that.
Therefore solution is complete.
Now, we could also output a minimal set of operations, but not required.
One more nuance: The problem states "you may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 × 2, and replace every element in the chosen submatrix with 1." So it's on B, not on A. But we apply operations that are all-1 blocks in A. Since A is target, we can pre-select those blocks. That's fine.
Now, what about the possibility of applying operations that are not all-1 blocks but after some operations, some zeros become ones, making later operations safe? For instance, if a cell is 0 in target, but we could later set it to 1 and then set it back? No back operation. So once set to 1, stays 1. So if a cell is 0 in target, it must never be set. So any operation that would set it at any time is prohibited. So any operation we perform must have all four cells being 0 initially? Not exactly; they could be 1 from previous operations, but they must be 1 in target. But if a cell is 0 in target, it must remain 0 always. So it must never be set. Thus it cannot be part of any operation. So all operations must consist entirely of cells that are 1 in target. Therefore indeed each operation must be an all-1 block.
Thus solution stands.
Now, we can code this.
We need to output the operations in any order.
Now, consider a scenario where there are many all-1 blocks, but some of them are not necessary; we output them all. That's fine.
But is there any case where outputting all all-1 blocks would exceed 2500? For n=m=50, (49*49)=2401, which is ≤ 2500. So it's okay. But wait, the problem says "0 ≤ k ≤ 2500 should hold."