We need to maximize the number of subrectangles (contiguous, with rows i1..i2 and cols j1..j2) that have odd sum. We have a grid size (2^N-1) x (2^M-1). We can choose each entry 0 or 1. We need to maximize number of subrectangles with odd sum. Possibly we can achieve maximum where all subrectangles are odd? But total subrectangles count is ((2^N-1)2^N/2?) Actually number of intervals: rows: choose i1<=i2: number of row intervals = R(R+1)/2 where R = 2^N-1. Similarly columns: C*(C+1)/2 with C = 2^M-1. So total subrectangles = (R(R+1)/2)*(C(C+1)/2). For all subrectangles to have odd sum, that would require some property? Possibly impossible. Need to maximize.
Let's denote grid size: R = 2^N - 1, C = 2^M - 1. Entries binary. For each subrectangle, sum mod 2. Count odd sums.
We want to maximize number of odd sums. Equivalent to maximize total oddness.
This is reminiscent of binary matrices and number of submatrices with parity. There's known results about maximum number of submatrices with odd sum. But we need to construct.
Given that N, M up to 10, R, C up to 1023, so total subrectangles huge. But we need to output a pattern.
We need to think about parity of sum over rectangle: S(i1,i2,j1,j2) = prefix sum P[i2][j2] XOR P[i1-1][j2] XOR P[i2][j1-1] XOR P[i1-1][j1-1] mod 2, if we define prefix sum modulo 2. So oddness condition: the XOR of four corners of prefix matrix (mod 2) equals 1.
So number of subrectangles with odd sum is number of choices of (i1<=i2, j1<=j2) such that P[i2][j2] XOR P[i1-1][j2] XOR P[i2][j1-1] XOR P[i1-1][j1-1] = 1, where P is prefix sum mod 2 (with P[0][] = P[][0] = 0). Here indices from 0 to R, with P[0][c]=0, P[r][0]=0.
Thus we have a binary matrix of size (R+1) x (C+1) for P, with first row/col zeros. Actually P[0][] and P[][0] are fixed to 0. The inner P entries (1..R, 1..C) are determined by a: a[i][j] = P[i][j] XOR P[i-1][j] XOR P[i][j-1] XOR P[i-1][j-1] (since inclusion-exclusion). But a must be binary. So any binary matrix P with first row and column zero yields a valid a? Check: For any assignment of P (binary) with P[0][c]=0, P[r][0]=0, define a[i][j] = P[i][j] XOR P[i-1][j] XOR P[i][j-1] XOR P[i-1][j-1]. This a is binary. Conversely, given a, we can compute P as cumulative XOR sum (mod2) of a. So it's bijective: P is the prefix XOR matrix (like 2D parity). So we can consider choosing P with fixed boundaries.
Now, number of subrectangles with odd sum = number of (i1,i2,j1,j2) with i1<=i2, j1<=j2 such that P[i2][j2] XOR P[i1-1][j2] XOR P[i2][j1-1] XOR P[i1-1][j1-1] = 1.
Note that (i1-1) and (i2) indices range 0..R, similarly j1-1, j2 range 0..C. So essentially we have a set of points: rows indices 0..R and columns indices 0..C. For any two row indices r1 < r2? Actually i1-1 and i2: let x = i1-1, y = i2, with x <= y-1? Because i1 <= i2 so x <= y-1. Actually i1-1 could equal y if i1-1 = i2 => i1 = i2+1 which is not allowed because i1 <= i2, so x <= y-1? Wait i1-1 = i2? That would imply i1 = i2+1 > i2, not allowed. So x <= y-1, i.e., x < y. So we consider pairs of row indices (x,y) with 0 <= x < y <= R. Similarly column indices (u,v) with 0 <= u < v <= C. Then condition: P[y][v] XOR P[x][v] XOR P[y][u] XOR P[x][u] = 1.
This is exactly number of axis-aligned rectangles with corners at (x,u), (x,v), (y,u), (y,v) (where x<y, u<v) such that XOR of the four corners = 1.
Thus the oddness is: sum over all pairs (x,y) with 0<=x<y<=R and (u,v) with 0<=u<v<=C of f(x,y;u,v) where f = 1 if the XOR of the four bits is 1.
Alternatively, we can think of this as: total number of rectangles (including those that could be of size zero? No, since x<y and u<v, it's non-empty) on the (R+1) x (C+1) grid of points with binary values (P), but the top row and left column are forced to 0. So we have a binary matrix Q of size (R+1) x (C+1) with Q[0][]=Q[][0]=0 (fixed), and the rest Q[i][j] for i,j>=1 can be chosen arbitrarily (since they correspond to a). Then the objective is to maximize the number of axis-aligned rectangles (with distinct opposite corners) for which the XOR of four corners = 1.
Thus problem reduces to: Given dimensions (R+1) x (C+1) with first row and column fixed to 0, choose the remaining entries to maximize number of rectangles (with x<y, u<v) where XOR of four corners = 1.
Now note that for any fixed rows x<y, columns u<v, the XOR of corners = (Q[x][u] XOR Q[x][v] XOR Q[y][u] XOR Q[y][v]). Since Q[0][] and Q[][0] are 0, many rectangles involve those fixed zeros.
Goal: maximize count.
Observation: For any two rows (i,j) with i<j, consider the vector of bits in those rows across columns. The contribution from these two rows is number of column pairs (u<v) such that (Q[i][u] XOR Q[i][v] XOR Q[j][u] XOR Q[j][v]) = 1.
Simplify: Define for each column c, the pair bits (Q[i][c], Q[j][c]). The XOR of the two bits across rows? Actually, for columns u and v, the expression is (Q[i][u] XOR Q[i][v] XOR Q[j][u] XOR Q[j][v]) = (Q[i][u] XOR Q[j][u]) XOR (Q[i][v] XOR Q[j][v])? Let's check: (a XOR b XOR c XOR d) = (a XOR c) XOR (b XOR d)? Actually XOR is associative and commutative: (a XOR b) XOR (c XOR d) = a XOR b XOR c XOR d. But we want to express as (a XOR c) XOR (b XOR d). Compute (a XOR c) XOR (b XOR d) = a XOR c XOR b XOR d = same. So indeed it equals (Q[i][u] XOR Q[j][u]) XOR (Q[i][v] XOR Q[j][v]). So if we define D_c = Q[i][c] XOR Q[j][c] (the difference between the two rows at column c), then the condition is D_u XOR D_v = 1, i.e., D_u != D_v. So for a given pair of rows, the number of column pairs (u<v) giving odd rectangle is: (# of columns where D=0) * (# where D=1). Because we need D_u != D_v.
Similarly, for a fixed pair of columns, if we consider rows, the expression symmetric.
Thus total oddness = sum over all row pairs (i<j) of (z_{ij} * o_{ij})? Actually for each row pair (i,j), let A = number of columns c with D_c=0, B = number with D_c=1. Then number of column pairs (u<v) contributing is AB. So contribution = AB.
But careful: This counts each rectangle exactly once (since rows fixed, columns fixed). So total = Σ_{0 ≤ i < j ≤ R} ( (# zeros in D_{ij}) * (# ones in D_{ij}) ), where D_{ij}[c] = Q[i][c] XOR Q[j][c] for c=0..C. However columns include 0, which is fixed to 0 for all rows? Actually Q[0][c] is fixed to 0 for all c? Wait first row (index 0) is all zeros? Actually Q[0][c] = 0 for all c? Yes, because row 0 corresponds to i1-1 = 0, P[0][c] = 0. Similarly Q[r][0] = 0. So column index 0 is also fixed to 0 for all rows. So the columns range 0..C, with column 0 all zeros. So D_{ij}[0] = Q[i][0] XOR Q[j][0] = 0 XOR 0 = 0. So column 0 always contributes to zeros. So we need to account for that. Similarly rows include index 0 fixed.
Thus we can view Q as (R+1) x (C+1) matrix with first row and first column zeros. The rest entries are free.
Now the total sum: Σ_{0≤i<j≤R} f(i,j) where f(i,j) = (A_{ij} * B_{ij}) with A_{ij}= number of columns c in [0..C] such that Q[i][c] XOR Q[j][c] = 0, B_{ij}= number where it's 1.
Because column count includes column 0 which gives 0 always. So that may affect.
Alternatively, we can sum over column pairs similarly, but it's symmetric.
Goal: maximize Σ_{i<j} A_{ij} B_{ij}. Since A_{ij}+B_{ij}=C+1 (total columns). For fixed total columns Ncol = C+1, product A*B is maximized when A and B are as balanced as possible: max product is floor(Ncol/2)*ceil(Ncol/2). So the maximum possible for a given pair of rows is at most floor((C+1)/2) * ceil((C+1)/2). Similarly, for each row pair we want to achieve that product. Is it possible to simultaneously achieve maximum product for all row pairs? That would require that for every pair of rows i<j, the Hamming distance between their rows (over all columns) is either floor((C+1)/2) or ceil((C+1)/2) (i.e., as balanced as possible). Moreover, the row vectors (of length C+1) must have first column (index 0) fixed to 0, so each row vector has its 0-th entry = 0. So we need a set of (R+1) binary vectors of length C+1, with first coordinate 0, such that the Hamming distance between any two distinct vectors is as large as possible, ideally exactly floor((C+1)/2) or ceil((C+1)/2). But maximum possible pairwise distance given length L with first bit fixed? Actually maximum possible minimum Hamming distance? But we need each pair to have distance about half length. For L = C+1. In binary vectors, typical pairwise distances for a large set can vary. We have up to R+1 = 2^N, since R=2^N-1 so R+1 = 2^N. Similarly C+1 = 2^M. So we have 2^N rows (indices 0..2^N-1?) Actually R = 2^N-1, so rows indices 0..2^N-1? Because 0 to R inclusive gives count = R+1 = 2^N. Yes exactly. Similarly columns indices 0..C inclusive, count = C+1 = 2^M. So Q is a 2^N x 2^M binary matrix with first row and first column forced to 0. That's interesting: dimensions are powers of two.
Thus we have a binary matrix of size 2^N by 2^M, with row 0 all zeros, column 0 all zeros. We can choose other entries arbitrarily. We want to maximize Σ_{0≤i<j<2^N} A_{ij}B_{ij}, where A_{ij}=#columns where rows i and j agree, B_{ij}=#columns where they differ. Since total columns = 2^M, product = A_{ij}*(2^M - A_{ij}) = (2^M)*A_{ij} - A_{ij}^2. To maximize total sum, we want each A_{ij} to be as close to 2^{M-1} as possible (since product is symmetric). Actually product is maximized when A=2^{M-1} (if 2^M even) or when A= floor/ceil. So we want Hamming distance between rows i and j to be as close to 2^{M-1} as possible.
But can we achieve that for all pairs simultaneously? Given that row 0 is all zeros, then for any row i>0, the Hamming distance between row 0 and row i is simply the number of 1's in row i (since row0 all zeros). That distance must also be about 2^{M-1}. So each row i should have about half ones. So each row should have roughly 2^{M-1} ones. Also for two non-zero rows i and j, the Hamming distance = (popcount(row_i XOR row_j)). Since popcount(row_i) ≈ 2^{M-1}, popcount(row_j) ≈ 2^{M-1}, then the distance can range from 0 to 2^M. But to be around 2^{M-1}, rows should be nearly orthogonal (uncorrelated). This suggests using rows that are independent binary vectors such that any two distinct rows have Hamming distance exactly 2^{M-1} (if possible). Is that possible for up to 2^N rows of length 2^M? If we consider all vectors in a vector space over GF(2), the Hamming distance between two vectors equals popcount of their XOR. For vectors in GF(2)^(2^M) (with coordinate 0 fixed? Actually first coordinate is fixed 0, so we are in a subspace of dimension 2^M-1? But we could ignore first coordinate? Actually row 0 is all zeros, and row i for i>0 must have first coordinate 0 because Q[i][0]=0. So all rows have 0 in column 0. So effectively each row vector is of length L = 2^M, but first bit always 0. So the effective length for distance is L, but the first coordinate contributes 0 always (agreement). So the maximum possible distance between two rows is at most 2^M - 1 if they differ in all other columns. But we can treat columns 1..2^M-1 free. So total columns with variability is 2^M - 1? Wait careful: columns indices: 0..C inclusive, C = 2^M-1, so total columns = 2^M. Column 0 is forced 0 for all rows. So each row has a vector of length 2^M with first coordinate fixed 0. So the remaining 2^M - 1 coordinates are free. So each row can be thought of as a binary vector of length (2^M - 1) (since coordinate 0 is always 0, and it's the same for all rows, so it contributes to agreement always). Then the distance between two rows is the Hamming distance over the full 2^M coordinates, which is just the distance over the (2^M - 1) free coordinates because coordinate 0 always equal (0). So effectively L' = 2^M - 1. Then total columns count in product A*B is 2^M, but the 0-th column always gives agreement, so A includes that column always. So for any pair, there is at least 1 agreement from column 0. So A_{ij} = 1 + (number of other columns where they agree). B_{ij} = (number where they differ). And total columns = 2^M, so A_{ij} + B_{ij} = 2^M.
Goal: maximize A_{ij} * B_{ij} = A*(2^M - A). With A at least 1. The maximum occurs when A is as close to 2^M/2 as possible. If 2^M is even, maximum at A=2^{M-1} (exactly half). If 2^M is odd? 2^M is always even for M>=1. For M=0? But M>=1, so 2^M is even. So maximum product is (2^{M-1})^2 = 4^{M-1}? Actually (2^{M-1})(2^{M-1}) = 2^{2M-2}. But wait product is AB = (2^{M-1})^2 = 2^{2M-2}. That's for even total columns. So we want A_{ij}=2^{M-1} exactly. So we need each pair of rows to agree in exactly half the columns, i.e., Hamming distance = 2^{M-1}. Since column 0 always agrees, that means over the remaining (2^M - 1) columns, they must agree in exactly (2^{M-1} - 1) columns? Because total agreements = 1 (col0) + agreements_rest. So we need 1 + agreements_rest = 2^{M-1} => agreements_rest = 2^{M-1} - 1. Then disagreements_rest = (2^M - 1) - (2^{M-1} - 1) = 2^M - 1 - 2^{M-1} + 1 = 2^{M-1}. So Hamming distance (over all columns) = disagreements = 2^{M-1} (since all disagreements are in rest). So distance = 2^{M-1}. So indeed each pair should have Hamming distance exactly 2^{M-1} over the full length, which is half of 2^M.
So we need a set of 2^N binary vectors of length 2^M (with first coordinate fixed to 0) such that any two distinct vectors have Hamming distance exactly 2^{M-1}. Additionally, row 0 is all zeros. So we need 2^N - 1 other vectors each with Hamming weight exactly 2^{M-1} (since distance from all-zero is weight) and pairwise distance exactly 2^{M-1}. That's a set of binary vectors with constant weight and constant pairwise distance. That is like an equidistant code. In binary, if two vectors have same weight w and distance d, then their dot product? Actually distance = weight(v) + weight(u) - 2inner = w + w - 2inner => inner = (2w - d)/2. For distance d = 2^{M-1} and w = 2^{M-1}, then inner = (2w - d)/2 = (2*2^{M-1} - 2^{M-1})/2 = (2^{M-1})/2 = 2^{M-2}. So inner product (over reals, number of positions where both 1) should be 2^{M-2}. So we need a set of binary vectors of length L=2^M, all with weight w = L/2, and pairwise intersection size exactly w/2. Also first coordinate forced 0, so these vectors must have 0 at coordinate 0. That means coordinate 0 contributes weight 0 for all vectors. So effectively we need vectors of length L-1 = 2^M - 1 with weight w' = w = L/2? Wait weight of full vector includes coordinate 0, which is 0. So total weight = weight over rest. So we require weight over rest = L/2 = 2^{M-1}. So we need vectors in {0,1}^{L-1} with weight 2^{M-1} and pairwise intersection (number of positions where both 1) = 2^{M-2}. This is reminiscent of the rows of a Hadamard matrix? Actually consider the set of all vectors in {0,1}^{L} that are indicators of subsets of size L/2? Not all pairs have constant intersection.
Is it possible to have 2^N such vectors? Since L = 2^M is a power of two, we can think of the group of binary vectors of length L, but with a fixed first coordinate 0. The maximum size of an equidistant code with distance L/2? There is known concept: Hadamard matrices produce sets of ±1 vectors with pairwise inner product -1/(L) maybe? For binary, we can use the rows of a Hadamard matrix (excluding first) mapped to 0/1 by (1+value)/2, giving constant weight L/2 and pairwise intersection L/4. For a Hadamard matrix of order L (which exists if L is a power of two), we have L rows of ±1 that are orthogonal. After mapping to binary (0/1) by (1+value)/2, the rows have weight L/2, and any two distinct rows have exactly L/4 positions where they both are 1 (since dot product of ±1 vectors is 0: each coordinate product is ±1, sum zero, so number of agreements minus disagreements =0; after mapping, the count of both 1 is L/4, both 0 is L/4, one 1 one 0 is L/2). Indeed for ±1 vectors u and v with entries ±1, we have u_i * v_i = 1 if they agree, -1 if disagree. Since sum_i u_i v_i = 0 (orthogonal), then #agreements - #disagreements = 0, so #agreements = #disagreements = L/2. After mapping to binary: Let a_i = (1+u_i)/2, b_i = (1+v_i)/2. Then both 1 when u_i=v_i=1 => both 1. Both 0 when u_i=v_i=-1. And one 1 one 0 otherwise. For agreements (u_i=v_i), we have half of coordinates? Actually from #agreements = L/2, those consist of both 1 and both 0. We need to know distribution. Since the rows have equal numbers of +1 and -1? In a Hadamard matrix normalized with first row all +1, then each other row has exactly L/2 +1 and L/2 -1. And orthogonal means for any two distinct rows, the number of coordinates where they are both +1 equals number where both -1 = L/4, and number where they differ = L/2. So after mapping to binary, both 1 count = L/4, both 0 count = L/4, and one 1 one 0 count = L/2. So weight of each row (excluding first) = L/2 (since half +1). And intersection size (both 1) = L/4. So this matches our requirement: L = 2^M, weight = L/2 = 2^{M-1}, intersection = L/4 = 2^{M-2}. Also note that first column we need to be 0 for all rows. In the Hadamard construction, we can set the first coordinate to correspond to a fixed value? Let's see: We have rows of a Hadamard matrix of order L = 2^M. The standard Sylvester construction: Hadamard matrix H_{2^k} = H_2 ⊗ ... with H_2 = [[1,1],[1,-1]]. The first row is all ones. Each other row has equal number of +1 and -1. In the binary mapping, we set a_{i,j} = (1 + H_{i,j})/2. This yields first row becomes all ones (since H_{1,j}=1). But we need row 0 to be all zeros. In our Q matrix, row 0 is all zeros. So we can perhaps shift: Use the Hadamard rows for rows 1..L-1, and set row 0 as zeros. But also we need column 0 to be zeros for all rows. In the Hadamard construction, column 0 corresponds to first column. If we take the Hadamard matrix with rows as our Q rows (excluding row 0 maybe), we have L rows (including row 0?). Actually we need total rows = 2^N, which could be less than or equal to L? Because 2^N vs 2^M. N and M can be different. So we need up to 2^N rows, but the number of available rows from a Hadamard matrix of size 2^M is 2^M (including the all-ones row). However we only need 2^N rows. Since N and M up to 10, we can have 2^N ≤ 2^M or 2^N > 2^M. We need to maximize across all row pairs. The product for a pair of rows is A*B. The maximum possible product for a given pair is floor(2^M/2)*ceil(2^M/2) = (2^{M-1})^2, since 2^M even. But can we achieve that product for all row pairs simultaneously? Maybe not if number of rows exceeds something. There might be combinatorial constraints: maximum size of a set of binary vectors of length L with constant weight L/2 and pairwise intersection L/4 is at most L (by Fisher's inequality? Actually it's related to equiangular lines, but such sets exist: the rows of a Hadamard matrix give L vectors (including all ones) with these properties. That's exactly a simplex? Actually the set of L vectors (including all ones) in {0,1}^L with weight L/2 and pairwise intersection L/4 is possible when L is a power of 2. That's a classic example: the (L, L/2, L/4) symmetric block design? Indeed, the set of L vectors corresponds to the incidence of points and blocks in a symmetric 2-(L, L/2, L/4) design (if you take complement as well). So maximum possible size is L. So if 2^N ≤ 2^M, we can pick 2^N rows from that Hadamard set (including maybe the all-ones row) and then adjust for column 0 fixed zero. But we need column 0 to be zero for all rows. In the Hadamard set, column 0 is not necessarily zero. We can maybe permute columns and set column 0 to be the one where all rows have 0? We need Q[i][0] = 0 for all i. So we can take the Hadamard vectors, then perhaps complement some bits to make the first coordinate 0. Since we have flexibility, we can apply a column flipping? Let's think.
We have Q matrix of size 2^N x 2^M. Row 0 is all zeros. For rows 1..2^N-1, we need vectors of length 2^M with Q[i][0]=0. We can choose these vectors arbitrarily. The Hadamard set gives vectors with weight 2^{M-1} and pairwise intersection 2^{M-2}. But they have no guarantee about first coordinate. However, we can apply a transformation: For each row, we can XOR with a fixed vector (i.e., flip certain columns) without changing the pairwise distances? Actually Hamming distance between two vectors is unchanged if we apply the same bitwise XOR to both? If we apply the same flipping (i.e., complement some coordinates) to all rows uniformly, Hamming distances remain the same because (u XOR v) = (u' XOR v') if we apply same XOR mask to each. Indeed, if we set Q'[i] = Q[i] XOR t for all i, then distance(Q'[i], Q'[j]) = popcount( (Q[i] XOR t) XOR (Q[j] XOR t) ) = popcount(Q[i] XOR Q[j]) same. Also product AB? But A and B refer to agreement counts, not just distance. If we flip some columns globally, then for a pair of rows, the number of agreements on a column flips if the column is flipped? Actually if we XOR all rows with a fixed mask m, then for each column c, Q'[i][c] = Q[i][c] XOR m_c. For two rows i,j, agreement on column c originally: Q[i][c] == Q[j][c]. After XOR with m_c, condition becomes (Q[i][c] XOR m_c) == (Q[j][c] XOR m_c) which is equivalent to Q[i][c] == Q[j][c]. So agreement is unchanged! Because XOR with same bit on both does not change equality. So indeed, flipping columns globally does not affect agreement patterns between rows. Therefore we can freely permute columns and flip columns? Flipping globally doesn't change agreement. Permuting columns also just relabels columns, doesn't affect agreement counts. So we can rearrange columns arbitrarily. Therefore, we can take the Hadamard vectors (or any set) and then permute columns so that column 0 becomes a column where all rows have the same value? We need Q[i][0]=0 for all i. That means for each row i, the bit at column 0 must be 0. So if we have a set of vectors where some column (say the first) has some pattern, we can possibly transform to make all zeros? Since flipping a column globally would flip all entries in that column. If we want all zeros, we can start with a column that is constant across rows, either all 0 or all 1. If it's all 0, good; if it's all 1, flipping it makes all 0. So we need a column that is constant across all rows (including row 0 which is zero). But row 0 is all zeros already; if we include row 0, then we need that column to be 0 for row 0, and also for other rows we can set? However, if we take a set of vectors that includes the all-zero vector (row 0) and some others, and we want a column where all rows have the same value, we need that column to be 0 for all rows. In the Hadamard set, if we include the all-zero row (which is not part of Hadamard rows because Hadamard rows have weight half), we need to check if there is a column constant across all rows including the all-zero row. In the Hadamard matrix, rows are orthogonal. If we take the Hadamard matrix (with entries ±1) and map to binary, we have L vectors (including the all-ones row after mapping). The all-ones row corresponds to the first row of Hadamard which is all +1, mapping to all 1's. That's not all zeros. If we want an all-zero row, we could take the complement of that? Actually we can include the all-zero vector by adding it, but then we would have to check distances from all-zero to others. Distance from all-zero to a vector of weight L/2 is L/2, which is exactly what we need (since we want distance L/2). So we could take row 0 as all zeros, and then take L-1 other vectors from the Hadamard set? However, the Hadamard set (L vectors) includes one vector that is all ones (or all 1 after mapping). That vector's weight is L. But we want weight L/2. So maybe we should not include that one. Instead, we consider the set of vectors obtained from a Hadamard matrix of order L (power of two) where we take rows 2..L (excluding the first all-ones row) and map to binary via (1+entry)/2. Those rows have weight L/2. That gives L-1 vectors. Additionally we can have the all-zero vector. That gives L vectors total? Actually L-1+1 = L vectors. But we need 2^N rows; if 2^N <= L, we can pick a subset. But does the all-zero vector together with those L-1 vectors maintain constant pairwise distance? Distance between all-zero and any of those is weight = L/2, good. Distance between any two of those L-1 vectors is L/2? As per Hadamard property, yes: their Hamming distance = L/2 (since they have weight L/2 each and intersection L/4, so distance = weight_i + weight_j - 2intersection = L/2+L/2 - 2*(L/4) = L). Wait compute: weight each = L/2. Intersection = L/4. So distance = L/2 + L/2 - 2*(L/4) = L - L/2 = L/2? Actually L - L/2 = L/2. Yes L - L/2 = L/2. So distance = L/2. Good. So the set of L vectors (all-zero plus L-1 Hadamard vectors) have pairwise distance L/2? Check distance between two Hadamard vectors: L/2. Distance between all-zero and any Hadamard: L/2. So all pairwise distances are L/2. That's perfect! However, we must ensure that the all-zero vector is distinct from the others; it's fine.
But does the all-zero vector have weight L/2? No, it has weight 0, but we don't need all vectors to have weight L/2? For product A*B, for a pair (i,j), we need A_{ij}*B_{ij} to be maximized, which requires A = B = L/2. That requires that for that pair, number of agreements = number of disagreements = L/2. For the pair (0,i) where row0 is all-zero, we need A_{0,i}*B_{0,i} maximized. A_{0,i} = number of columns where both are 0? Since row0 is all 0, agreement occurs when row i has 0; disagreement when row i has 1. So A_{0,i} = (L - weight(row_i)), B_{0,i} = weight(row_i). For product to be L^2/4, we need weight(row_i) = L/2. So indeed row i must have weight exactly L/2. So the all-zero row is fine as long as each other row has weight L/2. That holds for the Hadamard vectors (excluding the all-ones one). So if we include row 0 (all zero) and L-1 vectors each of weight L/2 and pairwise distance L/2, we have L vectors with the desired property. But note: we also need the column 0 to be zero for all rows. Currently, the Hadamard vectors may have varying bits in each column. However, we can apply a column permutation and global flipping to make column 0 all zero? We need to ensure that after transformations, row 0 remains all zero? Row 0 is all zero initially; any global column flipping would flip bits in that row, making it not all zero. So we cannot apply global flipping to all rows because that would change row 0. But we could apply a transformation that only affects rows other than row 0? But then pairwise distances between rows would be affected differently. However, we could instead not start with row 0 as all-zero; we could design Q such that row 0 is all zero (fixed) and other rows are chosen to meet conditions. The transformations we can apply to the set of rows (excluding row 0) can include permuting columns and flipping columns, but flipping a column would change the entry in row 0 as well if we apply to all rows. But we can think: we are free to assign values to Q[i][j] for i>=1, j>=1 arbitrarily. The fixed zeros are row 0 and column 0. So we cannot flip column 0 because it's fixed to 0 for all rows. But we can permute columns? Column 0 is fixed; we cannot permute column 0 with others because column 0 is a specific index that must be zero. But we can relabel columns: we have indices 0..L-1; column 0 is fixed zero. So we cannot change that. So our design must have Q[i][0]=0 for all i. So column 0 is forced 0. The other columns (1..L-1) we can assign freely. So in our construction, we need vectors of length L, where the first coordinate (column 0) is 0 for all rows. So effectively our vectors have a fixed 0 at position 0. The all-zero row naturally has 0 there. For other rows, we need them to have 0 at column 0 as well. So we need to construct a set of vectors (of length L, with first bit 0) such that:
- For each row i (i>=1), weight over all L columns is L/2.
- For any two distinct rows i,j (including i=0), Hamming distance = L/2.
If we can construct such a set of size up to L (including row 0), then we can achieve maximum oddness: For each row pair, AB = (L/2)(L/2) = L^2/4. Then total oddness = number of row pairs * number of column pairs? Wait careful: total oddness = Σ_{i<j} A_{ij} B_{ij}. If each pair achieves product = (L/2)(L/2) = L^2/4, then total = (number of row pairs) * (L^2/4). Number of row pairs = (R+1 choose 2) = (2^N choose 2) = 2^{N-1}(2^N - 1). So total oddness = (2^{N-1}(2^N - 1)) * (L^2/4) where L = 2^M. That is (2^{N-1}(2^N - 1)) * (2^{2M}/4) = (2^{N-1}(2^N - 1)) * 2^{2M-2} = (2^{N+2M-3}(2^N - 1)). But is that the maximum possible? Perhaps it's the theoretical upper bound because for each pair (i,j) the product cannot exceed (L/2)^2. So sum over all pairs ≤ (number of pairs)(L/2)^2. So if we can achieve equality for all pairs, that's maximum.
But is it always possible to have such a set of size up to 2^N given constraints? The maximum possible size of a set of binary vectors of length L (with first coordinate fixed 0) such that all pairwise Hamming distances are exactly L/2. This is a problem of constructing an equiangular set? Actually it's a set of vectors with constant pairwise distance. Over binary, such sets are equivalent to constant-weight codes with specific intersection. Known maximum size for binary vectors of length L with pairwise distance L/2: It is at most L, and can be achieved when L is a power of 2 using the rows of a Hadamard matrix (with all-ones row excluded, but including all-zero vector). However, we have the extra constraint that all vectors have first coordinate 0. In the standard Hadamard construction, we can adjust.
Let's attempt to construct such vectors. Let L = 2^M. We need up to K = 2^N vectors including the all-zero vector. Since N, M up to 10, we can brute force? Not needed; we need constructive pattern.
Approach: Use the rows of a Walsh-Hadamard matrix of order L. Specifically, consider the L x L matrix H with entries ±1, generated recursively: H(1)=[1]; H(2n) = [[H(n), H(n)],[H(n), -H(n)]]. The first row is all ones. Each other row has equal number of +1 and -1. Now, map +1 -> 0, -1 -> 1? Or map to binary such that weight becomes L/2? Actually we need weight L/2. If we map +1 -> 0, -1 -> 1, then a row with equal +1 and -1 becomes weight L/2 (since -1 maps to 1). The first row (all +1) would map to all 0, which is all-zero vector. That's perfect! Because row 0 in Q is all-zero. So if we take the Hadamard matrix H of order L, and map +1→0, -1→1 (or maybe the opposite), then the first row becomes all 0. And the other rows become vectors of weight L/2, since they have exactly L/2 entries of -1 (assuming half +1, half -1). Indeed in Sylvester construction, each non-first row has exactly L/2 +1 and L/2 -1. So after mapping +1→0, -1→1, we get rows: first row all 0; other rows have exactly L/2 ones. Also, what about pairwise distances? For any two distinct rows, the Hamming distance between the binary vectors is the number of positions where they differ. In the ±1 representation, they are orthogonal: sum_i H_{ri} H_{ci} = 0. This implies that the number of positions where they agree minus the number where they disagree = 0, so #agreements = #disagreements = L/2. Under mapping +1→0, -1→1, agreement in binary corresponds to original entries being same (both +1 or both -1). So #agreements remains L/2, #disagreements L/2. So Hamming distance = #disagreements = L/2. Perfect! Also, note column 0 (first column) in the Hadamard matrix: In the Sylvester construction, the first column of H is all +1? Actually in the recursive construction, the first column is all +1 because H(1)=[1], and when we do Kronecker product, the first column remains all +1? Let's check: H_2 = [[1,1],[1,-1]]. First column: [1,1]^T. H_4 = H_2 ⊗ H_2 = [[1,1,1,1],[1,-1,1,-1],[1,1,-1,-1],[1,-1,-1,1]]. First column is all 1. So yes, the first column is all +1. Under mapping +1→0, that column becomes all 0. So column 0 (the first column) becomes all zeros automatically! That's exactly what we need: Q[i][0] = 0 for all i. Perfect! So the matrix Q (prefix XOR matrix) can be taken as the binary image of the Sylvester Hadamard matrix of order L = 2^M, with mapping: 0 for +1, 1 for -1. Then Q has dimensions L x L. But we only need rows 0..2^N-1? Actually we need (R+1) = 2^N rows. That is the number of rows we need. But Q must have exactly 2^N rows? Wait Q is the prefix XOR matrix of size (R+1) x (C+1) where R+1 = 2^N, C+1 = 2^M. So Q has dimensions 2^N rows and 2^M columns. So if 2^N ≤ 2^M, we can simply take the first 2^N rows of this Hadamard matrix (after mapping). The first row (all zeros) is row 0. Then rows 1..2^N-1 correspond to some of the other Hadamard rows. Since the Hadamard rows (except first) all have weight L/2 and pairwise distance L/2 (as they are orthogonal), any subset will also have pairwise distance L/2? Actually pairwise distance between any two distinct rows from the set is still L/2, because orthogonality holds for any two distinct rows of the full Hadamard matrix. So taking any subset preserves that property. So if 2^N ≤ 2^M, we can take the first 2^N rows (including row 0) and that gives a matrix Q with desired property: each pair of rows has exactly L/2 agreements and L/2 disagreements. So product A(L/2) = 4^{M-1} (since L=2^M, L/2=2^{M-1})? Actually L/2 = 2^{M-1}. So product = 2^{2M-2}. Great.
If 2^N > 2^M, then we have more rows than columns. We need to construct 2^N rows of length L=2^M with the property that pairwise distance = L/2. But maximum size of such a set is at most L (by some bound). Because we can treat each row as a vector in {0,1}^L. For constant distance L/2, the maximum size is at most L (see fact: Equidistant binary codes with distance L/2 are equivalent to a set of mutually unbiased bases? Actually known: Binary codes with all distances equal to L/2 can have size at most L, achieved by the rows of a Hadamard matrix plus complement? Including all-zero gives size L. So if 2^N > L, we cannot have all pairs distance L/2. So the maximum achievable sum may be less than the naive upper bound. Since N and M up to 10, 2^N and 2^M can be up to 1024. So it's possible that 2^N > 2^M. In that case we need to design a set of 2^N rows with length L, where row 0 is all zeros, and we want to maximize Σ_{i<j} A_{ij} B_{ij} = Σ_{i<j} d_{ij} * (L - d_{ij})? Actually product = agreements * disagreements = (L - d) * d? Wait distance d = number of disagreements = B_{ij}. Agreements A = L - d. So product = d*(L-d). Since L is even, product is maximized when d = L/2, giving L^2/4. If d deviates, product is smaller.
So when we have more rows than L, we cannot have all pairs at distance L/2. We need to design rows to maximize sum of d*(L-d) over all pairs. This is a combinatorial optimization: Choose a set S of size K = 2^N vectors in {0,1}^L (with first coordinate 0? Actually column 0 is fixed 0, so length L but coordinate 0 is forced 0. So effectively the vectors lie in a subspace of dimension L-1? But we can still consider the full length with coordinate 0 fixed 0. The distance contributions from column 0 is always 0 because both are 0, so that column always agrees. So effective length for distance is L-1? Wait careful: distance d = number of columns where rows differ. Since column 0 always same (0), it never contributes to disagreements. So disagreements only come from columns 1..L-1. So the maximum possible distance between two rows is at most L-1. But we previously thought max distance L/2? But L is even, L/2 may be less than L-1. Actually from the Hadamard construction, we got distance L/2, but that's with all columns including column 0. In that construction, column 0 contributed 0 to disagreements because both rows had 0 in column 0? In Hadamard mapping, column 0 is all zeros, so indeed it's fixed 0. So distance between rows is measured over all columns, but column 0 always 0 for all rows, so it never adds to distance. So the maximum possible distance between two rows is at most L-1 (if they differ in all other columns). But can we achieve distance L/2? Yes, it's possible, but maybe we can achieve larger distances? However product d*(L-d) is maximized when d is as close to L/2 as possible. Since L is even, L/2 is integer, and d can range from 0 to L. But with column 0 fixed, d can be at most L-1 (since column 0 always agreement). But d = L/2 is less than L-1 if L>2. So the product maximum is at (L/2)*(L/2) = L^2/4. If d could be larger, say d = L-1, then product = (L-1)*1 = L-1, which is much smaller than L^2/4 for L>2. So to maximize product, we want d close to L/2, not necessarily maximum. So the Hadamard construction gave d = L/2, which is optimal. Could we have d = L/2 exactly? Yes. So the maximum product per pair is L^2/4. So we want as many pairs as possible to have d = L/2. But we cannot exceed size L for a code with all pairwise distances exactly L/2 (as known from orthogonal arrays). So if K = 2^N > L, we cannot have all pairs at distance L/2. Then some pairs will have distance different from L/2, giving product less than L^2/4.
Thus we need to find the maximum possible sum given K vectors with fixed first coordinate 0. This is reminiscent of constructing a set of binary vectors that maximizes sum of d*(L-d). Possibly the maximum is achieved by taking all possible vectors of weight L/2? But the number of such vectors is C(L-1, L/2) maybe huge, but we have only K vectors. We need to choose K vectors to maximize sum of products. This is like maximizing sum over pairs of Hamming distances (or rather the product). For fixed L, the sum of distances is also something. But we need a constructive solution for any N,M up to 10. Since input sizes small (grid up to 1023x1023), we can output a pattern. Maybe the problem expects a simple construction using XOR of row and column indices? The oddness maximization might be achieved by setting a_{i,j} = parity of (i & j) or something like that. That is reminiscent of the "Pascal matrix" or "Sierpinski triangle". Let's explore.
Recall that the prefix XOR matrix Q is defined from a. There might be a known pattern: For a grid of size (2^N-1) x (2^M-1), filling a_{i,j} = (i & j) == 0? Or maybe a_{i,j} = ((i-1) & (j-1)) == 0? Actually there's a known pattern: In the context of combinatorial game "Turning Turtles" or "Nim", the Sprague-Grundy matrix for Wythoff? Not sure.
Alternate viewpoint: We want to maximize number of subrectangles with odd sum. For each pair of rows (i<j) and columns (u<v), condition is D_u != D_v where D_c = Q[i][c] XOR Q[j][c]. So for each row pair, the number of column pairs with D_u != D_v is (#0s)(#1s). So overall oddness = Σ_{i<j} f_i_j, where f_i_j = z_{ij} * o_{ij}. To maximize, we want for each row pair to have as balanced D vector as possible (i.e., half zeros, half ones). Since we can choose Q freely (subject to Q[0][]=0, Q[*][0]=0), we have control over rows. Similarly, by symmetry, we could also think about column pairs.
We can treat rows as vectors in {0,1}^{L}, with first coordinate fixed 0. Let’s denote the set of rows as R0, R1, ..., R_{K-1} where K=2^N. R0 = zero vector. For i>=1, we choose vectors v_i. Then oddness = Σ_{0≤i<j≤K-1} ( (L - d(v_i, v_j) ) * d(v_i, v_j) ), where d = Hamming distance. Because agreements = L - d, disagreements = d, product = d(L-d). Since L even, maximum per pair is (L/2)^2.
So we need to maximize Σ_{i<j} d_{ij} (L - d_{ij}). This is equivalent to maximizing Σ_{i<j} ( - (d_{ij} - L/2)^2 + (L/2)^2 ). Because d(L-d) = -(d - L/2)^2 + (L/2)^2. So maximizing sum of products is equivalent to minimizing sum of squared deviations from L/2. So we want all distances as close to L/2 as possible.
This is a coding theory problem: Find a code of size K in {0,1}^L (with a fixed coordinate 0 for all codewords) that minimizes sum of squared deviations from L/2 of pairwise Hamming distances. For L a power of two, one optimal construction is to use the rows of a Hadamard matrix (mapped) which gives all distances exactly L/2 for up to L codewords. For K > L, we cannot keep all distances L/2; we must have some distances deviating. Then what's the maximum sum? Possibly we can take all possible vectors? But there are only 2^{L-1} vectors (since first bit fixed 0) but K may be up to 2^N which could be up to 1024, while L = 2^M could be as small as 2 (if M=1). Let's analyze small cases to see pattern.
We need to output a grid for given N,M. Maybe there is a known optimal construction regardless of N,M: Fill a_{i,j} = 1 if (i & j) == 0? Or maybe fill with 1s on all cells? That would give many odd sums? Let's test small.
We need to produce a pattern that maximizes oddness. The constraints small enough that we could theoretically compute optimal via brute force for given N,M? But we need to output a solution for any N,M up to 10, so there must be a simple pattern.
I suspect the intended solution is to use the Hadamard matrix construction for Q, i.e., set Q[i][j] = parity of popcount(i & j) or something like that. Indeed, the Sylvester Hadamard matrix H of size 2^k has entries H_{i,j} = (-1)^{popcount(i·j)} if we index rows and columns from 0, using bitwise dot product (over GF(2)). Specifically, if we index rows i and columns j from 0 to 2^k-1, and define H_{i,j} = (-1)^{ i·j } where i·j is the dot product of binary representations (i & j) then popcount mod 2. That's the Walsh-Hadamard matrix. That matrix has first row and first column all 1 (since 0·j = 0). To map to binary with first row all 0 and first column all 0, we could set Q[i][j] = popcount(i & j) mod 2? That would give 0/1. Check: For i=0, i&j=0 => 0, so Q[0][j] = 0, good. For j=0, i&0=0 => Q[i][0]=0, good. So define Q[i][j] = (popcount(i & j) mod 2). That yields a binary matrix of size 2^N x 2^M? But careful: i ranges 0..2^N-1, j ranges 0..2^M-1. popcount(i & j) mod 2 is the parity of the bitwise AND of i and j. Since i has N bits, j has M bits. However i and j have different bit lengths. But we can think of i and j as integers, and we can take their binary representations of length max(N,M) but they have different lengths. Actually we need the AND operation to be defined on bits; we can consider i and j in binary with sufficient bits, but i has up to N bits, j up to M bits. The AND of i and j (as integers) yields bits where both have 1. The parity of popcount of that is well-defined. However, does this produce a matrix where each row (except row 0) has