We need to parse: "places two chessmen on two different empty cells, the Manhattan distance between which is exactly 3." So each move places two pieces simultaneously, and they must be distinct empty cells, distance exactly 3. He repeats this, each time placing two pieces, until no more pairs of empty cells with Manhattan distance 3 remain (or he can choose any order). We want maximum number of chessmen placed i.e., maximum number of pieces that can be placed on board via such moves, given that each move adds exactly two pieces, and they must satisfy distance exactly 3, and cannot reuse cells that already have a piece. We can choose the sequence to maximize total pieces. Equivalent to: what's the maximum size of a set of cells that can be partitioned into pairs such that each pair has Manhattan distance exactly 3? Because we can place them pairwise in that order. But careful: The process is dynamic: we need to be able to sequentially add pairs, each using two empty cells. The order may matter if earlier placements block later pairs. However, if we can find a set of cells that can be partitioned into pairs of distance 3, then we can place them in that order (since all cells initially empty, we pick a pair, place, etc.). However, might there be configurations where you can place more pieces than the maximum size of a pairwise matching? Possibly you could place pieces such that not all placed pieces are paired among themselves? But each move places two pieces, so total placed is even. The final set of occupied cells is the union of all these pairs. Could there be a scenario where you cannot partition final occupied set into pairs of distance 3, but you could still place them because intermediate placements allowed? For example, you could place a pair, then later use one of those pieces with a new empty cell to form a distance-3 pair? No, because each move places two pieces simultaneously, both must be empty at that moment. So once a cell is occupied, it cannot be used again. And you cannot place a piece on an occupied cell. So after placement, the two cells become occupied. In later moves, you cannot involve those cells. So the set of occupied cells at the end is exactly the union of the pairs placed. Each pair consists of two cells with Manhattan distance 3. The pairs are disjoint. So the final set can be partitioned into pairs (the pairs used). So the maximum number of pieces placed is twice the maximum number of disjoint pairs of cells with Manhattan distance exactly 3 that can be placed sequentially. However, is it always possible to achieve any matching? Since you choose order, but as long as the pairs are disjoint, you can place them in any order, because all cells are empty initially. So the problem reduces to: find the maximum number of disjoint pairs of cells (i.e., maximum matching) on an n x m grid where each pair must have Manhattan distance exactly 3. Since the grid is bipartite? Actually, Manhattan distance parity: For two cells, Manhattan distance parity is same as (x1+y1)+(x2+y2) mod 2? Actually, |dx|+|dy| mod 2 = (dx+dy) mod 2? Since |dx|+|dy| ≡ dx+dy (mod 2) because parity of absolute value sum is same as sum of absolute values? Actually, careful: |dx|+|dy| mod 2 = (dx mod 2) XOR? Let's compute: dx mod 2 can be 0 or 1; |dx| mod 2 = dx mod 2 if dx is integer. So parity of distance = (dx+dy) mod 2. Because |dx| ≡ dx (mod 2) since sign doesn't matter: -1 mod 2 = 1? In modulo 2, -1 ≡ 1. Indeed, parity of |dx| is same as parity of dx because if dx is odd, |dx| is odd; if even, even. So parity of sum = (|dx|+|dy|) mod 2 = (dx mod 2 + dy mod 2) mod 2 = (dx+dy) mod 2. So Manhattan distance parity is same as parity of (dx+dy). But note that (x1+y1) and (x2+y2) have opposite parity if and only if (dx+dy) is odd? Actually, (x1+y1) - (x2+y2) = dx+dy. So parity difference of sum coordinates equals parity of distance. So distance odd => sums have opposite parity; distance even => sums have same parity.
Since required distance is exactly 3 (odd), the two cells must have opposite parity: one black, one white if we color chessboard in checkerboard style (i.e., (x+y) even vs odd). So pairs always cross colors.
Thus we can think of it as a bipartite graph between black cells and white cells, with an edge if Manhattan distance = 3. We need maximum matching in this bipartite graph. Since the graph is huge (n,m up to 1e9), we need a formula.
But note: The grid is not necessarily bipartite complete; edges are specific.
We need maximum number of disjoint pairs, i.e., maximum matching. Since the graph is bipartite, maximum matching ≤ min(#black, #white). But we also have structural constraints.
Goal: Output the maximum number of chessmen, which is 2 * size of maximum matching.
We need to compute maximum matching size M, then answer = 2M.
Alternatively, we can think of covering the board with domino-like shapes? But dominoes cover adjacent cells distance 1; here distance 3 shapes.
We need to pair cells that are at distance exactly 3. That includes many possibilities: (dx,dy) such that |dx|+|dy|=3. The possibilities: (3,0), (0,3), (2,1), (1,2), and negative signs. So basically, moves like 3 steps horizontally, or 3 vertically, or L-shaped (2+1) like knight moves? Actually, Manhattan distance 3 includes (2,1) which is like a knight move but not exactly because knight move is sqrt(5) Euclidean but Manhattan distance 3 includes (2,1) which is 2 steps in one direction and 1 step orthogonal. So it's like a "Chebyshev"? So it's all cells that are exactly 3 steps away in grid.
Thus, the graph is essentially: each cell connected to cells that are 3 away.
We need maximum matching in this graph on a rectangular grid.
Because n,m can be up to 1e9, we need a closed-form solution.
We can try to derive a formula by considering tiling patterns. Possibly the maximum number is floor(nm/ something)? But since each move uses two cells, the maximum possible pieces placed is even and ≤ nm. But sometimes can't fill all cells because of parity and constraints.
Observations: Because distance is odd, matching must pair opposite colors. So maximum matching cannot exceed the number of cells of the minority color. So M ≤ min(B, W) where B = number of black cells, W = number of white cells. For an n x m grid, if nm is even, B = W = nm/2; if odd, one color has one extra cell. So min = floor(nm/2). So maximum pieces ≤ 2 * floor(nm/2) = even floor down to nm if even, else nm - 1.
But can we always achieve that? Probably not. For small boards, like 2x2, we saw answer 0. So need more nuanced.
Let's try small boards to see patterns.
We need to compute M for given n,m.
Given n,m up to 1e9, likely the answer is something like: if n*m is small, special cases; else formula.
Maybe we can think of a tiling with a pattern that pairs all but at most a few cells.
Since Manhattan distance 3 includes many moves, perhaps we can almost fill the board.
Consider a 1D line (n=1, m=M). Actually board is 2D but maybe we can reduce to 1D? Let's analyze.
We need to pair cells in a grid. This resembles a problem from Codeforces: "Little C loves 3" maybe from Codeforces Round? I recall a problem: "Little C Loves 3 I" or something? Actually there is a Codeforces problem: "Little Pony and Harmony Chest"? Not that.
But I think I've seen a problem: "Little C loves 3" with n,m up to 10^9. Possibly the answer is something like: if n==1 or m==1, special; else formula.
Let's test manually.
Case n=1, m arbitrary. Then grid is a 1-row. Manhattan distance between (1,i) and (1,j) = |i-j|. So we need pairs with distance exactly 3. So essentially, we need to pair cells i and i+3. That's like a matching on a line graph with edges connecting i to i+3. Maximum number of disjoint edges? This is like a graph where vertices are positions 1..m, edges between i and i+3 (if both exist). We want maximum matching. This can be computed: we can greedily pair from left to right? Since edges are length 3, they don't overlap if we pair (1,4), then (5,8) etc? But careful: edges (i,i+3) are disjoint if they don't share vertices. Overlap possible if edges share a vertex. For example, (1,4) and (4,7) share vertex 4. So we must choose non-overlapping edges.
We want maximum number of edges. This is like a maximum matching in a path graph with edges only between vertices of distance 3? Actually, edges exist only between vertices that are exactly 3 apart; not consecutive. So it's not a standard path matching.
Let's compute small m:
m=1: 0
m=2: 0 (distance <3)
m=3: can pair (1,4)? but m=3 so no; actually distance 3 requires i and i+3, so need at least 4 cells? For m=3, positions 1,2,3, no pair distance 3. So 0.
m=4: can pair (1,4) => 2 pieces. Also maybe (2,5) no. So maximum 1 pair => 2 pieces.
m=5: positions 1..5. Can we get 2 pairs? Pairs must be disjoint. Options: (1,4) and (2,5)? But (1,4) uses 1,4; (2,5) uses 2,5 => disjoint? Yes, they are disjoint. So we can have 2 pairs => 4 pieces. Also (1,4) and (5,?) no. So max 2? Check (2,5) works. Also (1,4) and (2,5) both exist? Distance: |2-5|=3 yes. So 2 pairs, uses all cells except maybe 3. So 4 pieces out of 5 cells. So answer 4.
m=6: cells 1..6. Can we get 3 pairs? Pair (1,4), (2,5), (3,6) all disjoint? (1,4) uses 1,4; (2,5) uses 2,5; (3,6) uses 3,6 => all 6 cells used. So 3 pairs => 6 pieces. So m=6 => 6.
m=7: cells 1..7. Maximum? Possibly 2 pairs? Let's try to cover as many as possible. Can we get 3 pairs? 3 pairs would use 6 cells. Need disjoint pairs each distance 3. Consider pairs: (1,4), (2,5), (3,6) uses 1,2,3,4,5,6 -> leaves 7. Could also try (4,7) but 4 used. (5,8) no. (1,4) and (5,8) no. So 3 pairs possible? Actually (1,4),(2,5),(3,6) are all valid and disjoint, so 3 pairs => 6 pieces. Can we get a 4th pair? Would need 8 cells. So max 3 pairs => 6 pieces. But could we get 7? no because odd. So 6.
m=8: can we get 4 pairs? (1,4),(2,5),(3,6),(4,7) conflict because 4 used twice. Alternative: (1,4),(2,5),(3,6),(7,10?) no. Maybe (1,4),(5,8) uses 1,4,5,8 => 2 pairs, but we want more. Try (2,5),(6,9?) no. Let's systematically compute max matching on line with edges of step 3.
Graph: vertices 1..m. Edges: (i, i+3) for i=1..m-3. This is a disjoint union of three independent paths? Because modulo 3 classes: vertices with index mod 3 are connected? Actually edges connect i to i+3, so it's like three chains: class mod 3: vertices with same residue are connected via edges? Let's consider residues mod 3. If we take vertices with residue r, they are spaced by 3, and edges connect consecutive ones within that residue? Actually edge (i,i+3) connects a vertex of residue i mod 3 to vertex of residue (i+3) mod 3 = same residue. So edges only connect vertices of the same residue class? Check: i mod 3 = r, then i+3 mod 3 = r. So edges connect vertices within the same residue class? But wait, vertices i and i+3 have the same residue mod 3. But there are no edges connecting different residues? Actually (i,i+3) connects two vertices both with residue i mod 3. So the graph is composed of three disjoint subgraphs, each consisting of vertices of a particular residue class, arranged in a path order. For each residue r (0,1,2 mod 3, but adjust for 1-index), vertices: r, r+3, r+6,... up to ≤ m. Within each such set, edges exist between consecutive vertices (since (r+3k, r+3(k+1)) = distance 3). So each subgraph is a path (linear) where vertices are in order, and edges connect consecutive vertices in that path. So maximum matching in each path is floor(L/2) where L is number of vertices in that path (since it's a simple path graph). Because on a path, maximum matching = floor(number of vertices / 2). But careful: In each path, edges are only between consecutive vertices in that chain, i.e., between v_k and v_{k+1}. That's exactly a path. So the maximum number of disjoint edges in that path is floor(L/2). And these matchings across different residues are independent. So total maximum matching = sum_{r} floor(L_r/2). Then total pieces = 2 * that.
Now L_r = number of integers i in [1,m] with i ≡ r (mod 3). For m, with residues 1,2,0 (if we define residue mod 3 from 1). Let's define residues 0,1,2 corresponding to i mod 3 = 0,1,2 but careful with 1-index. Let's use 1-index: i mod 3 can be 1,2,0 (with 0 representing 3 mod). Simpler: L_k = ceil((m - k)/3) + 1? Actually count of numbers from 1 to m congruent to a given r (1,2,0). Let's do: For r = 1, count = ⌈m/3⌉? For m=1: 1 =>1; m=2:1,2 =>1; m=3:1,2,3 => residues 1,2,0 => count of 1 =1; count of 2=1; count of 0=1. For m=4: 1,2,3,4 => residues:1,2,0,1 => count of 1=2, 2=1, 0=1. So formula: count of residue r = floor((m - r)/3) + 1 if r ≤ m else 0. But easier: each residue appears either ⌊m/3⌋ or ⌈m/3⌉. Specifically, let q = m // 3, rem = m % 3. Then counts: for residues 1,2,0 (or 1,2,3 mod 3) in order: if rem=0: each count = q; if rem=1: residue 1 count = q+1, others q; if rem=2: residues 1 and 2 count = q+1, residue 0 count = q. For 1-index, residue 0 corresponds to multiples of 3. So counts: for classes mod 3 (1,2,0) we have: (q+1, q, q) for rem=1? Actually check m=4: q=1, rem=1 => residues: 1: q+1=2, 2: q=1, 0: q=1? But 0 means numbers divisible by 3: 3 only =>1 correct. So yes: for rem=1: counts: class1 = q+1, class2 = q, class0 = q. For rem=2: m=5: q=1, rem=2 => residues: 1:2? Actually numbers 1,2,3,4,5: residues 1,2,0,1,2 => class1: 1,4 =>2, class2:2,5=>2, class0:3=>1. So class1=q+1? q=1 =>2 correct; class2=q+1=2; class0=q=1. So yes.
Thus max matching for 1 x m: M = floor(L1/2)+floor(L2/2)+floor(L0/2). Then pieces = 2M. Let's test with earlier:
m=4: counts: q=1, rem=1 -> L1=2, L2=1, L0=1. floors: 1,0,0 => M=1 => pieces=2 matches.
m=5: q=1, rem=2 -> L1=2, L2=2, L0=1. floors:1,1,0 => M=2 => pieces=4 matches.
m=6: q=2, rem=0 -> L1=2, L2=2, L0=2. floors:1,1,1 => M=3 => pieces=6 matches.
m=7: q=2, rem=1 -> L1=3, L2=2, L0=2. floors: floor(3/2)=1,1,1 => M=3 => pieces=6 matches.
m=8: q=2, rem=2 -> L1=3, L2=3, L0=2. floors:1,1,1 => M=3 => pieces=6? But earlier we thought maybe 4 pairs? Let's compute: m=8, vertices 1..8. Can we achieve 4 pairs? That would be 8 pieces using all cells. Is that possible? We need to partition into 4 pairs each distance 3. Let's try to find: pairs must be (i,i+3). Because only possible. So we need to cover all 8 numbers with such edges, disjoint. That's like a perfect matching in this graph. Is there a perfect matching? We need to check if sum of floors(L_r/2) =? L1=3 (residue1: 1,4,7), L2=3 (2,5,8), L0=2 (3,6). Max matching: In residue1 path: vertices 1-4-7 (edges between 1-4 and 4-7). Maximum matching in a path of length 3 (3 vertices) is floor(3/2)=1 (can take either edge (1,4) or (4,7) but not both because they share vertex 4). So max 1 from that chain. Similarly residue2 path: 2-5-8, max 1. Residue0 path: 3-6, max 1 (edge 3-6). So total 3 edges. So indeed maximum is 3, not 4. Could there be a matching that uses edges across different residues? No, because edges only within same residue class as argued? Let's verify: Edge (i,i+3) always has both i and i+3 same residue mod 3. So indeed edges are only within same residue class. So matching cannot pair vertices of different residues. Therefore our decomposition is accurate. So for 1xn, answer = 2 * ( floor(L1/2)+floor(L2/2)+floor(L0/2) ). That equals 2 * ( ⌊count1/2⌋ + ⌊count2/2⌋ + ⌊count3/2⌋ ).
Simplify? Possibly answer = 2 * ⌊m/3⌋ * something? Let's derive closed form. For each residue class, floor(L/2) essentially L // 2. So total pairs = sum (L_r // 2). So pieces = 2 * sum (L_r // 2). That is not simply m - something. For m=8, that gives 6, so uncovered cells = 2.
But is this maximum for 1D always achievable? Yes, because the graph is a disjoint union of paths; maximum matching on a path is floor(L/2) and can be realized by taking every other edge; also we can choose edges independently across components. So the maximum matching is exactly sum floor(L_i/2). So that's correct.
Thus for n=1, answer = 2 * ( ⌊cnt1/2⌋+⌊cnt2/2⌋+⌊cnt0/2⌋ ) where cnt's are counts of residues mod3 from 1..m. That simplifies to? Possibly answer = 2 * ( ⌊m/3⌋ * something?) Let's compute directly.
Now for 2D, the graph is more complex because there are edges connecting cells not only horizontally/vertically but also diagonally? Actually Manhattan distance 3 includes moves like (2,1) which changes both row and column, crossing residues mod something? But we can analyze parity and maybe decompose into a bipartite graph that can be tiled.
Given the huge constraints, the answer likely has a simple formula based on n,m. Let's try to compute for small boards manually or via reasoning to infer pattern.
I recall a Codeforces problem: "C. Little Pony and Summer Sun Celebration"? Not.
Maybe it's from Codeforces Round #511? There's a problem "Little C Loves 3 II". Actually I remember a problem named "Little C Loves 3 II" from Codeforces Round #511 (Div. 2) or something. Let me think: There is a problem "A. Little C Loves 3 I" which is about splitting a number into three non-multiple of 3. And "B. Little C Loves 3 II" is about placing dominoes-like with distance 3 on an n x m board. Yes! I recall: Codeforces problem 1047C? Wait. Actually I think it's Codeforces Round #1047? Let's search memory: There is a problem named "Little C Loves 3 II" (CF 1045B? Not sure). But I recall a problem: "Little C Loves 3 II" where you have an infinite chessboard? Wait, maybe I'm confusing with "Little Pony and Harmony Chest".
Nevertheless, I think it's a known problem. The solution likely involves analyzing small boards and deriving formula for large ones.
Let me try to reason.
We need maximum matching on grid graph G where vertices are cells, edges between cells with Manhattan distance 3.
We can think of coloring the board with a pattern more refined than just black-white. Because distance 3 imposes restrictions mod something.
Observation: Manhattan distance 3 moves are like: (3,0), (0,3), (2,1), (1,2), and negatives. So if we consider coordinates modulo something, we might partition cells into groups such that edges only connect certain groups.
Consider coloring by (x mod something, y mod something). Since dx+dy = 3, mod 2 we already have parity cross. More granular mod? Let's examine mod 3 perhaps because of the distance 3? For moves (3,0) changes x by 3, so x mod 3 changes? Actually moving by (3,0) doesn't change x mod 3, but changes y mod? y unchanged. So both endpoints have same x mod 3, same y mod 3? Actually (3,0): x changes by 3, so x mod 3 remains same; y unchanged, so y mod 3 same. So (3,0) preserves (x mod 3, y mod 3). For (0,3) preserves as well. For (2,1): x changes by ±2, y by ±1. Mod 3, 2 ≡ -1, 1 ≡ 1. So (x mod 3, y mod 3) changes? If we add (2,1): (x+2, y+1). Then (x+2) mod 3 = (x mod 3)+2 mod3; (y+1) mod 3 = (y mod 3)+1 mod3. So it changes both coordinates mod 3. So not necessarily same residue. However, note that the sum (x+y) mod something: For (3,0): x+y changes by 3, so (x+y) mod 3 unchanged. For (0,3): same. For (2,1): change by 3, so (x+y) mod 3 unchanged as well. Indeed, |dx|+|dy|=3, so dx+dy could be ±3 or ±1? Actually dx and dy can be positive or negative combinations, but the sum of absolute values is 3, but the signed sum dx+dy could be -3, -1, 1, 3? Wait: For Manhattan distance, we consider absolute values. But the actual signed difference might vary. However, note that the parity of (x1+y1) and (x2+y2) differs because distance is odd. Actually earlier we argued distance odd => (x+y) parity opposite. But mod 3? Since distance = 3, the sum (x+y) changes by (dx+dy) which may not be 3 if signs differ. For example, moving from (x,y) to (x+2, y-1): dx=2, dy=-1, Manhattan distance = 3, but dx+dy = 1, which mod 3 is 1, not 0. So (x+y) mod 3 changes by 1 mod 3. So not invariant. But maybe there's a coloring with 4 colors or something.
Alternate approach: Model as bipartite graph with black/white parity. We need maximum matching. Could it be that the graph is regular enough that maximum matching equals min(#black, #white) except for some small boards? Let's test small cases.
We have sample: n=2,m=2 => answer 0. Here black and white counts: 2 each? Actually 2x2 board has 2 black, 2 white (if top-left black). min = 2, so maximum matching could be at most 2 pairs? But answer 0 suggests no edges exist. Indeed, distance between any two cells? In 2x2, max distance is 2 (from (1,1) to (2,2) = 2). So no pair distance 3, so matching size 0.
n=3,m=3 => total cells 9. Black? Standard coloring: (1,1) black? Let's assume (1,1) black. Then black count = 5? Actually 3x3: (1,1) black, (1,2) white, (1,3) black; (2,1) white, (2,2) black, (2,3) white; (3,1) black, (3,2) white, (3,3) black. So blacks = 5, whites = 4. So min = 4. Maximum pieces possible = 8 as output, meaning matching size = 4, i.e., we matched all whites with blacks. So maximum matching = 4, achieving the upper bound min(B,W) = 4. So it's perfect matching for the minority color.
Thus for 3x3, we can match all whites.
Now test other small boards manually or by reasoning to see if maximum matching is typically floor(n*m/2) except when constraints prevent.
But 2x2 had 0 < 2 (min=2). So there are restrictions.
Let's enumerate small n,m to guess pattern.
We'll try to compute maximum matching for small n,m (n,m up to maybe 5) by reasoning or known solutions from the problem.
I recall that Codeforces problem "Little C Loves 3 II" has answer:
- If min(n,m)=1, answer = (max/3 * 2) * 1? Actually maybe it's something like: if n=1 and m=1 =>0; if n=1 and m=2 =>0; n=1,m=3=>0; n=1,m=4=>2; n=1,m=5=>4; n=1,m=6=>6; n=1,m=7=>6; n=1,m=8=>6; n=1,m=9=>8? Let's compute using our 1D formula:
For m=9: q=3, rem=0 => L1=3, L2=3, L0=3 => floors:1,1,1 => M=3 => pieces=6. Wait our earlier for m=9 would be 6? But maybe there is also possibility of vertical moves? But n=1, only horizontal. So max pieces = 6. But is there any possibility of using 2D moves? No. So answer for 1x9 should be 6. However, from memory of the CF problem, I think they gave a formula for n=1 or 2. Actually I recall that in "Little C Loves 3 II", the board is n x m, and they wanted maximum number of dominos? Not exactly. Let's try to search memory: There is a problem named "Little C Loves 3 II" from CF Round #1045? Wait, I'll try to recall: I think it's problem 1047C? That's "Little C Loves 3 I" about splitting numbers. The next problem "Little C Loves 3 II" is about a game with distance 3. I think the solution involved many caseworks for small dimensions and then a formula for large dimensions: when both n,m >= something, answer = nm if nm even else n*m -1? But 3x3 gave 8 (which is 9-1), so that fits: if both >=? But 2x2 gave 0, not 3 (since 4 is even, would be 4? but it's 0). So not that simple.
Maybe the answer is nm if nm even, except when min(n,m) = 1 or 2? Let's test: 1x4 would be 2, but n*m=4 even, would give 4? but actual 1x4 we computed 2. So not.
Alternatively, maybe answer = floor(n*m/2)*2? That's trivial. So need specific.
Given the complexity, it's likely we need to compute maximum matching by considering the grid as a bipartite graph and using a theorem about matchings in grid graphs with moves of distance 3. Possibly it can be transformed into a flow? Not for 1e9.
But maybe we can tile the board with patterns of pairs. Since moves are of Manhattan distance 3, we can try to cover the board with "3-steps" moves. Could there be a periodic tiling that pairs almost all cells? For large boards, maybe we can achieve almost all cells except possibly a few when dimensions are small or mod something.
Let's think about constructing a matching that covers all but at most a constant number of cells. For bipartite graph, if the graph is regular enough and has a perfect matching for even total vertices, maybe we can achieve min(B,W). For many grids, min(B,W) = floor(n*m/2). So the question reduces to: when does a perfect matching (or near-perfect) exist?
So we need to know for which n,m we can match all cells of the minority color (i.e., all white if #white <= #black). That would give answer = 2 * min(B,W) = if nm even, = nm; if odd, = n*m - 1.
Thus answer would be either nm (if even) or nm - 1 (if odd) when such matching exists. But examples: 2x2 even, nm=4, but answer 0, not 4. So 2x2 fails. 3x3 odd, nm-1=8, matches. 1x4 even, n*m=4, but answer 2, not 4. So fails. So existence depends on n,m.
Thus we need to find for which n,m we can achieve full matching (i.e., covering all cells of the minority color), and if not, what's the maximum.
Given large dimensions, maybe the only obstructions are when one dimension is 1 or 2, or small values, and for larger dimensions (both >= something like 3?) it's possible to achieve full matching? But 3x2? Let's test 2x3 (n=2,m=3). Total cells 6, even. Can we place 6 pieces? That would require 3 pairs. Let's see if possible. Coordinates: rows 1-2, cols 1-3. Compute Manhattan distances: we need pairs with distance 3. List all pairs distance 3:
(1,1)-(1,4) no col4; (1,1)-(2,2): |1-2|+|1-2|=2? 1+1=2, not 3. (1,1)-(2,3): |1-2|+|1-3|=1+2=3 yes. So (1,1)-(2,3) distance 3.
(1,1)-(1,4) no; (1,1)-(4,1) no.
(1,2)-(2,1): |1-2|+|2-1|=1+1=2 no. (1,2)-(2,4) no. (1,2)-(2,0) no. (1,2)-(2,2): 1? No. (1,2)-(1,5) no. (1,2)-(2,? ) maybe (2,? ) with col? (1,2)-(2,4) no; (1,2)-(2,0) no. (1,2)-(2,? ) with difference: need dx=1,dy=2 or dx=2,dy=1 etc. (1,2)-(2,4) would be dx=1,dy=2 ->3, but col4 out. (1,2)-(2,0) dx=1,dy=2 ->3 but col0 out. (1,2)-(2,5) no. (1,2)-(3,1) row3 out. (1,2)-(3,3) row3 out. So maybe only some.
(1,3)-(2,1): dx=1,dy=2 ->3, yes (1,3)-(2,1).
(1,3)-(2,2): dx=1,dy=1=2 no.
(1,3)-(2,4) no.
(1,2)-(2,? ) also (1,2)-(2,? ) need dy = 2? Actually dx = |1-2|=1, dy = |2-?| must be 2 so ? = 0 or 4 out; or dx=2? impossible because rows only 2. So only vertical/horizontal moves of length 3: horizontal 3 steps: (1,1)-(1,4) no; (1,2)-(1,5) no; (1,3)-(1,6) no; vertical 3 steps: (1,1)-(4,1) no; (2,1)-(5,1) no; etc. So only diagonal-like moves: (dx=1,dy=2) or (dx=2,dy=1). With only 2 rows, dx max 1, so dx=1,dy=2 is only possibility. So edges exist between (1,col) and (2,col+2) if col+2 <=3? col+2 <=3 => col=1 => (1,1)-(2,3); also between (1,col) and (2,col-2) if col-2 >=1 => col=3 => (1,3)-(2,1). Similarly from row2 to row1 same. Also maybe (1,col) and (2,col+2) for col=1; (1,col) and (2,col-2) for col=3. Also (2,col) similarly. So we have two edges: (1,1)-(2,3) and (1,3)-(2,1). Also maybe (1,2) and (2,? ) none. So graph has 4 vertices involved: (1,1), (2,3), (1,3), (2,1). These are four distinct cells. They form two disjoint edges? Actually (1,1)-(2,3) and (1,3)-(2,1) are disjoint (no shared vertices). So we can place these two pairs, covering all 4 cells. That uses 4 pieces. The remaining cells are (1,2) and (2,2). Can they be paired? distance between (1,2) and (2,2) =1, not 3; (1,2)-(2,? ) none. So cannot pair them. So maximum pairs = 2 => 4 pieces. Could we do better than 2 pairs? Maybe a different pairing? The only edges are those two. So maximum matching size = 2, so pieces =4. So for 2x3, answer = 4. n*m=6, min(B,W)=? For 2x3, blacks? (1,1) black, (1,2) white, (1,3) black; (2,1) white, (2,2) black, (2,3) white. So blacks=3, whites=3. min=3, so perfect matching would be 3 pairs =6 pieces, but we only got 2 pairs =4. So not full.
Thus 2x3 gives answer 4.
What about 2x4? n=2,m=4 total 8. Let's compute possible edges. With rows=2, dx=1 or 0? Actually dx can be 0 or 1? Since rows only 1 and 2, dx = 0 if both in same row, dx = 1 if in different rows. Manhattan distance = |dx|+|dy| = 3. So possibilities:
- dx=0, then |dy|=3 => horizontal distance 3. So two cells in same row with columns differ by 3. So for row1: (1,1)-(1,4) is an edge. Similarly (1,2)-(1,5) no; for row1 only (1,1)-(1,4). For row2: (2,1)-(2,4).
- dx=1, then |dy|=2 => vertical difference 1 and column difference 2. So pairs between row1 and row2 with columns differ by 2: (1,c)-(2,c+2) if c+2<=4 => c=1,2? Actually c=1 gives (1,1)-(2,3); c=2 gives (1,2)-(2,4); also (1,c)-(2,c-2) if c-2>=1 => c=3,4? (1,3)-(2,1); (1,4)-(2,2). So these four edges.
So total possible edges: horizontal: (1,1)-(1,4), (2,1)-(2,4). Diagonal: (1,1)-(2,3), (1,2)-(2,4), (1,3)-(2,1), (1,4)-(2,2). Let's list all vertices: cells (1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4). We need maximum matching.
Can we achieve a perfect matching (4 pairs)? Let's try. We want to cover all 8 cells with disjoint edges.
Try to find 4 disjoint edges.
Consider using the two horizontal edges: (1,1)-(1,4) and (2,1)-(2,4). These use (1,1),(1,4),(2,1),(2,4). Then remaining cells: (1,2),(1,3),(2,2),(2,3). Can we pair these? Options: (1,2)-(2,4) but (2,4) used; (1,2)-(2,4) no; (1,2)-(2,0) no; (1,2)-(1,5) no; (1,2)-(2,?) maybe (2,? ) need distance 3: dx=1, dy=2 => (1,2)-(2,4) already used; (1,2)-(2,0) invalid; dx=0, dy=3 => (1,2)-(1,5) invalid; dx= -? So no. (1,3)-(2,1) but (2,1) used; (1,3)-(2,5) no; (1,3)-(2,1) used; (1,3)-(2,1) already used; (2,2)-(1,4) used? (1,4) used; (2,2)-(1,0) no; (2,2)-(1,4) used; (2,3)-(1,1) used; etc. So no pairing among remaining. So that matching fails to cover all.
Try using diagonal edges primarily. Perhaps we can cover all.
Let's try to construct a perfect matching manually.
We need each vertex matched. Let's think of bipartite: rows are only 2, so it's a bipartite graph between row1 and row2? Actually black-white parity also, but we can consider rows.
Let's denote set A = row1 cells, set B = row2 cells. Edges exist as described. Actually edges also within same row (row1-row1 or row2-row2). So it's not bipartite between rows; it's general graph. But we can still search.
We can try to use some same-row edges and cross-row edges.
Since total vertices 8, even.
We need 4 disjoint edges.
Let's enumerate all edges more systematically:
Same row1: only (1,1)-(1,4) because columns differ by 3.
Same row2: (2,1)-(2,4).
Cross row: (1,1)-(2,3), (1,2)-(2,4), (1,3)-(2,1), (1,4)-(2,2).
That's the entire edge set.
Observe that the graph is symmetric. Let's attempt to find a perfect matching.
If we use cross edge (1,1)-(2,3), then we cover (1,1) and (2,3). Then remaining: (1,2),(1,3),(1,4),(2,1),(2,2),(2,4). Now edges among these:
- (1,2)-(2,4) still available.
- (1,3)-(2,1) available.
- (1,4)-(2,2) available.
- Also (1,1)-(1,4) not because (1,1) used.
- (2,1)-(2,4) is same-row, both unused currently.
Now we need to match the remaining 6 vertices into 3 disjoint edges. Could we take (1,2)-(2,4), (1,3)-(2,1), (1,4)-(2,2). That would be three cross edges, all disjoint? Check: (1,2)-(2,4) uses (1,2) and (2,4); (1,3)-(2,1) uses (1,3) and (2,1); (1,4)-(2,2) uses (1,4) and (2,2). All distinct. Combined with earlier (1,1)-(2,3), we have all 8 vertices covered: (1,1),(2,3),(1,2),(2,4),(1,3),(2,1),(1,4),(2,2). Yes, all covered. Check duplicates: (2,3) is used only in first; (2,4) used in second; (2,1) third; (2,2) fourth. All distinct. So this is a perfect matching! Therefore 2x4 can achieve 4 pairs = 8 pieces. So answer for 2x4 would be 8.
Thus 2x4 achieves full coverage (n*m=8 even). Good.
Now test 2x5: total cells 10. Can we achieve 10? Possibly max 10? Let's analyze.
We'll need to see pattern for 2 x m.
For n=2, general m. Since rows only 2, edges types: same-row horizontal (distance 3 horizontally): (1,c)-(1,c+3) and (2,c)-(2,c+3). Cross-row: (1,c)-(2,c+2) and (1,c)-(2,c-2) (for appropriate columns). Also note cross-row also could have dx=1, dy=2, so that covers all. So graph as before.
We need maximum matching. This is a 1D-like problem but with two rows and cross connections. Maybe we can derive formula.
Given that 2x3 gave 4 (max 6? Actually 2x3 total 6, we got 4, so 2 uncovered). 2x4 gave 8 (full). 2x5? Let's try to compute manually or derive pattern.
We can attempt to model as a graph on columns? Since rows limited, we can treat each column as having two cells: top and bottom. Edges connect cells either in same column? No, distance 3 implies either horizontal moves (same row, columns differ by 3) or L-shaped moves (row diff 1, column diff 2). So edges connect cells possibly in different rows and columns.
We can attempt to compute maximum matching via DP for small m to guess formula, then generalize.
But since n,m up to 1e9, there must be simple formula based on m mod something for n=2.
Similarly for n=1, we already derived formula (which is not trivial but can be expressed simply maybe as 2 * floor( (2m)/3 )? Let's check: For m=4, 2floor(24/3)=2floor(8/3)=2*2=4, but actual answer is 2. Not that.
Our derived formula for n=1 gave pieces = 2 * sum_{r} floor(L_r/2). For m=4, L1=2, L2=1, L0=1 => floors=1,0,0 => total pairs=1 => pieces=2. So that is 2. So not a simple linear in m.
Maybe there's a known solution: For 1xn, answer = 2 * (m // 3 * 2? Actually m=6 gave 6, m=7 gave 6, m=8 gave 6, m=9 gave 6? Let's compute for m=9: counts: m=9 => q=3 rem=0 => L1=3, L2=3, L0=3 => floors:1,1,1 => total pairs=3 => pieces=6. So from m=6 to m=9, pieces=6 constant? Let's compute m=10: q=3 rem=1 => L1=4, L2=3, L0=3 => floors:2,1,1 => total pairs=4 => pieces=8. So it increases. So pattern: piece count increments by 2 every 3 m? Actually m=6-> pieces=6; m=7->6; m=8->6; m=9->6; m=10->8; That's like floor( (2m)/3 )? For m=6, floor(12/3)=4 => times 2? Actually floor(2m/3) = floor(12/3)=4, but pieces=6 => 2*? Not match. Maybe pieces = 2 * floor(2m/3)? For m=6: floor(12/3)=4 => 24=8, not 6. So no.
Our formula pieces = 2 * (⌊L1/2⌋+⌊L2/2⌋+⌊L3/2⌋). Where L1, L2, L3 roughly m/3 each. The sum of floors is roughly floor(m/2)? For m large, each L_i ≈ m/3, floor(L_i/2) ≈ m/6, sum ≈ m/2, so pieces ≈ m. Indeed, for large m, maximum pieces approach m (since we can cover almost all cells). For m=100, pieces would be around 100? Actually maximum pieces cannot exceed m (since n=1, total cells = m). So asymptotically we can cover nearly all cells? For m=9, pieces=6, coverage 6/9=66%; for m=10, pieces=8, 80%; for m=11? Let's compute m=11: q=3 rem=2 => L1=4, L2=4, L0=3? Actually rem=2: L1=4, L2=4, L0=3? Wait m=11: q=3, rem=2 => counts: residues 1: q+1=4, 2: q+1=4, 0: q=3 => floors:2,2,1 => total pairs=5 => pieces=10. So pieces=10, full coverage (since m=11 odd, max possible is 10). So for m=11, we can cover all but 1 cell. That's good.
So for 1D, as m grows, we can cover almost all cells except maybe a few when m mod 6? Actually pattern: m=6:6 pieces (full); m=7:6 (one leftover); m=8:6 (two left? Actually 8-6=2 left); m=9:6 (3 left); m=10:8 (2 left); m=11:10 (1 left); m=12:? Let's compute m=12: q=4 rem=0 => L1=4, L2=4, L0=4 => floors:2,2,2 => total pairs=6 => pieces=12 (full). So for m multiple of 6 and 12? Actually 6 gave 6 (full), 12 gave 12 (full). So 1D achieves full coverage when m divisible by 6? 6 and 12 both divisible by 6, also m=0 mod? Check m=3:0 not full; m=9: not full (6/9) but 9 not divisible by 6. So pattern might be: when m mod 6 = 0,1,? Let's test m=4 mod6:4 -> pieces=2, leftover 2; m=5->4 leftover1; m=6->6 full; m=7->6 leftover1; m=8->6 leftover2; m=9->6 leftover3; m=10->8 leftover2; m=11->10 leftover1; m=12->12 full. So maximum coverage (pieces) = m if m mod 6 = 0 or 2? Actually m=12 mod6=0 full; m=6 mod6=0 full; m=... m=2 mod6: m=2 -> pieces=0; m=8 mod6=2 -> pieces=6, not full; so not. m=3 mod6: m=3->0; m=9 mod6=3->6, not full; m=4 mod6: m=4->2; m=10 mod6=4->8, not full; m=5 mod6: m=5->4; m=11 mod6=5->10, not full; m=1 mod6: m=1->0; m=7 mod6=1->6, not full. So full coverage occurs at m=6,12,... maybe also m=0 mod 6. Possibly also m=2? No. So seems only multiples of 6 yield perfect matching for 1xn? Check m=18? Likely yes.
But our derived formula is exact, we can use for n=1.
Now for n=2, we saw m=3 gives 4 (max possible 6 => not full), m=4 gives 8 (full), m=5? Let's compute 2x5. We'll try to find maximum matching.
We can model as graph with vertices in two rows. We'll attempt to compute via reasoning or known results. Could be similar to 1D but with two rows, maybe we can achieve full coverage for m not small.
Let's try to construct for 2x5.
Cells: Row1: (1,1)-(1,5); Row2: (2,1)-(2,5). Total 10 cells.
List edges:
- Same row horizontal: (1,c)-(1,c+3) for c=1,2: (1,1)-(1,4), (1,2)-(1,5). Similarly row2: (2,1)-(2,4), (2,2)-(2,5).
- Cross row (dx=1,dy=2): (1,c)-(2,c+2) for c=1,2,3: (1,1)-(2,3), (1,2)-(2,4), (1,3)-(2,5). And (1,c)-(2,c-2) for c=3,4,5: (1,3)-(2,1), (1,4)-(2,2), (1,5)-(2,3).
Also maybe cross row with dy= -2? Already covered.
Also cross row with dy=2? Already.
Also note cross row with dx=1,dy=2 only; also maybe dx=1,dy=2 and dx=1,dy=-2 are same.
Also vertical distance 3? dx=3,dy=0 not possible because only 2 rows.
So edges list:
Horizontal: H1: (1,1)-(1,4); H2: (1,2)-(1,5); H3: (2,1)-(2,4); H4: (2,2)-(2,5).
Cross: C1: (1,1)-(2,3)
C2: (1,2)-(2,4)
C3: (1,3)-(2,5)
C4: (1,3)-(2,1) (since c=3 gives (1,3)-(2,1))
C5: (1,4)-(2,2)
C6: (1,5)-(2,3)
Check C4: (1,3)-(2,1) yes.
C5: (1,4)-(2,2)
C6: (1,5)-(2,3)
Also maybe (1,?)-(2,?) with dy= -2? Already included for c=5 gives (1,5)-(2,3) which is dy=-2? Actually (1,5)-(2,3): dx=1, dy=2? |5-3|=2, so yes.
Also (1,?)-(2,?) for c=?? (1,?)-(2,?+2) gave for c up to 3; (1,?)-(2,?-2) gave for c from 3 to5.
Now we need maximum matching.
We can attempt to find perfect matching covering all 10 cells.
Try to replicate pattern from 2x4: we used cross edges covering all columns. For 2x5, can we cover all?
Consider using cross edges: we need 5 edges covering 10 cells. Perhaps a pattern: match (1,1)-(2,3), (1,2)-(2,4), (1,3)-(2,5), (1,4)-(2,2), (1,5)-(2,1). Check if these 5 edges are disjoint? They involve all cells: row1: all columns 1-5; row2: columns 3,4,5,2,1 => all columns 1-5. But are all these edges valid? Check each:
(1,1)-(2,3) valid (C1)
(1,2)-(2,4) valid (C2)
(1,3)-(2,5) valid (C3)
(1,4)-(2,2) valid (C5)
(1,5)-(2,1) Is (1,5)-(2,1) valid? Distance: |1-2|=1, |5-1|=4, total=5, not 3. So not valid. So that last is not an edge. So that set fails.
Alternate: maybe use horizontal edges combined with cross.
Let's attempt systematic search or reasoning.
Because 2x5 has 10 vertices, maybe maximum matching is 4 (8 pieces) or 5 (10 pieces). Let's try to see if perfect matching exists.
We can treat it as a graph and try to find a perfect matching.
One approach: Since graph is bipartite? Actually it's not bipartite by rows because same-row edges connect vertices of same color? But recall parity: distance 3 odd => endpoints have opposite parity. In chessboard coloring, (x+y) parity flips. So edges always connect opposite colors. So the graph is bipartite with bipartition by (x+y) parity. For 2x5, let's compute parity. (1,1): even? (1+1=2 even) black? (1,2):3 odd; (1,3):4 even; (1,4):5 odd; (1,5):6 even; (2,1):3 odd; (2,2):4 even