We need to read t testcases, for each: n, m, x, y, then n lines of m characters each (only '.' and '*'). We need to cover all '.' squares with tiles: 1x1 costing x, or 1x2 covering two adjacent squares in the same row. Cannot cover black squares, cannot overlap. Find minimum total cost.
We can treat each row independently because tiles cannot cross rows (only horizontal). So for each row, we have a string of length m with '.' and ''. We need to cover contiguous '.' segments, but with the ability to use 1x2 tiles to cover pairs of adjacent '.' squares. For each segment of consecutive '.' of length L, we need to compute minimum cost to cover L squares with tiles of size 1 and 2 (horizontal only, covering adjacent positions). This is a standard problem: if 2x <= y then using 1x1 tiles is cheaper or equal than any 1x2 tile (since a 1x2 tile costs y, but two 1x1 cost 2x; if 2x <= y, it's cheaper to use 1x1). Actually careful: If y < 2x, then using 1x2 tiles may be cheaper, but we must also consider that we might not be able to pair all squares if L is odd. The optimal for a segment of length L: we can use k 1x2 tiles covering 2k squares, and L-2k 1x1 tiles. The cost: ky + (L-2k)x. Since we want minimize, we should use as many 1x2 tiles as possible if y < 2x. But if y < 2x, it's cheaper to use a 1x2 than two 1x1, so we would maximize k, i.e., use floor(L/2) 1x2 tiles and optionally one 1x1 if L odd. However, there might be a case where y < x? Possibly, but that seems unnatural because a 1x2 tile covers 2 squares, and if y < x, then a 1x2 tile is cheaper than a 1x1 tile, which would be strange but possible. If y < x, then we would prefer to cover even pairs with 1x2, but for an odd one we might still need a 1x1 (cost x) or perhaps we could use a 1x2 covering one white and one black? Not allowed because black squares can't be covered. So we cannot cover an odd '.' with a 1x2 if its neighbor is black. So the segment is contiguous '.' only; we cannot use a 1x2 that extends beyond segment. So for odd length, we must cover the last '.' with a 1x1. But is there any scenario where we would not want to use a 1x2 even if y < 2x? No, because each 1x2 tile replaces two 1x1 tiles (cost 2x). If y < 2x, it's cheaper. But if y < x, using a 1x2 tile on two squares saves even more, but there's no disadvantage since they cover exactly two squares and don't interfere. So the optimal is to use as many 1x2 tiles as possible, i.e., floor(L/2). So overall cost for segment = floor(L/2) * min(y, 2x)?? Wait careful: If y > 2x, then using two 1x1 is cheaper than one 1x2, so we would not use any 1x2 tiles; we'd just use L 1x1 tiles. If y <= 2x, then using a 1x2 tile for a pair is cheaper or equal to two 1x1 tiles. So we want to use floor(L/2) 1x2 tiles if y <= 2*x. But is there a case where y < x? Then it's even more beneficial to use 1x2. But still we can't cover odd with 1x2, so floor(L/2) * y + (L%2)*x. However, if y < x, could there be a better strategy to use a 1x2 tile covering one white and one white? That is already considered. For odd L, we might consider using a 1x2 overlapping a black? Not allowed. So that's optimal.
But wait: there might be a nuance: if y is less than x, then maybe we would want to use a 1x2 tile even if it covers a white and a black? Not allowed. So we must cover only white squares. So we cannot.
Thus the answer per row is sum over segments: if y < 2*x, cost = (segment_len//2)*y + (segment_len%2)x; else cost = segment_lenx.
But careful: if y < x, then using 1x2 tile is cheaper than 1x1, but we can't cover all squares with 1x2 if odd, so we pay x for the odd. However, is there a possibility to "overlap" a 1x2 partially? No, tiles cannot be cut, they must cover exactly squares. So that's it.
Thus overall answer is sum over all rows.
Edge Cases: m can be up to 1000, n up to 100, total nm <= 1e5, so O(total) is fine.
Check example: testcase 1: n=1,m=1,x=10,y=1. '.' length 1, since y=1 < 2*x=20, we use (1//2)*1 + (1%2)*10 = 0+10=10. Ok.
testcase2: n=1,m=2,x=10,y=1 => ".." length 2, y=1 < 20 => floor2/21 + 010 = 1. Ok.
testcase3: n=2,m=1,x=10,y=1 => each row has a '.' length 1, y=1 < 20 => each row cost 10, total 20. But note that 1x2 tile cannot be used because m=1, so each row separate. Ok.
testcase4: n=3,m=3,x=3,y=7 => 2x=6, y=7 >6, so we use all 1x1 tiles. Count white squares: row0 ".." => 2 whites, row1 ".." => 2 whites, row2 ".." => 2 whites? Actually ".." has a white at pos0? Wait string: ".." first char '.'? No: ".." means positions: [0] '.'? Actually the dot? Let's parse: ".." - the characters: first is '.'? Wait the string is given as ".."; the description: ".." means dot star dot? Actually it's ".."? In the input:
3 3 3 7
..
..
..
Yes row2 is "..": char0='.', char1='', char2='.'? Wait ".." would be dot star dot? But the text says ".." maybe it's a dot, star, dot? Actually the example output is 18. Count whites: Row0: ".." -> first two dots, third star => 2 whites. Row1: ".." -> star then two dots => 2 whites. Row2: ".." -> first dot? Actually if the string is "..", then char0='.', char1='*', char2='.' => 2 whites. So total whites = 6. All whites covered by 1x1 at cost 3 each => 18. So matches.
Thus the solution is simple.
But is there any scenario where using a 1x2 tile could be beneficial even if y > 2x? No, because each 1x2 tile covers two squares, but cost y > 2x, so it's more expensive than two 1x1. So never.
Thus answer is as described.
But wait: there is a nuance: the problem statement says "You are not allowed to rotate these tiles or cut them into 1 × 1 tiles." So the 1x2 tile can only be placed horizontally (same row). That's what we used.
Thus solution per test: read n,m,x,y; total_cost = 0; for each row: read line; parse segments of '.'; for each segment length L: if 2x <= y: total_cost += Lx; else: total_cost += (L//2)*y + (L%2)*x; print total_cost.
But careful: if 2x <= y, then using two 1x1 is cheaper or equal than one 1x2. If it's equal (2x == y) then either approach yields same cost for a pair. But using 1x2 for an even segment yields floor(L/2)y = floor(L/2)2x = Lx, same as using 1x1. For odd L, using 1x2 yields floor(L/2)2x + x = Lx as well. So both equal. So it's fine to just use the condition 2x <= y to use all 1x1. But what about the case where y < x? Then our else branch uses L//2y + (L%2)x. This might be optimal. However, consider if y < x, but 2x <= y? That's impossible because if y < x, then y < x => 2y < 2x? Actually check: If y < x, then 2x <= y cannot hold because y < x implies y < 2*x (since x>0). So else branch is appropriate.
But what if y is less than x? Then maybe we could consider covering a single white with a 1x2 tile partially covering a black? Not allowed. So fine.
Thus the solution is straightforward.
But hold on: There might be a twist: Could we sometimes use a 1x2 tile even if it covers a black square? No, explicitly says no black squares should be covered by tiles. So not allowed.
Thus solution works.
But wait, is there any scenario where it's better to not use a 1x2 tile even if y < 2*x? For example, if there is a gap? But segments are contiguous '.'; there is no reason to skip a 1x2 for a pair because it covers exactly two consecutive squares and doesn't interfere with others. However, consider that using a 1x2 tile might force a misalignment that leaves an isolated square later? But since the segment is contiguous, and we can tile it with a greedy approach of pairing from left to right, the number of 1x2 tiles is floor(L/2) regardless of arrangement. But could there be a case where using a 1x2 tile might prevent using another 1x2? No, because if you use a 1x2, it occupies two squares, and the remainder is L-2 squares, still contiguous. So any tiling of a line segment with dominos and monomers yields the same number of dominos if you maximize? Actually, if you want to minimize cost and y < 2x, you want as many dominos as possible, so maximum number of dominos is floor(L/2). There is always a tiling that uses floor(L/2) dominos (just pair adjacent squares). So maximum is floor(L/2). So yes.
Thus the answer is correct.
Edge Cases: Large testcases, but total nm <= 1e5, so reading line by line fine.
Implementation: We'll loop t times. Read line with n,m,x,y. Then for i in range(n): read line. In each row, we can iterate over characters: when we encounter '.', we increase count; when we encounter '*', we process the count (if count>0) and reset count. At end of row, process count.
Compute total.
Note: input may have trailing spaces? Not likely. Use sys.stdin.
Now confirm with examples.
I will implement.
But wait: The problem statement says "if a_{i,j} = "*", then the j-th square in the i-th row should be black; if a_{i,j} = ".", then the j-th square in the i-th row should be white." So '.' is white. So we count '.'.
Now answer must be printed per test case.
Now let's think about potential pitfalls: The condition 2*x <= y means that using two 1x1 is cheaper or equal to one 1x2. But note: if y is slightly more than 2x, then maybe it's still better to use 1x2? No, because 1x2 covers two squares and costs y; two 1x1 cost 2x; if y > 2x, two 1x1 cheaper. So use no 1x2. But what about mixing: Could it be that using a 1x2 in one place and using a 1x1 somewhere else yields lower total than using all 1x1? If y > 2x, then any 1x2 costs more than the two 1x1 it would replace, so replacing any 1x2 with two 1x1 would reduce cost, so all 1x1 is optimal. So condition holds.
If y < 2x, then using a 1x2 is cheaper than two 1x1. However, there might be a scenario where using a 1x2 prevents using another 1x2? Not in a contiguous segment; it's always possible to tile maximum dominos. So yes.
Thus solution is correct.
But is there any possibility to use a 1x2 tile that spans across a black square? No, not allowed. So we only consider contiguous '.'.
Thus solved.
Time complexity: O(total characters) per test, overall O(total characters across testcases) <= 1e5, fine.
Now, coding.
Edge Cases: if y is extremely low, but we have odd length, cost = floor(L/2)*y + x. But could it be better to maybe "waste" a tile by covering a white and a black? Not allowed. So fine.
Now I'll write solution.
Implementation:
import sys
def solve():
import sys
data = sys.stdin.read().split()
# But careful: lines contain '.' and '', which are not whitespace separated. So splitting on whitespace would break rows into strings? Actually rows are strings, and they contain no whitespace, so each row is a single token. So splitting works: after n m x y, next tokens are n rows strings.
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it)); m = int(next(it)); x = int(next(it)); y = int(next(it))
total = 0
if 2x <= y:
# just count all white squares
for i in range(n):
row = next(it)
total += row.count('.') * x
else:
# use min combo
for i in range(n):
row = next(it)
# process segments of '.'
cnt = 0
for ch in row:
if ch == '.':
cnt += 1
else:
total += (cnt//2)*y + (cnt%2)*x
cnt = 0
total += (cnt//2)*y + (cnt%2)*x
out.append(str(total))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
But wait: row.count('.') would count all '.'; that's fine. But we need to handle the else branch properly: using floor(L/2)y + remainderx. However, if y is extremely small, say y=1, x=100, then floor(L/2)*1 + (L%2)*100. That is correct.
But consider if y < x, then maybe we could use a 1x2 tile covering a white and a white? That is what we do. But is there any scenario where we might want to avoid using a 1x2 tile because it would create an odd leftover that forces an expensive 1x1? However, we are forced to cover all squares. Suppose L=3, x=100, y=1. Using two 1x2 tiles is impossible because need two tiles covering 4 squares. Options: (1x2 + 1x1) cost = 1+100=101, or three 1x1 cost=300. So 101 is optimal. Could we do something like place a 1x2 covering squares 2-3? Still same pattern. So 101 is min. So fine.
Now verify with examples: Use the code.
Example input:
4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
..
..
Our code:
Test1: n=1,m=1,x=10,y=1 => 2*x=20 > y=1, else branch. Row ".": cnt=1 => total += (1//2)*1 + (1%2)*10 = 0+10=10. ok.
Test2: n=1,m=2,x=10,y=1 => else branch. row "..": cnt=2 => total += (1)*1 +0=1. ok.
Test3: n=2,m=1,x=10,y=1 => else branch. first row ".": total += 10; second row ".": total += 10 =>20. ok.
Test4: n=3,m=3,x=3,y=7 => 2x=6 <= y? 6 <= 7 true, so if branch: count whites: row0 "..": count '.' =2 => +23=6; row1 "..": count '.' =2 => +6; row2 "..": count '.' =2? Actually "..": characters: first '.'? Wait we need to be careful: row2 is "..": that string is three characters: '.' then '' then '.'? Usually ".." would be dot star dot? But in the input it's ".."? The provided example:
3 3 3 7
..*
..
..
So the third row is ".." which indeed is dot, star, dot? Actually ".." means dot star dot? Let's check: The string literal ".." has characters: index0: '.', index1: '', index2: '.'. So yes two dots. So total '.' count = 2+2+2=6 => total cost = 6*3=18. ok.
So output: 10,1,20,18.
Now, let's consider if there is any case where it's beneficial to use some 1x2 even if 2*x <= y? No.
Thus solution correct.
One more nuance: The problem constraints say y can be up to 1000, x up to 1000. So integer fits.
Now, ensure we handle reading correctly: Using sys.stdin.read().split() works as rows are strings without spaces. However, it would split by whitespace, and each row string is a token. Good.
But careful: Input may have spaces after numbers? Usually not. But it's safe.
Edge case: n=0? No n>=1.
Thus final answer.
But wait: The problem states "the sum of n × m over all testcases doesn't exceed 10^5." So total chars <= 100k, so reading all is fine.
Now, I'll produce final answer.
But let's reflect on potential alternative: Some might think about dynamic programming per row, but simpler is as described.
Now, is there any scenario where using 1x2 tiles on some rows and 1x1 on others could be optimized with a mix even if 2*x <= y? No, because each tile choice only affects a single row.
Thus final.
Now, I'll write the final solution in Python.
One more thing: In the else branch, we could compute total using row processing as described. Alternatively, we could compute number of pairs across whole row: count consecutive '.' segments and compute floor of total whites divided by 2? No, because contiguous segments break pairing across black squares. So we need to treat segments separately.
Thus implementation done.
Now produce final answer.
I'll include comments.
But wait, maybe we can also compute the total pairs by counting number of ".." occurrences? Actually we need to consider that within a segment of length L, number of pairs we can use is floor(L/2). So one could count total white squares and subtract number of segments (since each segment with odd length leaves one unpaired)? Because total pairs = (total_white - number_of_segments_with_odd_length?) Actually total white = sum L_i. Total number of pairs possible = sum floor(L_i/2). That equals (total_white - number_of_odd_segments) // 2 because each odd segment has one leftover. However, we need to know number of odd segments to compute floor(L_i/2) sum. That can be computed by counting segments and their parity. So we could count total white and number of odd-length segments. But we also need to know lengths for modulo. But it's easier to just process as we read.
But let's think: total cost when y < 2*x = (total_white * x?) Actually not. It is: sum over segments: floor(L_i/2)*y + (L_i%2)*x. = y * total_pairs + x * number_of_odd_segments. So we could compute total white and number of odd segments (segments with odd length). But we can also compute total pairs as (total_white - odd_segments)//2. So we just need to count total white squares and number of odd-length '.' segments. That might be simpler than accumulating cnt. But we need to count segments. But processing each character and counting segments is fine.
Implementation: While iterating through row, when we see '.' we increment cnt; when we see '' and cnt>0, we process: if cnt%2==1: odd_segments+=1; pairs += cnt//2; cnt=0. At the end, also process. Then total = pairsy + odd_segments*x. But careful: In our formula, for each segment, we add (L%2)x; that's exactly odd_segments * x (since only odd segments contribute 1x). And pairs is sum L//2.
But note: if a segment length is 0? Not considered. So yes.
But we also need total white squares? Actually we don't need them if we use pairs and odd segments.
But if we are in the branch where 2*x <= y, we just count total white squares.
So we could implement that.
But current implementation is fine.
Now, check if there is any scenario where y < x, but maybe it's cheaper to use two 1x2 tiles to cover three squares? Not possible because you need even number of squares for dominos. So odd segment always requires one 1x1. But could we use a 1x2 tile that covers a white and a white, and then for the odd square maybe use a 1x2 overlapping with a black? Not allowed.
Thus final.
Now, answer.
But hold on: There is a nuance: The 1x2 tile costs y, and 1x1 costs x. If y < x, then it's cheaper to use a 1x2 even if it covers only one white? But you can't because it must cover two squares; if you place it covering a white and a black, it's not allowed. So not possible. But what if we have a segment of length 1? We must use 1x1, cost x. If y < x, then perhaps we could use a 1x2 tile covering the white and an extra white? But no other white adjacent. So forced.
Now, the problem may be from Codeforces Round #1490 (Div 4?) Actually it's problem B? No, it's "Theatre Square" again, but problem "1472B"? Actually it's Codeforces Round #... Anyway.
Thus solution stands.
Now I'll produce final output.
But there is a potential bug: In the else branch, I wrote:
if 2*x <= y:
use all 1x1
else:
use combination
But in the example test case 4, 2x=6, y=7, so 2x <= y? 6 <=7 true, so all 1x1. So correct.
Now, check if there is scenario where 2*x == y: Both choices yield same total. Using all 1x1 is simpler. So fine.
Thus final answer.
I'll output the solution.
But consider if the rows contain spaces? No.
Now code.
Potential improvement: For else branch, we could also use min cost formula: total_white * x - (pairs)(2x - y) if 2x > y? Actually if y < 2x, then each 1x2 saves (2x - y) compared to two 1x1s. So total cost = total_whitex - (pairs)(2x-y). But we need pairs as number of disjoint pairs we can cover. That is total white - odd_segments //2? Actually total pairs = (total_white - odd_segments)/2. So total cost = total_whitex - ((total_white - odd_segments)//2)*(2x-y). But careful integer division. But not necessary.
Anyway.
Thus final answer.
Now, we should include code.
Will the code handle large inputs? Yes.
Now, the answer is ready. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = int(next(it))
x = int(next(it))
y = int(next(it))
total = 0
# When two 1x1 tiles are not more expensive than one 1x2 tile
if 2 * x <= y:
for _ in range(n):
row = next(it)
total += row.count('.') * x
else:
# Use as many 1x2 tiles as possible in each maximal white segment
for _ in range(n):
row = next(it)
cnt = 0
for ch in row:
if ch == '.':
cnt += 1
else:
total += (cnt // 2) * y + (cnt % 2) * x
cnt = 0
total += (cnt // 2) * y + (cnt % 2) * x
out_lines.append(str(total))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()