We are given q queries. For each query, we have an n x m grid with characters '.' (white) and '*' (black). We can change white to black. We need the minimum number of cells to paint black so that there exists at least one cross. A cross at (x,y) means that all cells in row x and all cells in column y are black.
We want to find the minimum number of additional black cells needed.
Let's think: For each potential cross center (i, j), we need to ensure row i and column j are all black. Currently, some cells in row i and column j may be white. The number of white cells that need to be painted = (number of white cells in row i) + (number of white cells in column j) - possibly double-counted cell (i,j) if it's white? Because row i and column j intersect at (i,j). If (i,j) is white, it's counted in both row i white count and column j white count. So total whites to paint for cross (i,j) = white_in_row_i + white_in_col_j - (1 if cell (i,j) is white else 0). That is the number of minutes needed to achieve that cross.
We need min over all i,j of that quantity.
So the problem reduces to: given the grid, compute row white counts and column white counts efficiently, and then compute the minimum over all cells of row_white[i] + col_white[j] - (grid[i][j] == '.'? 1 : 0). That's O(nm) per query, which total constraints: sum nm <= 4e5, so O(total cells) is acceptable. But we need to be careful: n and m can be up to 5e4, but product limited to 4e5. So we can loop over each cell for each query. That's straightforward.
But wait, is there any nuance? Possibly we need to consider that after painting we need at least one cross, but the cost computed is correct for achieving that specific cross. The minimum over all possible crosses is the answer.
However, there's a catch: The cross must be all black, but we are allowed to paint any white cells, so yes.
But wait: Could there be a case where we can do better than that min? Possibly by painting cells that simultaneously help multiple crosses? But we only need at least one cross. So optimizing over a single cross is enough: any painting that yields a cross must include making some row and column all black. The cost to make row i and column j all black is exactly that number of white cells in that row and column (accounting for intersection). Because we can choose to paint exactly those white cells (and not paint others). There's no advantage to painting extra cells beyond those necessary. So indeed the minimal number of minutes to achieve at least one cross equals min_{i,j} (row_white[i] + col_white[j] - (grid[i][j]=='.')).
Thus the algorithm: For each query:
- Input n, m.
- Read grid as n strings.
- Compute an array row_white of size n: for each row, count '.'.
- Compute an array col_white of size m: for each column, count '.'.
- Then iterate over each cell (i,j): compute cost = row_white[i] + col_white[j] - (grid[i][j]=='.'? 1 : 0). Keep min.
- Output min.
Complexity: O(total cells) which is ≤ 4e5 total across queries. q up to 5e4 but sum n*m small. So fine.
But check constraints: n and m individually can be large (5e4) but product limited, so we cannot allocate an m-sized array for each query? Actually we can, because m can be up to 5e4, and sum of n across queries ≤ 5e4, but sum of m? It says ∑ n ≤ 5e4 and ∑ n*m ≤ 4e5. It doesn't say ∑ m individually, but m per query can be large if n is small. For example, n=1, m=5e4, product 5e4. So we need to allocate arrays of size m, which could be up to 5e4, but total memory across queries we can handle (since we process each query individually). That's fine.
One nuance: The input grid lines might not have spaces, each row is a string of length m.
Edge cases: n=1 or m=1. Then a cross is simply the whole row and column? Actually cross defined as all cells in row x and column y black. If n=1, then row is length m, column is just the single cell (since column y has only row 1). So cross exists if row 1 is all black? Actually need all cells in row 1 (i.e., the whole row) and all cells in column y (which is just cell (1,y)) black. That means essentially the whole row must be black, because column y is just a cell that is part of row; if row is all black, then any column y yields a cross. But to have a cross, we need both row and column black. If row is all black, then column y is automatically black because cell (1,y) is black. So condition reduces to row being all black. Similarly for m=1, need column all black. The cost formula still works: For n=1, row_white[1] is number of white in row. col_white[y] counts white in column y (which is just cell (1,y) so either 0 or 1). cost = row_white + col_white - (cell white?1:0). If cell is white: row_white includes it; col_white includes it; so cost = row_white + 1 -1 = row_white. That means we need to paint all whites in the row, which is correct: we must make the whole row black. If cell is black, row_white = number of other whites; col_white=0; cost = row_white; need to paint all other whites. So min over y yields same cost: row_white. So formula works.
Similarly for m=1.
Thus we can implement straightforwardly.
But need to ensure we don't exceed time due to many queries with small grids? The total cells 4e5, and we iterate over each cell twice: first to compute row and column counts (or one pass) and then to compute min. That's fine.
We should also consider possibly a more efficient approach: we can compute min by scanning rows and columns? But O(n*m) is fine.
Let's test with example.
We'll implement.
Potential pitfalls: Input reading large, use fast I/O. In C++ we can use ios_base::sync_with_stdio(false); cin.tie(0). In Python, input reading may be okay with sys.stdin. But total input size is at most sum(n*m) ~ 4e5 characters plus some numbers, so Python is fine.
But careful: The constraints state ∑ n ≤ 5e4 and ∑ n*m ≤ 4e5. So overall not huge. Python can handle.
Implementation details:
For each query:
- read n,m.
- initialize row_white = [0]*n, col_white = [0]*m
- grid = [] # store rows as strings
- for i in range(n):
line = input().strip()
grid.append(line)
count_dot = line.count('.')
row_white[i] = count_dot
for j,ch in enumerate(line):
if ch == '.':
col_white[j] += 1
- Then compute min_cost = large
- for i in range(n):
for j in range(m):
cost = row_white[i] + col_white[j] - (1 if grid[i][j]=='.' else 0)
if cost < min_cost:
min_cost = cost
- print(min_cost)
That's O(n*m) per query. Works.
Edge Cases: Is there any scenario where we might not need to paint any cell (cost 0) and there is already a cross? That would be caught as cost=0 for some (i,j) where row_white[i]==0 and col_white[j]==0, and cell black (so subtract 0). So fine.
Time complexity: O(total cells) across all queries ~ 4e5, which is trivial.
But we must note that sum n*m <= 4e5, but the number of queries up to 5e4. Many queries with tiny grids (e.g., n=1,m=1) -> total loops is fine.
Let's test with given example.
We'll simulate.
But I wonder if there is any hidden nuance: The cross requires that the entire row and entire column are black. But we are allowed to paint any white cells. Is it possible that the minimum number of painting minutes is less than the min over row_white[i] + col_white[j] - (cell white)? Could we paint a cell that is not in the chosen row or column but indirectly helps? No, because to achieve a cross, the only cells that matter are those in that row and column. Painting other cells does not help achieve that cross. So the only way to get a cross is to make some row i and column j all black. So we must paint at least the white cells in that row and column. Possibly we could achieve a cross by making both row i and column j black without painting cell (i,j) if it is already black? That's included. So indeed cost is that number.
But could we achieve a cross without necessarily having a single (i,j) that is the intersection? No, cross is defined by a pair (x,y). So we need to have some row and some column completely black. So yes.
Thus solution correct.
One more thought: Could there be a cross that is not aligned? No.
Hence solution is straightforward.
But hold on: There might be an alternative: painting cells to create a cross but maybe using a row and column that are not originally the ones we consider because after painting some cells, maybe both row and column become all black but we didn't target a specific cross? Still there will be some intersection coordinates. So min over all possible (i,j) of the cost to make row i and col j all black gives the answer.
Thus we are done.
However, I realize we might need to consider that the cost computed is the number of cells we need to paint. But is there any scenario where we can paint fewer cells by making both row i and column j black but not painting some white cells because those cells become black due to painting for the other dimension? Actually each white cell must be painted. So no.
Double-check: Suppose we want cross at (i,j). We must ensure all cells in row i are black. Those include cells (i,1...m). Some may be white. Similarly column j cells (1...n,j). There is overlap at (i,j). If (i,j) is white, it is in both sets, but we only need to paint it once. So cost = |white cells in row i| + |white cells in column j| - (1 if (i,j) white else 0). That is correct.
Thus answer is min cost.
Implementation straightforward.
Now, check example: 6th query:
5 5
.....
....
..
....
.....
We'll compute row_white: row0:5, row1:4? Actually row1: "...." -> dots at positions 1,2,4,5? Wait string length 5: positions: 0:'.',1:'.',2:'',3:'.',4:'.'? Actually "...." -> characters: . . * . . So 4 dots? Actually two dots, then star, then two dots => total 4 dots. So row_white[1]=4? But row index maybe 0-based. Row2: ".." -> . * * * . -> dots at pos0 and pos4 => 2 dots. Row3: "...." -> same as row1 => 4 dots. Row4: "....." -> 5 dots. col_white: compute. Let's compute quickly: col0: rows 0-4: row0 '.', row1 '.', row2 '.', row3 '.', row4 '.' => all dots => 5. col1: row0 '.', row1 '.', row2 '', row3 '.', row4 '.' => 4 dots? Actually row2 col1 is ''? row2 is ".." index1 is '', so not dot. So dots: row0, row1, row3, row4 => 4. col2: row0 '.', row1 '', row2 '', row3 '', row4 '.' => dots: row0, row4 =>2? Actually row1 col2 is '', row2 col2 is '', row3 col2 is ''; so only row0 and row4 dot =>2. col3: row0 '.', row1 '.', row2 '', row3 '.', row4 '.' => dots: row0,row1,row3,row4 =>4? Actually row2 col3 is '', yes. So 4. col4: row0 '.', row1 '.', row2 '.', row3 '.', row4 '.'? row2 col4 is '.'? row2 is ".." index4 is '.' => yes. So all dots =>5. Now compute cost for each cell. Minimum? They claim answer 4. Let's compute maybe for cross at (2,2) (0-index row2 col2). row_white[2]=2, col_white[2]=2, cell (2,2) is '' so cost =2+2-0=4. That's 4. Could there be lower? Check (2,1): row_white[2]=2, col_white[1]=4, cell (2,1) is ''? row2 col1 is '', cost=2+4=6. (1,2): row_white[1]=4, col_white[2]=2, cell (1,2) is ''? row1 col2 is '', cost=6. (0,2): row_white[0]=5, col_white[2]=2, cell (0,2) is '.' -> cost=5+2-1=6. (2,0): row_white[2]=2, col_white[0]=5, cell (2,0) is '.' -> cost=2+5-1=6. (4,2): row_white[4]=5, col_white[2]=2, cell (4,2) is '.' -> 5+2-1=6. (2,4): row_white[2]=2, col_white[4]=5, cell (2,4) is '.' -> 2+5-1=6. So min 4. Good.
Thus approach works.
Now for the 7th query:
5 3
...
..
..
..
Answer: 1. Compute quickly: row0: "..." -> 3 dots; row1: ".." -> 2 dots? Actually . * . -> two dots; row2: ".." -> two dots; row3: "" -> 0 dots; row4: ".." -> two dots. col0: rows: . . . * .? Actually row0 col0='.', row1 col0='.', row2 col0='.', row3 col0='', row4 col0='.' => 4 dots? row3 col0 is '', so dots: rows0,1,2,4 -> 4. col1: row0 '.', row1 '', row2 '', row3 '', row4 ''? Wait row4 col1 is ''? row4 ".." -> col1 is '', yes. So dots: only row0 col1 is '.'? Actually row0 col1 is '.'? row0 "..." -> col1 '.'; row1 col1 '', row2 col1 '', row3 col1 '', row4 col1 '' => only one dot -> 1. col2: row0 '.', row1 '.', row2 '.', row3 '', row4 '.' => dots: row0, row1, row2, row4 -> 4? row3 col2 ''? row3 "" -> col2 '', so yes. So col2: 4 dots. Now min cost: perhaps cross at (3,1) row3 all black? row_white[3]=0, col_white[1]=1, cell (3,1) is '*' => cost=0+1=1. So answer 1.
All good.
Thus solution seems correct.
But wait, there might be an optimization to avoid iterating over all cells for cost if we know that the min over i,j of row_white[i] + col_white[j] - (cell white) can be computed in O(n+m) by some precomputation? For example, we can find min over i of row_white[i] and min over j of col_white[j], but due to the subtraction term, the cell being white might reduce cost. However, we could also compute min cost by considering each cell? Since total cells limited, not needed.
But there is nuance: The constraints allow sum nm up to 4e5, which is small. So O(nm) per query is fine.
Thus the solution is simple.
But we need to consider that each query has up to maybe n=50000, m=8? Actually product ≤ 4e5, so if n is 50000, then m ≤ 8 (since 50000*8=400000). So loops over rows and columns still fine.
Now, implement in C++? We'll do Python because simpler.
But careful: Input reading may be heavy; use sys.stdin.read to parse all? Not necessary. Use sys.stdin.readline.
Implementation in Python:
def solve():
import sys
input = sys.stdin.readline
q = int(input().strip())
out_lines = []
for _ in range(q):
line = input().strip()
while line == '':
line = input().strip()
n,m = map(int,line.split())
row_white = [0]*n
col_white = [0]*m
grid = []
for i in range(n):
row = input().strip()
grid.append(row)
cnt = row.count('.')
row_white[i] = cnt
# update col_white
# iterate over characters
for j,ch in enumerate(row):
if ch == '.':
col_white[j] += 1
# compute min cost
min_cost = n+m # maximum possible cost is n+m-1? Actually max row_white <= m, col_white <= n, so cost <= m+n, okay.
for i in range(n):
rw = row_white[i]
# If we want to speed up, we can compute per row: for each column j.
# But just double loop.
row_str = grid[i]
for j in range(m):
cost = rw + col_white[j] - (1 if row_str[j]=='.' else 0)
if cost < min_cost:
min_cost = cost
out_lines.append(str(min_cost))
sys.stdout.write("\n".join(out_lines))
Test with sample.
But note: The input might contain spaces? No, each row is a string without spaces.
Edge Cases: Might have n or m = 0? No, both ≥1.
Double-check product constraints: nm ≤ 4e5, so total characters per query is limited. However, reading lines and iterating over them with enumerate row for col_white: that's O(nm) anyway.
But one optimization: we could compute col_white in a separate pass? But we can compute during reading, as above.
Memory: storing grid as list of strings is fine, total characters sum 4e5.
Now test with sample input.
We'll simulate quickly.
Input:
9
5 5
....
....
....
....
... (others). Should produce outputs.
Seems good.
But caution: The sample input lines may have trailing spaces? Usually not.
Thus answer.
However, there is a possible twist: Could there be a scenario where the minimal cost is to paint something like: maybe we don't need to paint all whites in row i and column j if we can choose a different cross after painting? That is, we might paint some cells that help create a cross but not necessarily making a particular row and column all black at the time of painting? For example, maybe painting a single white cell could create two crosses simultaneously? But we only need one. The idea is we can consider a painting strategy that might not be explicitly targeting a specific cross, but after painting some cells, a cross emerges. Could that be cheaper than any individual (i,j) cost? Let's think.
Suppose we have a grid where row i is almost all black except one white, and column j is almost all black except one white, but those two whites are the same cell (i,j). Then cost for (i,j) = 1+1-1=1. That's straightforward. Suppose row i has two whites (not at column j) and column j has two whites (not at row i), but there is overlap? Actually row i whites at columns a,b; column j whites at rows c,d. If there is no overlap, then cost for (i,j) = 2+2=4. Could there be a cheaper way? Perhaps we could paint a cell that is in row i and column k, and another cell in row l and column j, such that after painting those, maybe row i becomes all black and column j becomes all black? That's exactly painting the whites in row i and column j. But maybe we could instead paint a different set of cells that results in some other row x and column y being all black, which may require fewer paints. That's covered by min over all (x,y). So the best cross we can achieve is the one with minimal cost.
But could we achieve a cross by painting cells that are not exactly the whites of that row and column? Suppose we paint a cell that is white in row i, that's necessary. But what about column j? If we paint a cell that is not in column j but in row i? That doesn't affect column j. So we must paint all whites in column j. So the minimal paints to achieve cross (i,j) is exactly those whites. So no alternative.
Thus min over (i,j) is correct.
But is there a scenario where we can achieve a cross by painting fewer cells than that min because we can leverage existing crosses? For instance, if there is already a cross, cost=0. That's covered.
Thus solution stands.
Now, is there any subtlety about time? The problem statements says "Each minute you may choose a white cell and paint it black." So painting is done sequentially, but count is number of cells. So okay.
Thus the solution is correct.
Now implement.
But we need to be careful about reading input when there are empty lines? Usually not.
We can just use input().split() for first line of each query. However, after reading q, we need to loop. Inside, we read n,m. But note: There might be blank lines? Usually not. So we can rely on input().split().
Let's code.
One more nuance: For each query, we compute row_white and col_white. The total sum of n*m across all queries is 4e5. However, the sum of m across all queries might be larger? But not a problem.
Now double-check complexity: O(total cells) = 4e5, fine.
Edge case: n=50000, m=8, we store grid as list of 50000 strings each length 8. That's fine.
Now I'll produce final solution.
But I recall that there is known Codeforces problem "1118B" or something? Actually this is Codeforces Round 1150 problem? Not sure. But it's fine.
Let's test quickly on sample.
I'll run mental simulation:
Query1: grid already has cross? The first picture (5x5 with pattern) likely has cross at row3? Actually row index 3 (1-index) is ***** all black. And column 3 (1-index) is all black? Let's check: column 3: rows: row1: '' (third char), row2:'', row3:'', row4:'', row5:''? Actually given: .... => row1 col3='', row2 col3='', row3 all stars, row4 col3='', row5 col3='' => yes column3 all black. So cross exists, cost=0.
Query2: 3x4
...
...
Row1 all black. Column? Check column 1: rows: '', '.', '.' => not all black. But maybe cross at (1,1) requires column1 all black? Not. But there is a cross at (1,4)? Column4: rows: row1'', row2'.', row3'.' -> not all. Row2? Not all. Actually picture 2 has cross? The note says first 5 pictures already contain a cross. So query2 must have cross. Let's check: row2? row2 is "..." not all black. row3 similar. Row1 all black. Need column all black. Check column2: row1 '', row2''? Actually row2 "..." -> col2 is '', row3 "..." -> col2 is '', so column2 is all ''? row1 col2 is '', row2 col2 '', row3 col2 '*' -> yes all black. So cross at (1,2) exists. So cost 0.
Query3: 4x3
..
..
..
Row1 all black, column1? col1: row1 '', row2'', row3'', row4'*' => all black, so cross at (1,1) exists.
Query4: 5x5
..*
...
..***
Check cross? Possibly row3 all black, column3? row3 is , yes. column3: row1 col3 '*'? row1 "" col3 '', row2 ".." col3 '.'? Actually row2: positions: 1:'',2:'.',3:'',4:'.',5:'' -> col3 is '', row3 all black col3 '', row4 "..." col3 '', row5 ".." col3 ''? row5: ..* -> positions:1:'.',2:'.',3:'',4:'',5:'' -> col3 '', so column3 all black. So cross exists. cost 0.
Query5: 1 4
All black row, any column? row1 all black, column any: cell black, so cross exists? Actually cross requires row and column both all black. Since n=1, column is just that cell, so if row all black, column also black. So cross exists. cost 0.
Query6: we computed cost 4.
Query7: cost 1.
Query8: 3 3
..
.
..
Compute: row_white: row0: 2? Actually .. -> '.' '' '.' -> two dots; row1: . -> dot at col1? Actually . -> star, dot, star? Wait string ".": index0 '', index1 '.', index2 '' => one dot? Actually dot at col1 only, so row1 white count=1; row2: .. -> two dots. col_white: col0: rows: '.' , '', '.' => two dots; col1: '', '.', '' => one dot? Actually row0 col1 '', row1 col1 '.', row2 col1 '' => one dot; col2: '.', '', '.' => two dots. Then cost for each cell. Check cross at (1,1) (0-index row1 col1) -> row_white[1]=1, col_white[1]=1, cell is '.'? row1 col1 is '.' => cost =1+1-1=1. That's minimal? Others: (0,0): row0=2, col0=2, cell '.' => 2+2-1=3; (0,1): row0=2, col1=1, cell '' => 2+1=3; (0,2): row0=2, col2=2, cell '.' => 2+2-1=3; (1,0): row1=1, col0=2, cell ''? row1 col0='' => 1+2=3; (1,2): row1=1, col2=2, cell '' => 1+2=3; (2,0): row2=2, col0=2, cell '.' => 2+2-1=3; (2,1): row2=2, col1=1, cell '' => 2+1=3; (2,2): row2=2, col2=2, cell '.' => 2+2-1=3. So min=1. So answer 1.
Query9: 4 4
.*
....
.*
.*
Compute row_white: row0: "."? Actually given "."? Wait "." with first char? It says "." but note: ". **"? The string is ". **"? Actually given: ". **"? Let's see the input:
4 4
.*
....
.*
.*
So row0: ".**" (star, dot, star, star?) Actually "." means characters: index0:'', index1:'.', index2:'', index3:''? But "." is ambiguous. Usually, it's a 4-character string: first char '', second '.', third '', fourth ''. But then it's "." which is "", ".", "", ""? Actually "." would be star, dot, star, star? That is 4 chars: '' , '.' , '' , ''? However the pattern "." could also be interpreted as star, dot, star, star? But it's written ". "? The sample says:
4 4
.
....
.
.
I think it's: row0 = "*." meaning characters: '', '.', '', ''? But let's parse: "." has length 4: char1 '', char2 '.', char3 '', char4 ''? Actually "." -> first char '', then "." -> '.' then ''? That would be '', '.', '', ''? Yes. So row0: ".**" -> positions: 1:'',2:'.',3:'',4:''. So row0 white count=1 (only col2). row1: "...." -> 4 whites. row2: ".**" -> same as row0, 1 white. row3: ".**" -> 1 white.
col_white: col0: rows: '', '.', '', '' => only row1 dot => 1 white. col1: rows: '.', '.', '.', '.'? Actually row0 col1 is '.'? Wait row0 "." index1 (0-index) is '.'? Let's index 0: row0[0]='', [1]='.', [2]='', [3]=''. So col1 (index1): '.' from row0, row1[1]='.' (since row1 "...."), row2[1]='.' (row2 "."), row3[1]='.' (row3 ".**") => all four rows have '.'? Actually row2 and row3 have '.' at index1? row2 "." -> index1 '.'; row3 "*." -> index1 '.'; so col1 has 4 dots. col2: rows: row0[2]='', row1[2]='.', row2[2]='', row3[2]='' => one dot (row1). col3: rows: row0[3]='', row1[3]='.', row2[3]='', row3[3]='' => one dot (row1). So col_white = [1,4,1,1].
Now compute costs. The answer given is 2. Let's compute min. Look at cross at (0,0): row0 white=1, col0 white=1, cell (0,0) is '' => cost = 1+1 =2. (0,2): row0=1, col2=1, cell (0,2) is '' => cost =2. (2,0): row2=1, col0=1, cell (2,0) '' => 2. (2,2): row2=1, col2=1, cell '' => 2. (0,3): row0=1, col3=1, cell '' =>2. (3,0): row3=1, col0=1, cell '' =>2. (1,0): row1=4, col0=1, cell '.' => cost=4+1-1=4. (1,1): row1=4, col1=4, cell '.' =>4+4-1=7. (1,2): row1=4, col2=1, cell '.' =>4+1-1=4. (1,3): similarly 4. (0,1): row0=1, col1=4, cell '.' =>1+4-1=4. (2,1): row2=1, col1=4, cell '.' =>1+4-1=4. (3,1): row3=1, col1=4, cell '.' =>4. (2,3): row2=1, col3=1, cell '' =>2. (3,2): row3=1, col2=1, cell '' =>2. (3,3): row3=1, col3=1, cell '*' =>2. So min=2. So answer 2.
All matches.
Thus solution is correct.
One more nuance: The problem may have large n,m individually but we need to ensure that reading the lines and iterating over columns for each row for col_white update is efficient. In Python, iterating over each character in a string of length m is O(m). For each row, we do that. So total O(n*m). That's fine.
We might want to avoid using row.count('.') plus another loop, but row.count('.') also iterates over the string; that would be double iteration per row. But we can combine: we can compute row_white[i] by counting '.' in the loop that updates col_white. We can have a variable cnt=0, then for each j, if ch=='.', cnt++, col_white[j]++. Then assign row_white[i]=cnt. This reduces passes. However, using row.count('.') is also O(m) and the separate loop is O(m), so two passes per row. Still total O(2 * n*m) which is fine given total 4e5. But we can combine to be a bit more efficient.
Let's implement combined:
for i in range(n):
row = input().strip()
grid.append(row)
cnt = 0
for j,ch in enumerate(row):
if ch == '.':
cnt += 1
col_white[j] += 1
row_white[i] = cnt
That's efficient.
Now, for min cost, we again loop over all cells: O(nm). So total O(2n*m) per query. Acceptable.
But we could also compute min cost differently: Since we have row_white and col_white, we need min over i,j of row_white[i] + col_white[j] - (grid[i][j]=='.'). The subtraction term is only -1 when cell is white. So the minimal value is at most min_i row_white[i] + min_j col_white[j] (maybe minus 1 if there is a white cell at the intersection of the rows and columns achieving those mins). But not necessarily because the row and column that give min sum might not intersect at a white cell to get the -1, but a different combination could yield lower due to the -1. Because of that, we cannot simply take min row + min col - something; we need to check actual intersections.
But again total cells small.
Thus final solution is as above.
Now, code.
But there is one more catch: The input constraints state ∑ n ≤ 5e4 and ∑ n ⋅ m ≤ 4e5. However, there is no guarantee that ∑ m is bounded similarly. But we allocate col_white of size m per query. If many queries have large m (e.g., 5e4) but n small (like 1), total m across queries could be large (e.g., 5e4 * 1e4? But q up to 5e4, but ∑ n is ≤ 5e4, meaning the total number of rows across all queries is ≤ 5e4. For each query, n can be 1, so we could have up to 5e4 queries with n=1, each m could be up to 4e5? But nm ≤ 4e5, so m ≤ 4e5 per query. However, ∑ n ≤ 5e4, but that doesn't bound the number of queries with n=1: we could have 5e4 queries with n=1, each m up to 4e5? But nm ≤ 4e5, so with n=1, m can be up to 4e5. That would make total cells across all queries = sum(nm) ≤ 4e5 (overall), not per query. Actually constraint says ∑ n ⋅ m ≤ 4 ⋅ 10^5. That is total sum over all queries. So the total cells across all queries is ≤ 4e5. So if we have many queries, each with small nm, the sum is limited. So total memory allocation across all queries is fine, and total time is O(4e5). So even if we allocate col_white of size up to 4e5 in a single query, that's okay because total cells across all queries is limited. But if a single query has n=1, m=400000, then n*m=400k, which is allowed. So we need to allocate col_white of size 400k, which is okay (like 3.2 MB for ints). But Python list of ints of size 400k is about 3.2 MB, fine. However, we also store grid as list of rows; if n=1, we store 1 string of length 400k, which is 400k bytes, also fine.
So overall memory usage within limits.
Thus we can implement safely.
Edge Cases: There might be a scenario where we can achieve cross without painting all white cells in row and column if we choose a different cross after painting some cells. But we are looking for min over all possible final crosses. But maybe we can paint some cells that partially fill both row and column, and then the cross might be at a different row and column? For example, paint a cell that makes row i all black, but column j still has white, but then we might consider cross at (i,k) where column k becomes all black because of that painting? That cross would require row i black (now done) and column k black. The cells painted to make row i black might also help column k. But the minimal cost to achieve any cross is min over all (i,j) of the number of white cells in row i and column j. However, consider we might paint a cell that is in both row i and column j, but we might later use a different column. But we are allowed to choose which cells to paint. The minimal number of paints needed to ensure there exists at least one all-black row and one all-black column that intersect at a black cell is exactly min_{i,j} (white_i + white_j - white_intersect). Because if we decide to aim for cross (i,j), we must paint those whites. Could there be a strategy that paints fewer cells, yet after painting, there is some cross (x,y) where the white cells in row x and column y are not exactly those we painted? But the painted set must include all white cells in row x and column y. So the number of painted cells is at least white_in_row_x + white_in_col_y - (if intersection painted twice?). Actually if we paint a set S, and after painting, row x and column y are all black, then S must contain all white cells in row x and column y (since those cells were white initially and need to become black). So |S| >= white_row_x + white_col_y - (1 if cell (x,y) was white else 0) because that cell would be counted twice if both row and column include it. So the minimum over all (i,j) of that lower bound is a lower bound for any painting strategy that yields a cross. And we can achieve exactly that lower bound by painting exactly those white cells. So the minimal possible minutes equals that min. So it's both necessary and sufficient.
Thus our answer is correct.
Hence solution is complete.
Now produce final answer with explanation.
But we need to output for each query a single integer.
Implementation in Python.
Potential improvements: Since q can be large, using sys.stdin.read and parse manually might be faster, but not necessary.
Let's write final code.
I'll include fast I/O and combine loops.
Now produce final solution message. ```python
import sys
def solve() -> None:
data = sys.stdin.read().splitlines()
it = iter(data)
q = int(next(it).strip())
out = []
for _ in range(q):
# read n, m
while True:
line = next(it).strip()
if line:
break
n, m = map(int, line.split())
row_white = [0] * n
col_white = [0] * m
grid = []
for i in range(n):
row = next(it).strip()
grid.append(row)
cnt = 0
for j, ch in enumerate(row):
if ch == '.':
cnt += 1
col_white[j] += 1
row_white[i] = cnt
best = n + m # an upper bound
for i in range(n):
rw = row_white[i]
row_str = grid[i]
for j in range(m):
cost = rw + col_white[j] - (1 if row_str[j] == '.' else 0)
if cost < best:
best = cost
out.append(str(best))
sys.stdout.write("\n".join(out))
if name == "main":
solve()