You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.

The square still has a rectangular shape of $n \times m$ meters. However, the picture is about to get more complicated now. Let $a_{i,j}$ be the $j$-th square in the $i$-th row of the pavement.

You are given the picture of the squares:  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. 

The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:  $1 \times 1$ tiles — each tile costs $x$ burles and covers exactly $1$ square;  $1 \times 2$ tiles — each tile costs $y$ burles and covers exactly $2$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $1 \times 1$ tiles. 

You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.

What is the smallest total price of the tiles needed to cover all the white squares?


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of testcases. Then the description of $t$ testcases follow.

The first line of each testcase contains four integers $n$, $m$, $x$ and $y$ ($1 \le n \le 100$; $1 \le m \le 1000$; $1 \le x, y \le 1000$) — the size of the Theatre square, the price of the $1 \times 1$ tile and the price of the $1 \times 2$ tile.

Each of the next $n$ lines contains $m$ characters. The $j$-th character in the $i$-th line is $a_{i,j}$. If $a_{i,j} = $ "*", then the $j$-th square in the $i$-th row should be black, and if $a_{i,j} = $ ".", then the $j$-th square in the $i$-th row should be white.

It's guaranteed that the sum of $n \times m$ over all testcases doesn't exceed $10^5$.


-----Output-----

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.


-----Example-----
Input
4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.

Output
10
1
20
18



-----Note-----

In the first testcase you are required to use a single $1 \times 1$ tile, even though $1 \times 2$ tile is cheaper. So the total price is $10$ burles.

In the second testcase you can either use two $1 \times 1$ tiles and spend $20$ burles or use a single $1 \times 2$ tile and spend $1$ burle. The second option is cheaper, thus the answer is $1$.

The third testcase shows that you can't rotate $1 \times 2$ tiles. You still have to use two $1 \times 1$ tiles for the total price of $20$.

In the fourth testcase the cheapest way is to use $1 \times 1$ tiles everywhere. The total cost is $6 \cdot 3 = 18$.

Difficulty: EASY
Tags: Amortized analysis

Implementation

Greedy algorithms

Complete search

Dynamic programming
Skill types: Dynamic programming

Amortized analysis

Complete search

Greedy algorithms

Starter code:

Solutions:
from sys import stdin, stdout
t = int(stdin.readline())
for w in range(t):
	(n, m, x, y) = [int(s) for s in stdin.readline().split(' ')]
	val = []
	for i in range(n):
		e = stdin.readline()[:-1]
		count = 0
		for j in e:
			if j == '.':
				count += 1
			else:
				if count:
					val.append(count)
				count = 0
		if count:
			val.append(count)
	if 2 * x <= y:
		ans = sum(val) * x
	else:
		ans = 0
		for i in val:
			if i == 1:
				ans += x
			else:
				ans += y * int(i / 2)
				if i % 2 == 1:
					ans += x
	ans = str(int(ans)) + '\n'
	stdout.write(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	for _ in range(N()):
		(n, m, x, y) = RL()
		gp = [list(input()) + ['*'] for _ in range(n)]
		res = 0
		for i in range(n):
			lst = -1
			now = 0
			for j in range(m + 1):
				if gp[i][j] == '*':
					le = j - lst - 1
					cos1 = le * x
					cos2 = le // 2 * y + le % 2 * x
					now += min(cos1, cos2)
					lst = j
			res += now
		print(res)
main()


T = int(input())
for t in range(T):
	(n, m, x, y) = map(int, input().split())
	lst = []
	for i in range(n):
		lst.append(list(input()))
	tot = 0
	if 2 * x < y:
		price = 2 * x
	else:
		price = y
	for i in range(n):
		for j in range(m - 1):
			if lst[i][j] == '.':
				if lst[i][j + 1] == '.':
					tot += price
					lst[i][j + 1] = '*'
				else:
					tot += x
		if lst[i][m - 1] == '.':
			tot += x
	print(tot)


import sys
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(x * 2, y)
	res = 0
	for i in range(n):
		a = input().split('*')
		res += sum((y * (len(s) // 2) + x * (len(s) % 2) for s in a))
	print(res)


t = int(input())
while t:
	t = t - 1
	(n, m, x, y) = map(int, input().split())
	l = []
	cnt = 0
	hmm = 0
	for i in range(n):
		z = input()
		hmm += z.count('.')
		l.append(list(z))
	for i in range(n):
		for j in range(m - 1):
			z = l[i][j]
			if z == '-':
				continue
			if z == '.' and l[i][j + 1] == '.':
				cnt += y
				l[i][j + 1] = '-'
			elif z == '.':
				cnt += x
		if l[i][m - 1] == '.':
			cnt += x
	print(min(cnt, hmm * x))


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	if y < 2 * x:
		tt = y
	else:
		tt = 2 * x
	tc = 0
	for z in range(n):
		a = input()
		w = []
		for z in range(0, len(a)):
			if a[z] == '.':
				w.append(z)
		b = 0
		while b < len(w) - 1:
			if w[b] + 1 == w[b + 1]:
				tc += tt
				b += 2
			else:
				tc += x
				b += 1
		if b == len(w) - 1:
			tc += x
	print(tc)


def solve(n, m, x, y, a):
	(c_x, c_y) = (0, 0)
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if a[i][j] == 0:
					c_x += 1
		return c_x * x
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == 0 and a[i][j + 1] == 0:
				c_y += 1
				j += 2
			elif a[i][j] == 0:
				c_x += 1
				j += 1
			else:
				j += 1
	return c_x * x + c_y * y

def convert(a):
	re = []
	for i in a:
		if i == '*':
			re.append(1)
		else:
			re.append(0)
	re.append(1)
	return re
test = 1
test = int(input())
for t in range(0, test):
	(n, m, x, y) = [int(x) for x in input().split()]
	arr = []
	for i in range(n):
		a = input()
		arr.append(convert(a))
	price = solve(n, m, x, y, arr)
	print(price)


def f(m, x, y):
	if 2 * x <= y:
		ct = 0
		for i in range(len(m)):
			for j in range(len(m[0])):
				if m[i][j] == '.':
					ct = ct + 1
		return ct * x
	else:
		ct = 0
		for i in range(len(m)):
			j = 0
			while j < len(m[0]):
				if j == len(m[0]) - 1:
					if m[i][j] == '.':
						ct = ct + x
				elif m[i][j] == '.' and m[i][j + 1] == '.':
					ct = ct + y
					j = j + 1
				elif m[i][j] == '.':
					ct = ct + x
				j = j + 1
		return ct
for tc in range(int(input())):
	al = list(map(int, input().split()))
	n = al[0]
	m = al[1]
	x = al[2]
	y = al[3]
	mat = []
	for i in range(n):
		temp = list(input())
		mat.append(temp)
	print(f(mat, x, y))


import math
import sys
from sys import stdin
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = [int(j) for j in input().split()]
	arr = []
	for i in range(n):
		arr.append(input())
	if x * 2 <= y:
		counti = 0
		for i in range(n):
			counti += arr[i].count('.')
		print(counti * x)
	else:
		counti = 0
		countii = 0
		for i in range(n):
			counti += arr[i].count('.')
		for i in range(n):
			temp = m - 1
			j = 0
			try:
				while temp > 0:
					if arr[i][j] == '.' and arr[i][j + 1] == '.':
						countii += 1
						j += 2
					else:
						j += 1
					temp -= 1
			except:
				temp -= 1
		result = countii * y
		counti = counti - 2 * countii
		result += counti * x
		print(result)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split(' ')))
	total = 0
	if 2 * x <= y:
		for __ in range(n):
			total += x * list(input()).count('.')
	else:

		def cost(s):
			return len(s) // 2 * y + len(s) % 2 * x
		for __ in range(n):
			rows = input().split('*')
			total += sum(list(map(cost, rows)))
	print(total)


t = int(input())
for k in range(t):
	(n, m, x, y) = map(int, input().split())
	sq = []
	for i in range(n):
		sq.append(list(input()))
	s = 0
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if sq[i][j] == '.':
					s += x
	else:
		for i in range(n):
			for j in range(m):
				if sq[i][j] == '.' and j < m - 1 and (sq[i][j + 1] == '.'):
					s += y
					sq[i][j] = 'x'
					sq[i][j + 1] = 'x'
				elif sq[i][j] == '.':
					s += x
					sq[i][j] = 'x'
	print(s)


import sys
from math import log2, floor, ceil, sqrt
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	(n, m, x, y) = Ri()
	s = []
	for i in range(n):
		temp = ri()
		s.append(temp)
	if 2 * x <= y:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.':
					cnt += 1
		print(cnt * x)
	else:
		cnt = 0
		flag = list2d(n, m, False)
		for i in range(n):
			for j in range(m - 1):
				if not flag[i][j]:
					if s[i][j] == '.' and s[i][j + 1] == '.':
						cnt += y
						flag[i][j + 1] = True
						flag[i][j] = True
					elif s[i][j] == '.':
						cnt += x
						flag[i][j] = True
			if not flag[i][m - 1] and s[i][m - 1] == '.':
				cnt += x
				flag[i][m - 1] = True
		print(cnt)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	price = 0
	for i in range(n):
		a = input()
		if len(a) == 1:
			if a[0] == '.':
				price += x
		elif 2 * x <= y:
			while True:
				ind = a.find('*')
				if ind == -1:
					price += len(a) * x
					break
				else:
					price += x * ind
					a = a[ind + 1:]
		else:
			while True:
				ind = a.find('*')
				if ind == -1:
					if len(a) % 2 == 0:
						price += y * (len(a) / 2)
					else:
						price += y * (len(a) // 2) + x
					break
				else:
					if ind % 2 == 0:
						price += y * (ind / 2)
					else:
						price += y * (ind // 2) + x
					a = a[ind + 1:]
	print(int(price))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	square = []
	single_tiles = 0
	double_tiles = 0
	for i in range(n):
		square.append(input())
		single_tiles += square[i].count('.')
		double_tiles += square[i].count('..')
	sum = 0
	if 2 * x <= y:
		sum = x * single_tiles
	else:
		sum = double_tiles * y + (single_tiles - 2 * double_tiles) * x
	print(sum)


import math
t = int(input())
for q in range(t):
	(n, m, x, y) = list(map(int, input().split(' ')))
	a = []
	cont = 0
	for k in range(n):
		linha = input()
		a.append(list(linha))
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if j < m - 1:
					if a[i][j + 1] == '.' and y < 2 * x:
						a[i][j] = '*'
						a[i][j + 1] = '*'
						cont += y
					else:
						a[i][j] = '*'
						cont += x
				else:
					a[i][j] = '*'
					cont += x
	print(cont)


import sys
input = sys.stdin.readline

def solve():
	(N, M, F, S) = map(int, input().split())
	L = []
	for i in range(N):
		L.append(list(input()))
	ANSWER = 0
	if S < 2 * F:
		for i in range(N):
			for j in range(M):
				if L[i][j] == '.':
					if j + 1 < M:
						if L[i][j + 1] == '.':
							L[i][j + 1] = '#'
							ANSWER += S
						else:
							ANSWER += F
					else:
						ANSWER += F
		print(ANSWER)
	else:
		for i in range(N):
			for j in range(M):
				if L[i][j] == '.':
					ANSWER += F
		print(ANSWER)
test = int(input())
for _ in range(test):
	solve()


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	su = 0
	for i in range(n):
		s = input()
		j = 0
		while j < m:
			if s[j] == '.':
				if j + 1 < m:
					if s[j + 1] == '.':
						if 2 * x >= y:
							su += y
							j += 2
						else:
							su += 2 * x
							j += 2
					else:
						su += x
						j += 1
				else:
					su += x
					j += 1
			else:
				j += 1
	print(su)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = y if x * 2 > y else x * 2
	ans = 0
	for _ in range(n):
		s = list(map(lambda x: len(x), input().split('*')))
		for i in s:
			if i == 0:
				continue
			if i % 2 == 0:
				ans += i // 2 * y
			else:
				ans += i // 2 * y + x
	print(ans)


def solve(mat, n, m, x, y):
	if x <= y / 2:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					cnt += 1
		return int(x * cnt)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				cnt = 0
				while j < m and mat[i][j] == '.':
					cnt += 1
					j += 1
				ans += cnt // 2 * y + cnt % 2 * x
				while j < m and mat[i][j] == '*':
					j += 1
		return ans

def read():
	t = int(input())
	for i in range(t):
		(n, m, x, y) = list(map(int, input().strip().split()))
		mat = []
		for i in range(n):
			mat.append(input().strip())
		ans = solve(mat, n, m, x, y)
		print(ans)
read()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	c = 0
	if 2 * x > y:
		for i in range(n):
			a = input()
			j = 0
			k = 0
			while j < m and k < m:
				if a[j] == '.':
					j += 1
				else:
					dif = j - k
					j += 1
					k = j
					mod = dif % 2
					div = dif // 2
					ans += div * y + mod * x
			dif = j - k
			mod = dif % 2
			div = dif // 2
			ans += div * y + mod * x
	else:
		for i in range(n):
			a = input()
			c += a.count('.')
		ans = c * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	matrix = [list(input()) for i in range(n)]
	if x * 2 <= y:
		cnt = 0
		for i in matrix:
			cnt += i.count('.')
		print(cnt * x)
	else:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if j < m - 1 and matrix[i][j + 1] == '.' and (matrix[i][j] == '.'):
					matrix[i][j + 1] = '#'
					matrix[i][j] = '#'
					cnt += y
				elif matrix[i][j] == '.':
					cnt += x
		print(cnt)


import sys
input = sys.stdin.readline
t = int(input().strip())
while t:
	ans = 0
	t -= 1
	(n, m, x, y) = map(int, input().split())
	field = list(('.' * m for _ in range(n)))
	for i in range(n):
		field[i] = list(input().strip())
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if field[i][j] == '.':
					ans += x
	else:
		for i in range(n):
			j = 0
			while j + 1 <= m:
				if field[i][j:j + 2] == ['.', '.']:
					j += 2
					ans += y
				elif field[i][j] == '.':
					ans += x
					j += 1
				else:
					j += 1
	print(ans)


n = int(input())
for i in range(n):
	price1 = 0
	price2 = 0
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	matrix = [[0 for x in range(m)] for y in range(n)]
	for k in range(n):
		line = list(input())
		cont = 0
		for z in range(len(line)):
			if cont == 1:
				cont = 0
				continue
			try:
				if line[z] == '.' and line[z] == line[z + 1]:
					cont = 1
					price2 = price2 + y
				elif line[z] == '.':
					price2 = price2 + x
			except IndexError:
				if line[z] == '.':
					price2 = price2 + x
		for z in range(len(line)):
			if line[z] == '.':
				price1 = price1 + x
	if price1 < price2 and price1 != 0:
		print(price1)
	elif price2 == 0:
		print(price1)
	else:
		print(price2)


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
	f = iter(open('B.txt').readlines())

	def input():
		return next(f).strip()
else:
	input = lambda : sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)
import itertools
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	s = []
	for i in range(n):
		s.extend(list(map(len, input().split('*'))))
	res = 0
	for i in s:
		res += y * (i // 2) + x * (i % 2)
	print(res)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	a = [list(input()) for i in range(n)]
	ans = 0
	for j in range(n):
		k = 0
		while k < m:
			if k < m - 1 and a[j][k] == a[j][k + 1] == '.':
				ans += min(x * 2, y)
				k += 1
			elif a[j][k] == '.':
				ans += x
			k += 1
	print(ans)


import math
t = int(input())
while t:
	inp1 = list(map(int, input().strip().split()))[:4]
	(m, n, x, y) = (inp1[0], inp1[1], inp1[2], inp1[3])
	matrix = []
	for i in range(m):
		inside = input()
		list1 = []
		for j in inside:
			list1.append(j)
		matrix.append(list1)
	cost = 0
	for i in range(m):
		j = 0
		while j < n - 1:
			if matrix[i][j] == '.' and matrix[i][j + 1] == '.':
				cost += min(2 * x, y)
				matrix[i][j] == ','
				matrix[i][j + 1] = ','
			elif matrix[i][j] == '.' and matrix[i][j + 1] != '.':
				cost += x
				matrix[i][j] == ','
			j += 1
		if matrix[i][j] == '.':
			cost += x
	print(cost)
	t -= 1


a = int(input())
for i in range(a):
	(n, m, c1, c2) = list(map(int, input().strip().split()))
	c2 = min(2 * c1, c2)
	cost = 0
	for j in range(n):
		ro1 = input()
		ro = []
		for mn in ro1:
			ro.append(mn)
		for k in range(m):
			if ro[k] == '.' and k != m - 1:
				if ro[k + 1] == '.':
					cost += c2
					ro[k] = 0
					ro[k + 1] = 0
				else:
					cost += c1
					ro[k] = 0
			elif ro[k] == '.' and k == m - 1:
				cost += c1
				ro[k] = 0
	print(cost)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [[]]
	for i in range(n):
		if a[-1]:
			a.append([])
		cur = 0
		for j in input():
			if j == '.':
				cur += 1
			elif j == '*':
				if cur:
					a[-1].append(cur)
				cur = 0
			if cur % 2 == 0 and cur != 0:
				a[-1].append(2)
				cur = 0
		if cur:
			a[-1].append(cur)
	if 2 * x <= y:
		summ = 0
		for i in range(len(a)):
			summ += sum(a[i]) * x
		print(summ)
	else:
		summ = 0
		for i in range(len(a)):
			for j in range(len(a[i])):
				if a[i][j] == 1:
					summ += x
				else:
					summ += y
		print(summ)


import math
t = int(input())
for w in range(t):
	(n, m, x, y) = (int(i) for i in input().split())
	l = [0] * n
	c = 0
	for i in range(n):
		l[i] = list(input())
		j = 0
		while j < m:
			if j == m - 1:
				if l[i][j] == '.':
					c += x
					j += 1
				else:
					j += 1
			elif l[i][j] == '.' and l[i][j + 1] == '.':
				c += min(2 * x, y)
				j += 2
			elif l[i][j] == '.':
				c += x
				j += 1
			else:
				j += 1
	print(c)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	(n, m, x, y) = M()
	a = []
	c = 0
	for i in range(n):
		a.append(IN())
	if 2 * x <= y:
		for i in range(n):
			c += a[i].count('.') * x
	else:
		for i in range(n):
			j = 0
			while m - 1 > j:
				if a[i][j] == '.':
					if a[i][j + 1] == '.':
						c += y
						j += 2
					else:
						c += x
						j += 2
				else:
					j += 1
			if a[i][-1] == '.' and j == m - 1:
				c += x
	print(c)


from sys import stdin
import math
a = int(stdin.readline())
for b in range(0, a):
	A = stdin.readline().split()
	n = int(A[0])
	m = int(A[1])
	x = int(A[2])
	y = int(A[3])
	if 2 * x > y:
		AA = 1
	else:
		AA = 0
	start = 0
	cost = 0
	for u in range(0, n):
		r = stdin.readline()
		for v in range(0, m):
			if r[v] == '.':
				if AA == 0:
					cost += x
				elif start == 0:
					if v == m - 1:
						cost += x
					else:
						start = 1
				else:
					cost += y
					start = 0
			elif start == 1:
				cost += x
				start = 0
	print(cost)


import sys
import math
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	f = 0
	if 2 * x <= y:
		y = 2 * x
	for i in range(n):
		j = 0
		while j < m:
			if l[i][j] == '.':
				if l[i][j + 1] == '.':
					f += y
					j += 2
				else:
					f += x
					j += 1
			else:
				j += 1
	print(f)


I = input
exec(int(I()) * "n,m,x,y=map(int,I().split());print(sum(l//2*min(2*x,y)+l%2*x for _ in[0]*n for l in map(len,I().split('*'))));")


from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect
import math

def test_case():
	info = list(map(int, input().split()))
	(n, m, x, y) = info
	mat = []
	for _ in range(n):
		a = list(input())
		mat.append(a)
	ans = 0
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					ans += x
	else:
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					if j + 1 < m and mat[i][j + 1] == '.':
						ans += y
						mat[i][j + 1] = '*'
					else:
						ans += x
	return ans
for _ in range(int(input())):
	ret = test_case()
	if ret != None:
		print(ret)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n, m, x, y) = I()
	an = 0
	for i in range(n):
		g = input()
		j = 0
		while j < m:
			if g[j] == '.' and (j != m - 1 and g[j + 1] == '.'):
				an += min(2 * x, y)
				j += 1
			elif g[j] == '.':
				an += x
			j += 1
	print(an)


for q in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		st = input()
		arr.append(list(st))
	am = 0
	for i in range(n):
		(j, l) = (0, 1)
		te = 0
		while j < m:
			if arr[i][j] == '.':
				te += 1
				while l < m and arr[i][l] == '.':
					te += 1
					l += 1
				if te % 2 == 0:
					am += min(2 * x, y) * (te // 2)
				else:
					am += min(2 * x, y) * (te // 2)
					am += x
				te = 0
			j = l
			l = j + 1
	print(am)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l1 = []
	cost = 0
	for i in range(n):
		l1.append(input())
	mn = min(2 * x, y)
	for i in range(n):
		j = 0
		while j < m:
			if j < m - 1 and l1[i][j] == l1[i][j + 1] == '.':
				cost += mn
				j += 1
			elif l1[i][j] == '.':
				cost += x
			j += 1
	print(cost)


from itertools import groupby
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	Ans = 0
	for i in range(n):
		S = list(input())
		A = [S[0]]
		for i in range(1, m):
			if A[-1] != S[i]:
				A.append(S[i])
		res = [len(list(j)) for (_, j) in groupby(S)]
		for i in range(len(A)):
			if A[i] == '.':
				p = res[i] // 2
				q = res[i] % 2
				Ans += min(p * y + q * x, x * res[i])
	print(Ans)


for a in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	count = 0
	for i in range(n):
		l.append([])
		nn = input()
		for j in range(m):
			if nn[j] == '.':
				l[i].append(0)
			else:
				l[i].append(1)
	if y > x * 2:
		y = 2 * x
	for b in l:
		for k in range(m):
			if k < m - 1:
				if b[k] == 0:
					if b[k + 1] == 0:
						count += y
						b[k + 1] = 1
					else:
						count += x
			elif b[k] == 0:
				count += x
	print(count)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	count1 = 0
	count2 = 0
	if 2 * x < y:
		y = 2 * x
	for i in range(n):
		list2 = input().split()
		listToStr = ''.join(list2)
		count1 = count1 + listToStr.count('..')
		listToStr = listToStr.replace('..', '')
		count2 = count2 + listToStr.count('.')
	print(count1 * y + count2 * x)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	plates = [[1 if v == '*' else 0 for v in input()] for _ in range(n)]
	double_valid = y <= 2 * x
	ones = 0
	doubles = 0
	if not double_valid:
		ones = n * m - sum((sum(s) for s in plates))
	else:
		for row in plates:
			for i in range(m - 1):
				if row[i] == 0:
					row[i] = 1
					if row[i + 1] == 0:
						doubles += 1
						row[i + 1] = 1
					else:
						ones += 1
			if row[-1] == 0:
				ones += 1
	print(x * ones + y * doubles)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if x <= y // 2:
		for _ in range(n):
			string = input()
			ans += string.count('.') * x
	else:
		for _ in range(n):
			string = input()
			long = string.count('..')
			short = string.count('.')
			ans += long * y + (short - long * 2) * x
	print(ans)


import math
for ix in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	tiles = []
	for i in range(n):
		tiles.append(list(input()))
	price = 0
	if 2 * x <= y:
		for j in tiles:
			price += x * j.count('.')
		print(price)
	else:
		for j in tiles:
			sk = 0
			if m == 1 and j[0] == '.':
				price += x
			for k in range(m - 1):
				if sk:
					sk = 0
				elif j[k] == '.':
					if j[k + 1] == '.':
						price += y
						sk = 1
					else:
						price += x
			if j[-1] == '.' and m > 1 and (sk == 0):
				price += x
		print(price)


t = int(input())
for q in range(t):
	(n, m, x, y) = map(int, input().split())
	answer = 0
	for i in range(n):
		s = input()
		j = 1
		while j < m:
			if s[j - 1] == '.':
				if s[j] == '.' and 2 * x > y:
					answer += y
					j += 1
				else:
					answer += x
			if j == m - 1:
				if s[j] == '.':
					answer += x
			j += 1
		if m == 1 and s[0] == '.':
			answer += x
	print(answer)


t = int(input())

def fun(n, m, x, y, a):
	ans = 0
	for i in range(n):
		b = []
		j = 0
		k = 0
		while j < m:
			if a[i][j] == '*':
				j += 1
				k = 0
				continue
			else:
				while j < m and a[i][j] == '.':
					j += 1
					k += 1
				b.append(k)
		for j in b:
			if 2 * x >= y:
				ans += j % 2 * x
				ans += (j - j % 2) / 2 * y
			else:
				ans += j * x
	print(int(ans))
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		b = input()
		a.append(b)
	fun(n, m, x, y, a)


T = int(input())
for _ in range(0, T):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		y = 2 * x
	L = []
	for i in range(0, n):
		s = input()
		L.append(s)
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if L[i][j] == '.' and j + 1 < m and (L[i][j + 1] == '.'):
				ans += y
				j += 2
			elif L[i][j] != '.':
				j += 1
			else:
				ans += x
				j += 1
	print(ans)


for _ in range(int(input())):
	(r, c, o, t) = map(int, input().split())
	if t < o * 2:
		m = 0
		for i in range(r):
			k = input()
			y = k.count('..')
			z = k.count('.') - y * 2
			m += y * t + z * o
		x = m
	else:
		m = 0
		for i in range(r):
			k = input()
			m += k.count('.')
			x = m * o
	print(x)


import sys
file_name = ''
if file_name:
	f = open(file_name, 'r')
else:
	f = sys.stdin
line = f.readline()
t = int(line)
for _ in range(t):
	(n, m, x, y) = [int(x) for x in f.readline().split()]
	x2 = min(2 * x, y)
	cost = 0
	for _ in range(n):
		line = f.readline().rstrip()
		for s in line.split(sep='*'):
			cost += len(s) // 2 * x2 + len(s) % 2 * x
	print(cost)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mapa = ''
	for _ in range(n):
		mapa += input() + 'X'
	(jeden, dwa) = (mapa.count('.'), mapa.count('..'))
	if y >= 2 * x:
		print(jeden * x)
	else:
		print((jeden - dwa * 2) * x + dwa * y)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if y > 2 * x:
		c = 0
		for j in range(n):
			c += sum(list(map(len, input()[:-1].split('*')))) * x
	else:
		c = 0
		for j in range(n):
			for i in list(map(len, input()[:-1].split('*'))):
				if i % 2 == 0:
					c += i // 2 * y
				else:
					c += i // 2 * y + x
	print(c)


cases = int(input())
for _ in range(cases):
	(n, m, x, y) = map(int, input().split())
	w = []
	tot = 0
	for i in range(n):
		w.append(input().split('*'))
	for i in range(len(w)):
		a = w[i]
		for j in range(len(a)):
			m1 = a[j].count('..')
			s2 = m1 * min(2 * x, y)
			r = (len(a[j]) - 2 * m1) * x
			if m1 > 0:
				tot += s2 + r
			else:
				tot += s2
			if m1 == 0:
				m2 = a[j].count('.')
				tot += m2 * x
	print(tot)


import sys
from math import ceil, sqrt, sin, pi, tan
from collections import defaultdict
read = input
import heapq
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
primes = []

def ps(sm):
	if sm < 0:
		return 1
	sq = int(sqrt(sm))
	if sq * sq == sm:
		return 0
	return 1

def check(s, d):
	for el in s:
		if int(el) in d:
			return False
	return True
import math

def nCr(n, r):
	f = math.factorial
	return f(n) / f(r) / f(n - r)

def dfs():
	ans = 0
	st = [(0, 0)]
	vis = [[]]
for _t in range(cin()):
	(n, m, x, y) = mi()
	a = []
	for i in range(n):
		a.append(list(input()))
	nw = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				nw += 1
	if 2 * x <= y:
		print(nw * x)
		continue
	ans = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if j > 0 and a[i][j - 1] == '.':
					ans += 1
					a[i][j] = '*'
	print(x * (nw - 2 * ans) + y * ans)


t = int(input())
i = 0
while i < t:
	(n, m, x, y) = map(int, input().split())
	arr = []
	for a in range(n):
		s = input()
		s = list(s)
		arr.append(s)
	count = 0
	for a in range(n):
		for b in range(m):
			if arr[a][b] == '.' and b + 1 < m and (arr[a][b + 1] == '.'):
				count = count + min(x * 2, y)
				arr[a][b] = '*'
				arr[a][b + 1] = '*'
			elif arr[a][b] == '.':
				count = count + x
	print(count)
	i = i + 1


T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	li = []
	for _ in range(n):
		li.append(input())
	if y >= x * 2:
		count = 0
		for i in range(n):
			for j in range(m):
				if li[i][j] == '.':
					count += 1
		print(count * x)
	else:
		count = 0
		for i in range(n):
			j = 0
			while j < m:
				if li[i][j] == '.':
					if j < m - 1 and li[i][j + 1] == '.':
						count += y
						j += 2
					else:
						count += x
						j += 1
				else:
					j += 1
		print(count)


t = int(input())
for y in range(t):
	(r, c, ruppee, dollar) = map(int, input().split())
	rama = []
	for j in range(r):
		array = [-1]
		string = input()
		for k in range(c):
			if string[k] == '*':
				array.append(k)
		array.append(c)
		array.append(c)
		rama.append(array)
	add = 0
	if 2 * ruppee > dollar:
		dollar = dollar
	else:
		dollar = 2 * ruppee
	i = 0
	while i < r:
		j = 0
		while j <= c:
			if rama[i][j] == rama[i][j + 1]:
				break
			s = rama[i][j + 1] - rama[i][j] - 1
			if s == 1:
				add += ruppee
			elif s > 1:
				add += dollar * (s // 2)
				add += ruppee * (s % 2)
			j += 1
		i += 1
	print(add)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for _ in range(n):
		a = input().strip().split('*')
		for i in a:
			l = len(i)
			if l == 1:
				cost += x
			else:
				cost += min(x * l, l // 2 * y + l % 2 * x)
	print(cost)


t = int(input())
import math
delay = []
for _ in range(t):
	line = input().split()
	ll = [int(l) for l in line]
	n = ll[0]
	m = ll[1]
	x = ll[2]
	y = ll[3]
	oddprice = x
	evenprice = 2 * x if 2 * x < y else y
	ans = 0
	for _ in range(n):
		arr = list(input())
		blob = []
		b = 0
		for ptr in range(m):
			if arr[ptr] == '.':
				b += 1
			else:
				blob.append(b)
				b = 0
		blob.append(b)
		teven = 0
		todd = 0
		for i in blob:
			if i % 2 == 0:
				ans += i // 2 * evenprice
			else:
				ans += i // 2 * evenprice + oddprice
	delay.append(ans)
for d in delay:
	print(d)


t = int(input())
ans = []
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	otv = 0
	if 2 * x <= y:
		var = 1
	else:
		var = 0
	a = [input() for i in range(n)]
	i = 0
	j = 0
	while i < n:
		while j < m:
			if a[i][j] == '.':
				if var == 1:
					j += 1
					otv += x
				elif j != m - 1 and a[i][j + 1] == '.':
					otv += y
					j += 2
				else:
					otv += x
					j += 1
			else:
				j += 1
		i += 1
		j = 0
	ans.append(otv)
for i in ans:
	print(i)


import math
import functools

def foo(n, m, x, y, s):
	res = 0
	canPutTwo = y < 2 * x
	for i in range(n):
		for j in range(m):
			if s[i][j] != '.':
				continue
			if j + 1 < m and s[i][j + 1] == '.' and canPutTwo:
				s[i][j + 1] = '*'
				s[i][j] = '*'
				res += y
			else:
				s[i][j] = '*'
				res += x
	return res
for _ in range(int(input())):
	(n, m, x, y) = [*map(int, input().split())]
	s = []
	for i in range(n):
		s.append([*input()])
	print(foo(n, m, x, y, s))


ans = []
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(str(input())))
	count = 0
	if y <= 2 * x:
		for i in range(n):
			j = 0
			while j < m:
				if mat[i][j] == '.':
					if j + 1 < m and mat[i][j + 1] == '.':
						count += y
						j += 1
					else:
						count += x
				j += 1
	else:
		for i in range(n):
			j = 0
			while j < m:
				if mat[i][j] == '.':
					count += x
				j += 1
	ans.append(count)
for i in ans:
	print(i)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from math import ceil
for _ in range(int(input()) if True else 1):
	(n, m, x, y) = map(int, input().split())
	a = []
	tt = 0
	for __ in range(n):
		a += [input()]
		tt += a[-1].count('.')
	if y >= 2 * x:
		print(x * tt)
		continue
	ans = 0
	cnt = 0
	for i in a:
		j = i + '*'
		for k in j:
			if k == '.':
				cnt += 1
			else:
				ans += y * (cnt // 2) + x * (cnt % 2)
				cnt = 0
	print(ans)


import sys

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	(n, m, x, y) = I()
	c = 0
	doubles = 0
	singles = 0
	for i in range(n):
		arr = input()
		i = 0
		singles += arr.count('.') * x
		while i < m - 1:
			if arr[i] == '.' and arr[i + 1] == '.':
				doubles += y
				i += 2
				continue
			if arr[i] == '.':
				doubles += x
			i += 1
		if i == m - 1 and arr[i] == '.':
			doubles += x
	if doubles == 0:
		print(singles)
		continue
	print(min(singles, doubles))


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	(n, m, x, y) = [int(x) for x in input().split()]
	total = 0
	for i in range(n):
		row = input().strip()
		arr = []
		t = 0
		for el in row:
			if el == '.':
				t += 1
			elif t != 0:
				arr.append(t)
				t = 0
		if t != 0:
			arr.append(t)
		if 2 * x <= y:
			total += sum(arr) * x
		else:
			for el in arr:
				total += y * (el // 2)
				if el & 1:
					total += x
	print(total)
tt = int(input())
for test in range(tt):
	solve()


T = int(input())
for _ in range(T):
	(n, m, x, y) = list(map(int, input().split()))
	a = [input() for _ in range(n)]
	(one, two) = (0, 0)
	for i in range(n):
		last = a[i][0]
		white = 1 if last == '.' else 0
		for j in range(1, m):
			if a[i][j] == '.':
				white += 1
			else:
				two += white // 2
				one += white % 2
				white = 0
		if white > 0:
			two += white // 2
			one += white % 2
	if y <= 2 * x:
		ans = one * x + two * y
	else:
		ans = one * x + 2 * two * x
	print(ans)


t = int(input())
ans = []
for i in range(t):
	cost = 0
	(n, m, one, two) = [int(i) for i in input().split()]
	if one * 2 > two:
		flag = True
	else:
		flag = False
	d = []
	for i in range(n):
		d.append(input())
		if flag == True:
			if d[i].find('..') != -1:
				cost += d[i].count('..') * two
				d[i] = d[i].replace('..', '**')
		if d[i].find('.') != -1:
			cost += d[i].count('.') * one
	ans.append(cost)
for i in ans:
	print(i)


import math
for _ in range(int(input())):
	s = [int(i) for i in input().split()]
	n = s[0]
	m = s[1]
	x = s[2]
	y = s[3]
	Su1 = 0
	Su2 = 0
	for i in range(n):
		s2 = [i for i in input().split('*')]
		for xx in s2:
			k = len(str(xx))
			if k % 2 == 0:
				Su1 += k * x
				Su2 += k // 2 * y
			else:
				Su1 += k * x
				Su2 += k // 2 * y + x
	print(min(Su1, Su2))


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
for _ in range(int(fastinput())):
	(n, m, x, y) = map(int, fastinput().split())
	y = min(y, 2 * x)
	ans = 0
	for _ in range(n):
		s = list(input())
		count = 0
		for i in range(m - 1):
			if s[i] == '.' and s[i + 1] == '.':
				s[i] = '*'
				s[i + 1] = '*'
				count += y
			elif s[i] == '.':
				count += x
				s[i] = '*'
		if s[m - 1] == '.':
			count += x
		ans += count
	print(ans)


t = int(input())
while t:
	t -= 1
	(n, m, x, y) = list(map(int, input().split()))
	dot = 0
	saath = 0
	for i in range(n):
		s = input()
		j = 0
		while j < m - 1:
			if s[j] == '.' and s[j + 1] == '.':
				saath += 1
				j += 2
			else:
				j += 1
		dot += s.count('.')
	a1 = dot * x
	a2 = saath * y + (dot - 2 * saath) * x
	print(min(a1, a2))


def read_int():
	n = int(input())
	return n

def read_ints():
	ints = [int(x) for x in input().split(' ')]
	return ints

def solve(n, m, x, y, a):
	A = []
	for row in a:
		nrow = []
		for char in row:
			if char == '*':
				nrow.append(0)
			else:
				nrow.append(1)
		A.append(nrow)
	if y >= 2 * x:
		res = 0
		for row in A:
			res += sum(row)
		return res * x
	else:
		res = {1: 0, 2: 0}
		for row in A:
			i = 0
			while i < m:
				if i != m - 1:
					if row[i] + row[i + 1] == 2:
						res[2] += 1
						i += 2
					elif row[i] == 1:
						res[1] += 1
						i += 2
					else:
						i += 1
				else:
					if row[i] == 1:
						res[1] += 1
					i += 1
		return res[1] * x + res[2] * y
t = read_int()
for case in range(t):
	(n, m, x, y) = read_ints()
	a = []
	for i in range(n):
		row = input()
		a.append(row)
	sol = solve(n, m, x, y, a)
	print(sol)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		str = input()
		aa = str.count('..')
		a = str.count('.')
		if y / 2 < x:
			ans += aa * y + (a - 2 * aa) * x
		else:
			ans += a * x
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	if y > 2 * x:
		y = 2 * x
	out = 0
	for i in range(n):
		line = input().strip()
		curr = 0
		for c in line:
			if c == '.':
				out += y * curr
				curr = 1 - curr
			else:
				out += x * curr
				curr = 0
		out += x * curr
	print(out)


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	matrix = []
	for j in range(n):
		s = str(input())
		matrix.append(s)
	a = min(2 * x, y)
	s = 0
	for j in range(n):
		k = 0
		while k < m:
			if k != m - 1 and matrix[j][k] == '.' and (matrix[j][k + 1] == '.'):
				s += a
				k += 2
			elif k != m - 1 and matrix[j][k] == '.' and (matrix[j][k + 1] != '.'):
				s += x
				k += 1
			elif k == m - 1 and matrix[j][k] == '.':
				s += x
				k += 1
			else:
				k += 1
	print(s)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	count = 0
	for i in range(n):
		s = input().split('*')
		for j in s:
			le = len(j)
			if le % 2 == 0 and le > 0:
				if 2 * x >= y:
					count += le // 2 * y
				else:
					count += le * x
			elif le % 2 != 0 and le > 0:
				if 2 * x >= y:
					count += le // 2 * y + x
				else:
					count += le * x
	print(count)


t = int(input())
for tc in range(t):
	s = input().split()
	(n, m, x, y) = (int(s[0]), int(s[1]), int(s[2]), int(s[3]))
	co = 0
	for j in range(n):
		si = input().split('*')
		for i in si:
			if len(i) == 0:
				pass
			elif len(i) % 2 == 0:
				if 2 * x >= y:
					co += len(i) // 2 * y
				else:
					co += len(i) * x
			elif 2 * x >= y:
				co += len(i) // 2 * y + x
			else:
				co += len(i) * x
	print(co)


T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	arr = [input() for _ in range(n)]
	res = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if arr[i][j] == '*':
				if cnt == 1:
					res += x
				cnt = 0
			elif arr[i][j] == '.':
				cnt += 1
				if cnt == 2:
					if x * 2 < y:
						res += x * 2
					else:
						res += y
					cnt = 0
		if cnt == 1:
			res += x
	print(res)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	total = 0
	for _ in range(n):
		l.append(input())
	for n in l:
		if n.count('..') > 0:
			total += min(n.count('..') * y + abs(n.count('.') - n.count('..') * 2) * x, n.count('.') * x)
		else:
			total += n.count('.') * x
	print(total)


t = int(input())
for i in range(t):
	(r, c, x, y) = map(int, input().split())
	l1 = []
	l2 = []
	for j in range(r):
		s = input()
		l1 = list(s)
		l2.append(l1)
	if 2 * x <= y:
		c = 0
		for j in range(r):
			c = c + l2[j].count('.')
		print(c * x)
	else:
		al = 0
		for j in range(r):
			al = al + l2[j].count('.')
		c = 0
		for j in l2:
			ct = 0
			for k in j:
				if k == '.':
					ct += 1
				elif k == '*':
					ct = 0
				if ct == 2:
					c += 1
					ct = 0
		singles = al - c * 2
		ans = c * y + singles * x
		print(ans)


def calculaResultado(qtdBranco, qtdAdj, valor1, valor2):
	result1 = qtdBranco * valor1 + qtdAdj * valor2
	result2 = qtdBranco * valor1 + qtdAdj * 2 * valor1
	if result1 <= result2:
		print(result1)
	else:
		print(result2)
numeroTestes = int(input())
for count in range(0, numeroTestes):
	(linhas, colunas, valor1, valor2) = [int(i) for i in input().split()]
	qtdBranco = 0
	qtdAdj = 0
	for linha in range(0, linhas):
		linhaInput = input()
		linhaInput = linhaInput.replace(' ', '')
		indice = 0
		while indice < colunas:
			if linhaInput[indice] == '.':
				qtdBranco += 1
				if indice + 1 < colunas:
					if linhaInput[indice + 1] == '.':
						qtdAdj += 1
						qtdBranco -= 1
						indice += 1
			indice += 1
	calculaResultado(qtdBranco, qtdAdj, valor1, valor2)


t = int(input())
for case in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	cost = 0
	for _ in range(n):
		line = input()
		prev = False
		for char in line:
			if char == '.' and prev:
				prev = False
				cost += y
			elif char == '.' and (not prev):
				prev = True
			elif char == '*' and prev:
				prev = False
				cost += x
		if prev:
			cost += x
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		b = 0
		for _ in range(n):
			a = input()
			for i in range(m):
				if a[i] == '.':
					b += x
		print(b)
	else:
		b = 0
		for _ in range(n):
			a = input()
			k = 0
			for i in range(m):
				if a[i] == '*':
					b += k // 2 * y + k % 2 * x
					k = 0
				else:
					k += 1
			b += k // 2 * y + k % 2 * x
		print(b)


from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7
for Ti in range(int(input().strip())):
	(n, m, x, y) = [int(x) for x in input().strip().split()]
	y = min(y, 2 * x)
	s = []
	for ni in range(n):
		s.append(input().strip())
	ans = 0
	for si in s:
		prev = '*'
		cnt = 0
		for sj in si:
			if sj == '*':
				ans += cnt // 2 * y + cnt % 2 * x
				cnt = 0
				prev = '*'
			else:
				cnt += 1
				prev = '.'
		ans += cnt // 2 * y + cnt % 2 * x
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	y = min(y, 2 * x)
	for __ in range(n):
		s = input()
		i = 0
		while i < m:
			if s[i] == '*':
				i += 1
				continue
			j = i
			while j + 1 < m and s[j + 1] == '.':
				j += 1
			l = j - i + 1
			ans += l % 2 * x + l // 2 * y
			i = j + 1
	print(ans)


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
for tt in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append(input())
	cost = 0
	for r in range(n):
		c = 0
		while c < m:
			if c != m - 1:
				if grid[r][c] == grid[r][c + 1] == '.':
					cost += min(2 * x, y)
					c += 1
				elif grid[r][c] == '.':
					cost += x
			elif grid[r][c] == '.':
				cost += x
			c += 1
	print(cost)


import sys
import math
import collections
isOnline = not False
istest = not False
if not isOnline:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
inp = lambda : sys.stdin.readline()
m = 10 ** 9 + 7

def nCr(n, r):
	if n - r < 0:
		return 0
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
testcase = 1
if istest:
	testcase = int(input())
for _ in range(testcase):
	(n, m, x, y) = map(int, input().split())
	a = []
	ans = 0
	y = min(2 * x, y)
	for i in range(n):
		s = input()
		j = 0
		while j < len(s):
			if s[j] == '*':
				j += 1
				continue
			if s[j] == '.' and j + 1 < len(s) and (s[j + 1] == '.'):
				ans += y
				j += 2
				continue
			if s[j] == '.':
				ans += x
				j += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr += [input()]
	y = min(2 * x, y)
	ans = 0
	for i in range(n):
		temp = []
		count = 0
		for j in range(m):
			if arr[i][j] == '.':
				count += 1
			else:
				if count != 0:
					temp += [count]
				count = 0
		if count != 0:
			temp += [count]
		for aa in temp:
			if aa % 2 == 0:
				ans += y * aa // 2
			else:
				ans += y * (aa // 2) + x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c = 0
	for _ in range(n):
		s = input()
		i = 0
		while i < m:
			if s[i] == '.':
				j = i
				while j < m and s[j] == '.':
					j += 1
				c += min((j - i) // 2 * y + (j - i) % 2 * x, (j - i) * x)
				i = j
			else:
				i += 1
	print(c)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	c1 = 0
	c2 = 0
	for j in range(n):
		a1 = input()
		a.append(a1)
	if m == 1:
		for k in range(n):
			if a[k][0] == '.':
				c1 += x
		print(c1)
	else:
		for k in range(n):
			j = 0
			while j < m:
				if a[k][j] == '.':
					if j < m - 1:
						if a[k][j + 1] == a[k][j]:
							c1 += y
							c2 += 2 * x
						else:
							c1 += x
							c2 += x
					else:
						c1 += x
						c2 += x
					j += 2
				else:
					j += 1
		print(min(c1, c2))


import sys
from collections import defaultdict as dd
from collections import deque

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def bo(i):
	return ord(i) - ord('a')
t = fi()
while t > 0:
	t -= 1
	(n, m, x, y) = mi()
	a = []
	z = 0
	for i in range(n):
		p = si()
		a.append(p)
		z += p.count('.')
	if 2 * x <= y or m == 1:
		print(z * x)
	else:
		c = 0
		vis = [[0 for i in range(m + 1)] for j in range(n + 1)]
		for i in range(n):
			j = 0
			while j <= m - 1:
				if vis[i][j] or a[i][j] == '*':
					j += 1
					continue
				if j < m - 1 and a[i][j] == '.' and (a[i][j + 1] == '.'):
					vis[i][j] = 1
					vis[i][j + 1] = 1
					j += 2
					c += y
				elif a[i][j] == '.':
					vis[i][j + 1] = 1
					c += x
					j += 1
		print(c)


t = int(input())
x = []
y = []
m = []
n = []
berl = []
for i in range(t):
	(na, ma, xa, ya) = list(map(int, input().split()))
	n.append(na)
	m.append(ma)
	x.append(xa)
	y.append(ya)
	k = []
	for j in range(na):
		h = list(map(str, input()))
		k.append(h)
	berl.append(k)

def berland(n, m, x, y, berl):
	q = []
	for i in range(n):
		q.append([])
		for j in range(m):
			q[i].append(-1)
		cost = 0
	if 2 * x < y:
		for i in range(n):
			for j in range(m):
				if berl[i][j] == '.':
					cost += x
	elif m == 1:
		for i in range(n):
			if berl[i][0] == '.':
				cost += x
	elif m == 2:
		for i in range(n):
			if berl[i][0] == '.' and berl[i][1] == '.':
				cost += y
			elif berl[i][0] == '.' or berl[i][1] == '.':
				cost += x
	else:
		for i in range(n):
			for j in range(m - 1):
				if berl[i][j] == '.' and berl[i][j + 1] == '.' and (q[i][j] == -1) and (q[i][j + 1] == -1):
					cost += y
					q[i][j] = 0
					q[i][j + 1] = 0
			for j in range(m):
				if berl[i][j] == '.' and q[i][j] == -1:
					q[i][j] = 0
					cost += x
	return cost
for i in range(t):
	g = berland(n[i], m[i], x[i], y[i], berl[i])
	print(g)


t = int(input())
for z in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		arr.append(list(input()))
	p = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.':
				if j != m - 1:
					if arr[i][j + 1] == '.':
						if 2 * x > y:
							p += y
							arr[i][j + 1] = '*'
						else:
							p += x
					else:
						p += x
				else:
					p += x
	print(p)


import math
T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	two = 0
	one = 0
	for i in range(n):
		s = input()
		cnt = 0
		for j in range(m):
			if s[j] == '.':
				cnt += 1
			else:
				two += cnt // 2
				one += cnt % 2
				cnt = 0
		two += cnt // 2
		one += cnt % 2
	if y > 2 * x:
		ans = (one + two * 2) * x
	else:
		ans = two * y + one * x
	print(ans)


import sys
input = sys.stdin.readline
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, c, d) = map(int, input().split())
	d = min(d, 2 * c)
	ans = 0
	for i in range(n):
		tile = input().rstrip()
		flg = 0
		for j in range(m):
			if tile[j] == '*':
				if flg:
					ans += c
				flg = 0
			elif flg:
				flg = 0
				ans += d
			else:
				flg = 1
		if flg:
			ans += c
	print(ans)


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout
from collections import defaultdict

def solve(arr, n):
	pass

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = list(map(int, input().strip().split()))
		a = [input() for __ in range(n)]
		if 2 * x <= y:
			cnt = 0
			for i in range(n):
				cnt += a[i].count('.')
			print(cnt * x)
			continue
		cst = 0
		for i in range(n):
			j = 0
			while j < m:
				if a[i][j] == '.':
					if j + 1 < m and a[i][j + 1] == '.':
						cst += y
						j += 2
					else:
						cst += x
						j += 1
				else:
					j += 1
		print(cst)
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(n, m, x, y) = list(map(int, input().split()))
		cost = 0
		for i in range(n):
			diulei = input()
			s = 0
			while s <= m - 1:
				curele = diulei[s]
				if s < m - 1:
					nextele = diulei[s + 1]
					if curele == '.' and nextele == '.':
						if y >= 2 * x:
							cost += 2 * x
						else:
							cost += y
						s += 1
					elif curele == '.':
						cost += x
				elif curele == '.':
					cost += x
				s += 1
		yield cost
t = int(input())
ans = gift()
print(*ans, sep='\n')


t = input()
t = int(t)
for tt in range(0, t):
	(n, m, x, y) = input().split()
	(n, m, x, y) = (int(n), int(m), int(x), int(y))
	y = min(2 * x, y)
	cost = 0
	for i in range(0, n):
		s = input()
		j = 0
		while j < m:
			if j < m - 1:
				if s[j] == '.':
					if s[j + 1] == '.':
						cost += y
						j += 1
					else:
						cost += x
			elif s[j] == '.':
				cost += x
			j += 1
	print(cost)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c = 0
	for q in range(n):
		s = input()
		a = s.count('..')
		b = s.count('.')
		if y < 2 * x:
			c += a * y + (b - 2 * a) * x
		else:
			c += b * x
	print(c)


t = int(input())
for i in range(t):
	a = 0
	b = 0
	(n, m, x, y) = map(int, input().split())
	for j in range(n):
		s = input()
		a += s.count('.')
		b += s.count('..')
	if y > 2 * x or b == 0:
		print(x * a)
	else:
		print(int(y * b + x * (a - b * 2)))


for i in range(int(input())):
	a = list(map(int, input().split()))
	n = a[0]
	m = a[1]
	x = a[2]
	y = a[3]
	tab = []
	for j in range(n):
		tab += input()
	if x <= y / 2:
		cont = 0
		for j in range(n):
			for k in range(m):
				if tab[j * m + k] == '.':
					cont += 1
		print(int(cont * x))
	else:
		cont = 0
		cont1 = 0
		z = 0
		for j in range(n):
			for k in range(m):
				if z == 0:
					if tab[j * m + k] == '.':
						z = 1
				else:
					if tab[j * m + k] == '.':
						cont1 += 1
					else:
						cont += 1
					z = 0
			if z == 1:
				z = 0
				cont += 1
		print(int(cont * x + cont1 * y))


for i in range(int(input())):
	ans = 0
	(n, m, x, y) = map(int, input().split())
	for j in range(n):
		line = input()
		length = 0
		for k in range(m):
			if line[k] == '*':
				if length == 1:
					ans += x
				else:
					ans += min(length * x, length // 2 * y + length % 2 * x)
				length = 0
			elif k == m - 1:
				length += 1
				if length == 1:
					ans += x
				else:
					ans += min(length * x, length // 2 * y + length % 2 * x)
			else:
				length += 1
	print(ans)


T = int(input())
for i in range(T):
	nums = input().split()
	(n, m, x, y) = (int(nums[0]), int(nums[1]), int(nums[2]), int(nums[3]))
	Mat = []
	for i in range(n):
		row = str(input())
		Row = []
		for Char in row:
			if Char == '.':
				Row.append(1)
			else:
				Row.append(0)
		Mat.append(Row)
	deci = False
	cost = 0
	if 2 * x <= y:
		deci = True
	for i in range(n):
		for j in range(m):
			if Mat[i][j] == 1:
				if j != m - 1:
					if deci:
						cost += x
						Mat[i][j] = 0
					elif Mat[i][j + 1] == 1:
						cost += y
						Mat[i][j + 1] = 0
					else:
						cost += x
				else:
					cost += x
					Mat[i][j] = 0
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		b = input()
		a.append(b)
	c = 0
	k = 0
	for i in range(n):
		j = 0
		while j < m - 1:
			if a[i][j] == '.' and a[i][j + 1] == '.':
				c += min(y, 2 * x)
				j += 2
			elif a[i][j] == '.':
				c += x
				j += 1
			else:
				j += 1
			k = j
		if k == m - 1:
			if a[i][k] == '.':
				c += x
	print(c)


import sys
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

def solve():
	(n, m, x, y) = I()
	ans = 0
	for i in range(n):
		s = input()
		count = 0
		for j in range(m):
			if s[j] == '.':
				count += 1
			else:
				ans += min(x * count, count // 2 * y + count % 2 * x)
				count = 0
		ans += min(x * count, count // 2 * y + count % 2 * x)
	print(ans)
(t,) = I()
while t:
	t -= 1
	solve()


kl = int(input())
for l in range(kl):
	(n, m, x, y) = map(int, input().split())
	a = [''] * n
	sm = 0
	for i in range(n):
		a[i] = input()
	if 2 * x <= y:
		for i in range(n):
			sm += a[i].count('.')
		sm *= x
	else:
		(kx, ky) = (0, 0)
		for i in range(n):
			a[i] = '0' + a[i]
			pr = m + 1
			for j in range(1, m + 1):
				if a[i][j] == '.':
					if a[i][j - 1] == '.' and pr == j - 1:
						ky += 1
						kx -= 1
					else:
						kx += 1
						pr = j
		sm = kx * x + ky * y
	print(sm)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
ans = []
for _ in range(ii()):
	(n, m, x, y) = mi()
	p = [input() for i in range(n)]
	t = 0
	for i in range(n):
		p2 = p[i].count('..')
		t += p2 * min(2 * x, y) + (p[i].count('.') - 2 * p2) * x
	ans.append(t)
print(*ans, sep='\n')


def solve(a, n, m, x, y):
	cost = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j + 1 < m and a[i][j] == a[i][j + 1]:
					cost += y
					j += 2
				else:
					cost += x
					j += 1
			else:
				j += 1
	return cost
t = int(input())
ans = []
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	c = 0
	for _ in range(n):
		s = input()
		c += s.count('.')
		a.append(s)
	if 2 * x < y:
		ans.append(c * x)
	else:
		res = solve(a, n, m, x, y)
		ans.append(res)
ans = list(map(str, ans))
print('\n'.join(ans))


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	r = 0
	for j in range(a):
		l = input()
		l = l.replace(' ', '')
		l = l.split('*')
		if 2 * c <= d:
			for k in l:
				r += len(k) * c
		else:
			for k in l:
				r += d * (len(k) // 2)
				r += c * (len(k) % 2)
	print(r)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		p = pairp = 0
		for i in range(n):
			row = input()
			p += row.count('.')
			j = 0
			while j < m:
				if j <= m - 2 and row[j] == '.' and (row[j + 1] == '.'):
					pairp += y
					j += 1
				elif row[j] == '.':
					pairp += x
				j += 1
		print(min(p * x, pairp))
main()


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	if y > 2 * x:
		y = 2 * x
	cost = 0
	for i in range(n):
		cnt = 0
		tile = input()
		for j in range(m):
			if tile[j] == '*':
				cost += cnt // 2 * y + cnt % 2 * x
				cnt = 0
			else:
				cnt += 1
		cost += cnt // 2 * y + cnt % 2 * x
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	t = []
	for i in range(n):
		t.append([i for i in input()])
	if 2 * x < y:
		y = 2 * x
	res = 0
	for i in range(n):
		j = 0
		while j < m:
			if t[i][j] == '.':
				posi = j
				while posi + 1 < m and t[i][posi] == '.' and (t[i][posi + 1] == '.'):
					posi += 1
				if (posi - j + 1) % 2 == 0:
					res += y * ((posi - j + 1) // 2)
				else:
					res += x + y * ((posi - j) // 2)
				j = posi + 1
				continue
			j += 1
	print(res)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	f = []
	p = 0
	for k in range(n):
		b = list(input())
		s = b.count('.')
		if s == m:
			p += min(s * x, s // 2 * y + s % 2 * x)
		elif s == 0:
			p += 0
		else:
			u = 0
			for k in b:
				if k == '.':
					u += 1
				elif k == '*':
					p += min(u * x, u // 2 * y + u % 2 * x)
					u = 0
			if u > 0:
				p += min(u * x, u // 2 * y + u % 2 * x)
	print(p)


from sys import stdin
input = stdin.readline
t = int(input())
while t > 0:
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		subMat = list(input())
		mat.append(subMat)
	dp = [[0 for j in range(m)] for i in range(n)]
	dp[0][0] = x if mat[0][0] == '.' else 0
	for i in range(1, n):
		if mat[i][0] == '.':
			dp[i][0] = x
		else:
			dp[i][0] = 0
	total = 0
	for i in range(n):
		for j in range(1, m):
			if mat[i][j] == '*':
				dp[i][j] = dp[i][j - 1]
			elif mat[i][j - 1] == '*':
				dp[i][j] = x + dp[i][j - 1]
			else:
				s = dp[i][j - 1] - x
				if x + x < y:
					dp[i][j] = x + dp[i][j - 1]
				else:
					dp[i][j] = s + y
					mat[i][j] = '*'
		total += dp[i][m - 1]
	print(total)
	t -= 1


for _ in range(int(input())):
	(n, m, x, y) = [int(x) for x in input().split()]
	matr = []
	for i in range(n):
		row = input()
		matr.append(row)
	coun1 = 0
	coun2 = 0
	for i in range(n):
		j = 0
		while j < m:
			if matr[i][j] == '.':
				coun1 += 1
				if j < m - 1 and matr[i][j + 1] == '.':
					coun2 += 1
					j += 2
				else:
					j += 1
			else:
				j += 1
	print((coun1 + coun2) * x if y >= 2 * x else coun2 * y + (coun1 + coun2 - 2 * coun2) * x)


import math
t = int(input())
i = 0
while i < t:
	(n, m, x, y) = list(map(int, input().split()))
	j = 0
	a = []
	while j < n:
		b = input()
		a.append(b)
		j += 1
	j = 0
	summer = 0
	while j < n:
		k = 0
		whiteCounter = 0
		while k < m:
			if a[j][k] == '*':
				summer += min(whiteCounter // 2 * y + whiteCounter % 2 * x, whiteCounter * x)
				whiteCounter = 0
			else:
				whiteCounter += 1
			k += 1
		summer += min(whiteCounter // 2 * y + whiteCounter % 2 * x, whiteCounter * x)
		j += 1
	print(summer)
	i += 1


def solve(n, m, x, y, lis):
	white = 0
	seq = 0
	count = []
	for i in range(n):
		for j in range(m):
			if lis[i][j] == '.':
				white += 1
				seq += 1
			else:
				count.append(seq)
				seq = 0
		count.append(seq)
		seq = 0
	if 2 * x <= y:
		return x * white
	left = 0
	ans = 0
	for item in count:
		ans += y * (item // 2)
		left += item % 2
	ans += left * x
	return int(ans)
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	lis = []
	for i in range(n):
		lis.append(list(input()))
	print(solve(n, m, x, y, lis))


t = int(input())
for inh in range(0, t):
	(n, m, x, y) = map(int, input().split())
	(s, d) = ([], [])
	for i in range(0, n):
		ss = input()
		k = 0
		for j in range(0, m):
			if ss[j] == '.':
				k += 1
			else:
				if k == 1:
					s.append('A')
				k = 0
			if k == 2:
				k = 0
				d.append('A')
		if k == 1:
			s.append('A')
	ans = x * len(s)
	z = min(y, x * 2)
	ans += z * len(d)
	print(ans)


def solve():
	(n, m, x, y) = map(int, input().split())
	arr = []
	cost = 0
	for i in range(n):
		arr.append(input())
	if m == 1:
		for i in range(n):
			if arr[i][0] == '.':
				cost += x
		return cost
	for i in range(n):
		j = 0
		while j < m:
			if j + 1 < m and (arr[i][j] == '.' and arr[i][j + 1] == '.'):
				if 2 * x < y:
					cost += 2 * x
				else:
					cost += y
				j += 2
			elif arr[i][j] == '.':
				cost += x
				j += 1
			else:
				j += 1
	return cost
for _ in range(int(input())):
	print(solve())


import sys
input = sys.stdin.readline

def prog():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		matrix = [input().strip() for i in range(n)]
		if y < 2 * x:
			price = 0
			for row in matrix:
				curr_length = 0
				for a in row:
					if a == '.':
						curr_length += 1
					elif curr_length:
						price += curr_length // 2 * y
						if curr_length % 2 == 1:
							price += x
						curr_length = 0
				if curr_length:
					price += curr_length // 2 * y
					if curr_length % 2 == 1:
						price += x
			print(price)
		else:
			white = 0
			for row in matrix:
				for a in row:
					if a == '.':
						white += 1
			print(x * white)
prog()


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(a) for a in input().split()]
	count_1 = 0
	count_2 = 0
	for j in range(n):
		prev_white = False
		m_s = input()
		for k in m_s:
			if k == '.':
				if prev_white:
					count_1 -= 1
					count_2 += 1
					prev_white = False
				else:
					count_1 += 1
					prev_white = True
			else:
				prev_white = False
	if y > 2 * x:
		print(count_1 * x + count_2 * 2 * x)
	else:
		print(count_1 * x + count_2 * y)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	mp = [input() for _ in range(n)]
	if y >= 2 * x:
		print(x * sum((sum((1 for x in row if x == '.')) for row in mp)))
	else:
		tot = 0
		for row in mp:
			i = 0
			while i < m:
				if i + 1 < m and row[i] == row[i + 1] == '.':
					tot += y
					i += 2
				elif row[i] == '.':
					tot += x
					i += 1
				else:
					i += 1
		print(tot)


from sys import stdin, stdout
import math

def solve():
	cases = int(input())
	for case in range(1, cases + 1):
		(n, m, x, y) = [int(n) for n in input().split()]
		if x * 2 < y:
			ans = 0
			for i in range(n):
				s = input()
				ans += s.count('.') * x
			print(ans)
		else:
			one = 0
			two = 0
			for i in range(n):
				s = input()
				j = 0
				while j < len(s) - 1:
					if s[j] == '.':
						if s[j + 1] == '.':
							two += 1
							j = j + 2
						elif s[j + 1] == '*':
							one += 1
							j = j + 2
					elif s[j] == '*':
						j = j + 1
				if j == len(s) - 1:
					if s[j] == '.':
						one += 1
			ans = one * x + two * y
			print(ans)
solve()


import math
for t in range(int(input())):
	(n, m, x, y) = (int(i) for i in input().split(' '))
	a = [0] * n
	for i in range(n):
		a[i] = [{'.': 1, '*': 0}[i] for i in list(input())]
	if x <= y * 0.5 or m <= 1:
		print(sum([sum(i) for i in a]) * x)
	else:
		cost = 0
		for i in range(n):
			j = 0
			while j < m:
				if j < m - 1 and a[i][j] and a[i][j + 1]:
					cost += y
					j += 1
				elif a[i][j]:
					cost += x
				j += 1
		print(cost)


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math

def solution(A, n, m, x, y):
	total = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if A[i][j] == '*':
				j += 1
				continue
			if A[i][j] == '.' and j + 1 < m and (A[i][j + 1] == '.'):
				if 2 * x <= y:
					total += 2 * x
				else:
					total += y
				j += 2
				continue
			total += x
			j += 1
		i += 1
	return total

def main():
	T = read_int()
	for i in range(T):
		(n, m, x, y) = read_int_array()
		A = []
		for j in range(n):
			A.append(read_string())
		x = solution(A, n, m, x, y)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


def solve():
	ans = 0
	if 2 * x <= y:
		print(s * x)
		return
	else:
		for r in sq:
			r += '*'
			i = 0
			while i < m:
				if r[i] == '.':
					if r[i + 1] == '.':
						ans += y
						i += 1
					else:
						ans += x
				i += 1
	print(ans)
	return
t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	sq = []
	s = 0
	for i in range(n):
		inn = input()
		sq.append(inn)
		s += sum([int(c == '.') for c in inn])
	solve()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	res = 0
	if x * 2 > y:
		for __ in range(n):
			s = input()
			i = 0
			while i < m:
				if i < m - 1 and s[i] == s[i + 1] == '.':
					res += y
					i += 1
				elif s[i] == '.':
					res += x
				i += 1
	else:
		for i in range(n):
			s = input()
			for c in s:
				if c == '.':
					res += x
	print(res)


t = int(input())
for i in range(0, t):
	(n, m, x, y) = map(int, input().split())
	f = []
	su = 0
	for j in range(0, n):
		f.append(input())
	if 2 * x <= y:
		for l in f:
			su += x * l.count('.')
		print(su)
	else:
		for oo in range(0, n):
			s = f[oo]
			c = 0
			for k in range(0, m):
				if s[k] == '*':
					if c > 0:
						su = su + c // 2 * y + c % 2 * x
						c = 0
				else:
					c = c + 1
			if c > 0:
				su = su + c // 2 * y + c % 2 * x
		print(su)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lis = []
	for i in range(n):
		tmp = input()
		lis.append(tmp)
	cost = 0
	for i in lis:
		if '..' in i:
			tcount = i.count('..')
			if 2 * x <= y:
				cost += 2 * x * tcount
			else:
				cost += y * tcount
			i = i.replace('..', '')
		if '.' in i:
			tcount = i.count('.')
			cost += x * tcount
			i = i.replace('.', '')
	print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for j in range(n):
		l.append(list(input()))
	w1 = 0
	w2 = 0
	if 2 * x <= y:
		for q1 in l:
			for q2 in range(m):
				if q1[q2] == '.':
					w1 = w1 + 1
		print(w1 * x)
	else:
		for q1 in l:
			for q2 in range(m - 1):
				if q1[q2] == '.' and q1[q2 + 1] == '.':
					w2 = w2 + 1
					q1[q2 + 1] = '*'
				elif q1[q2] == '.':
					w1 = w1 + 1
			if q1[-1] == '.':
				w1 = w1 + 1
		print(w1 * x + w2 * y)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(a) for a in input().split()]
	p = []
	for i in range(n):
		p.append(input())
	cost = 0
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if p[i][j] == '.':
					cost += x
	else:
		for i in range(n):
			j = 0
			while j < m:
				if p[i][j] == '.':
					if j + 1 < m and p[i][j + 1] == '.':
						cost += y
						j += 1
					else:
						cost += x
				j += 1
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	data = []
	for _ in range(n):
		data.append(list(input()))
	if x * 2 <= y:
		result = 0
		for i in data:
			result += i.count('.')
		print(result * x)
	else:
		result = 0
		for i in range(n):
			for j in range(m - 1):
				if data[i][j] == '.' and data[i][j + 1] == '.':
					result += y
					data[i][j] = '*'
					data[i][j + 1] = '*'
		for i in data:
			result += x * i.count('.')
		print(result)


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007

def solve():
	t = nu()
	for tt in range(t):
		(n, m, x, y) = num()
		big = min(2 * x, y)
		ol = [0] * n
		op = 0
		for i in range(n):
			ol[i] = input()
			for j in range(m):
				if ol[i][j] == '.':
					op += 1
		if x * 2 <= y:
			print(op * x)
			continue
		cc = 0
		oi = 0
		for i in range(n):
			s = ol[i]
			j = 0
			while j < m:
				if s[j] == '.':
					if j + 1 < m and s[j] == '.' and (s[j + 1] == '.'):
						oi += 1
						j += 1
					elif s[j] == '.':
						cc += 1
				j += 1
		print(cc * x + oi * y)
solve()


from math import ceil

def solve():
	(n, m, x, y) = map(int, input().split(' '))
	s = 0
	save = 2 * x < y
	for _ in range(n):
		row = list(input())
		j = 0
		while j < m:
			if row[j] == '*':
				j += 1
				continue
			if j < m - 1 and row[j + 1] == '.' and (not save):
				s += y
				j += 2
			else:
				s += x
				j += 1
	print(s)
t = int(input())
while t:
	solve()
	t -= 1


from sys import stdin, stdout
read = stdin.readline
puts = stdout.write

def solve():
	(n, m, x, y) = [int(i) for i in read().split()]
	paint_two = True if 2 * x > y else False
	tiles = [read() for _ in range(n)]
	cost = 0
	for row in tiles:
		index = 0
		while index < m:
			if row[index] == '.' and index + 1 < m and (row[index + 1] == '.'):
				if paint_two:
					cost += y
				else:
					cost += 2 * x
				index += 1
			elif row[index] == '.':
				cost += x
			index += 1
	print(cost)
for i in range(int(read())):
	solve()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = y if x * 2 > y else x * 2
	ans = 0
	for _ in range(n):
		row = input()
		c = 0
		for tile in row:
			if tile == '.':
				c += 1
			else:
				ans = ans + c // 2 * y + c % 2 * x
				c = 0
		ans = ans + c // 2 * y + c % 2 * x
	print(ans)


import sys
max_int = 1000000001
min_int = -max_int
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, sys.stdin.readline().split())
	if y > 2 * x:
		y = 2 * x
	lens = []
	for _n in range(n):
		row = sys.stdin.readline()
		cntr = 0
		for letter in row:
			if letter == '.':
				cntr += 1
			elif cntr:
				lens.append(cntr)
				cntr = 0
		if cntr:
			lens.append(cntr)
			cntr = 0
	ans = 0
	for one in lens:
		ans += one // 2 * y + one % 2 * x
	print(ans)


t = int(input())
res = []
for i in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	sum1 = 0
	sum2 = 0
	for row in range(n):
		r = input().split('*')
		for j in r:
			length = len(j)
			sum2 += length // 2
			sum1 += length % 2
	if 2 * x < y:
		res.append(x * (sum1 + 2 * sum2))
	else:
		res.append(x * sum1 + y * sum2)
for i in res:
	print(i)


from collections import defaultdict, Counter
import math

def solve(n, m, x, y, a):
	flag = 0
	if x * 2 <= y:
		flag = 1
	s = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if flag == 0 and j < m - 1 and (a[i][j + 1] == '.'):
					s += y
					j += 1
				else:
					s += x
			j += 1
	return s
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(input()))
	res = solve(n, m, x, y, a)
	print(res)


import sys
input = sys.stdin.readline
for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = 0
	for k in range(n):
		a = input()
		d = 0
		for i in a:
			if i == '.':
				d += 1
			else:
				if 2 * x <= y:
					s += d * x
				else:
					s += y * (d // 2) + x * (d % 2)
				d = 0
		if d:
			if 2 * x <= y:
				s += d * x
			else:
				s += y * (d // 2) + x * (d % 2)
	print(s)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(0, n):
		a = input()
		a = list(a)
		l.append(a)
	if 2 * x <= y:
		ans = 0
		for i in range(0, n):
			for j in range(m):
				if l[i][j] == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		for i in range(0, n):
			for j in range(0, m):
				if l[i][j] == '.':
					if j + 1 < m:
						if l[i][j + 1] == '.':
							ans += y
							l[i][j + 1] = '*'
						else:
							ans += x
					else:
						ans += x
		print(ans)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = str(input())
		a = s.count('..')
		b = s.count('.')
		s = s.replace('..', '**')
		c = s.count('.')
		ans = ans + min(b * x, a * y + c * x)
	print(ans)


t = int(input())
for j in range(0, t):
	l1 = [int(x) for x in input().split()]
	n = l1[0]
	m = l1[1]
	x = l1[2]
	y = l1[3]
	l2 = [['a' for z in range(0, m)] for u in range(0, n)]
	white = 0
	for i in range(0, n):
		inp_str = input()
		for k in range(0, m):
			l2[i][k] = inp_str[k]
			if l2[i][k] == '.':
				white += 1
	two_white = 0
	for i in range(0, n):
		k = 0
		while k < m:
			if k < m - 1 and l2[i][k] == '.' and (l2[i][k + 1] == '.'):
				k += 2
				two_white += 1
			else:
				k += 1
	print(min(x * white, y * two_white + (white - two_white * 2) * x))


import math
t = int(input())
for x in range(t):
	[n, m, x, y] = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(input())
	if 2 * x <= y:
		count = 0
		for i in range(n):
			count += l[i].count('.')
		print(x * count)
	else:
		cost = 0
		for i in range(n):
			j = 0
			while j < m:
				if l[i][j] == '.':
					if j == m - 1:
						cost += x
						j += 1
					elif l[i][j + 1] == '.':
						cost += y
						j += 2
					else:
						cost += x
						j += 1
				else:
					j += 1
		print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	doublePrice = min(2 * x, y)
	totalPrice = 0
	for i in range(n):
		line = list(input())
		for j in range(len(line)):
			if line[j] == '.':
				if j + 1 < len(line) and line[j + 1] == '.':
					totalPrice += doublePrice
					line[j + 1] = '*'
				else:
					totalPrice += x
	print(totalPrice)


t = int(input().strip())
for _ in range(t):
	g = []
	(n, m, x, y) = map(int, input().strip().split())
	for _ in range(n):
		g.append(list(input().strip()))
	if x * 2 <= y:
		res = 0
		for s in g:
			for i in range(len(s)):
				if s[i] == '.':
					res += 1
		print(x * res)
	else:
		res1 = 0
		res2 = 0
		for i in range(len(g)):
			for j in range(len(g[i])):
				if g[i][j] == '.':
					if j + 1 < len(g[i]) and g[i][j + 1] == '.':
						res2 += 1
						g[i][j + 1] = '*'
					else:
						res1 += 1
		print(x * res1 + res2 * y)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split(' '))
	sum = 0
	for i in range(n):
		a = input()
		if 2 * x < y:
			l = [0, 0]
			for i in range(len(a)):
				if a[i] == '*':
					l[0] += 1
				else:
					l[1] += 1
			sum += l[1] * x
		else:
			(i, j) = (0, 0)
			while i < len(a) and j < len(a):
				if a[i] == '.':
					j = i
					while j < len(a):
						if a[j] == '.':
							j += 1
						else:
							break
					if (j - i) % 2 == 0:
						sum += (j - i) * y // 2
					else:
						sum += (j - i - 1) * y // 2 + x
					i = j
				else:
					i += 1
	print(sum)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	cost = 0
	for i in range(n):
		s = input()
		j = 0
		while j < m:
			if j == m - 1 and s[j] == '.':
				cost += x
			elif s[j] == '.' and s[j + 1] == '.':
				cost += min(2 * x, y)
				j += 1
			elif s[j] == '.' and s[j + 1] == '*':
				cost += x
			j += 1
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = tuple(map(int, input().split()))
	y = min(y, 2 * x)
	ans = 0
	for _ in range(n):
		line = [i for i in input()]
		i = 0
		while i < m:
			if line[i] == '.':
				if i != m - 1 and line[i + 1] == '.':
					ans += y
					i += 2
				else:
					ans += x
					i += 1
			else:
				i += 1
	print(ans)


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = int(stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, stdin.readline().split())
	soloworth = False
	if y >= x + x:
		soloworth = True
	type1 = 0
	type2 = 0
	for _ in range(n):
		s = input()
		temp = 0
		for i in s:
			if i == '.':
				temp += 1
			else:
				type1 += temp % 2
				type2 += temp // 2
				temp = 0
		type1 += temp % 2
		type2 += temp // 2
	if soloworth:
		print((type1 + 2 * type2) * x)
	else:
		print(type1 * x + type2 * y)


q = int(input())
for _ in range(q):
	(x, y, a, b) = map(int, input().split())
	li = [0] * x
	for i in range(x):
		li[i] = input()
	(p, r) = (0, 0)
	for i in range(x):
		p += li[i].count('..')
		r += li[i].count('.')
	b = min(b, 2 * a)
	print(p * b + (r - 2 * p) * a)


for _ in range(int(input())):
	(n, m, x, y) = [int(num) for num in input().split()]
	t_square = []
	for i in range(n):
		l1 = list(input())
		t_square.append(l1)
	a = 0
	b = 0
	c = 0
	for i in t_square:
		k = 0
		for j in range(len(i) - 1):
			if i[j] == '.':
				k += 1
			if i[j] == '*':
				a += k
				b += k // 2
				c += k % 2
				k = 0
		if i[-1] == '.':
			k += 1
			a += k
			b += k // 2
			c += k % 2
		else:
			a += k
			b += k // 2
			c += k % 2
	p1 = a * x
	p2 = b * y + c * x
	print(min(p1, p2))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = [input() for i in range(n)]
	ans = 0
	for i in range(n):
		j = 0
		while True:
			if j >= m:
				break
			if j + 1 < m:
				if a[i][j] == '.' and a[i][j + 1] == '.':
					ans += min(2 * x, y)
					j += 2
					continue
			if a[i][j] == '.':
				ans += x
				j += 1
				continue
			j += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	total = 0
	for j in range(n):
		s = input()
		con = 1
		w_tiles = s.count('.')
		ad_w_tiles = s.count('..')
		cost1 = (w_tiles - 2 * ad_w_tiles) * x + ad_w_tiles * y
		cost2 = w_tiles * x
		total += min(cost1, cost2)
	print(total)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = input()
		(cnt, j) = (0, 0)
		double = 0
		single = 0
		while j < m:
			if row[j] == '.':
				if j < m - 1:
					if row[j + 1] == '.':
						double += 1
						single += 1
						j += 1
				single += 1
			j += 1
		cnt = min(single * x, (single - double * 2) * x + double * y)
		ans += cnt
	print(ans)


for i in range(int(input())):
	b = []
	(n, m, x, y) = map(int, input().split())
	if x * 2 < y:
		y = x * 2
	for a in range(n):
		c = list(input())
		e = 0
		for d in range(m):
			if m == 1 and c[0] == '.':
				b.append(x)
				break
			if m == 1 and c[0] != '.':
				break
			if c[e] == '.' and c[e + 1] == '.':
				b.append(y)
				e += 2
				if e >= m - 1:
					if e == m - 1:
						if c[e] == '.':
							b.append(x)
					break
			elif c[e] == '.' and c[e + 1] != '.':
				b.append(x)
				e += 2
				if e >= m - 1:
					if e == m - 1:
						if c[e] == '.':
							b.append(x)
					break
			else:
				e += 1
				if e >= m - 1:
					if e == m - 1:
						if c[e] == '.':
							b.append(x)
					break
	print(sum(b))


def f(n, m, x, y, a):
	zb = '.'
	w = 0
	j = 0
	for i in range(n):
		R = []
		for t in range(m):
			if a[i][t] == zb:
				R.append(t)
		k = 0
		while k < len(R):
			if k + 1 < len(R) and R[k] + 1 == R[k + 1]:
				w += 1
				k += 2
			else:
				j += 1
				k += 1
	if 2 * x <= y:
		return x * (2 * w + j)
	else:
		return x * j + w * y
t = int(input())
while t > 0:
	(n, m, x, y) = map(int, input().split())
	a = []
	for l in range(n):
		L = input()
		a.append(L)
	print(f(n, m, x, y, a))
	t -= 1


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(str(input())))
	cost = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if j + 1 < m and mat[i][j] == '.' and (mat[i][j + 1] == '.'):
				cost += min(2 * x, y)
				j += 2
			elif mat[i][j] == '.':
				cost += x
				j += 1
			else:
				j += 1
		i += 1
	print(cost)


for _ in range(int(input())):
	(n, m, one, two) = map(int, input().split())
	grid = [input() for _ in range(n)]
	usey = two <= one * 2
	ans = 0
	for y in range(n):
		cnt = 0
		for x in range(m):
			if grid[y][x] == '.':
				cnt += 1
				if usey:
					if cnt == 2:
						ans += two
						cnt = 0
				else:
					ans += one
			elif usey:
				if cnt == 2:
					ans += two
				elif cnt == 1:
					ans += one
				cnt = 0
		if usey:
			if cnt == 2:
				ans += two
			elif cnt == 1:
				ans += one
	print(ans)


T = int(input())
for _ in range(T):
	(N, M, X, Y) = map(int, input().split())
	A = [input() for i in range(N)]
	Y = min(Y, X * 2)
	c = 0
	for row in A:
		s = row.split('*')
		for t in s:
			if len(t) == 0:
				continue
			(d, m) = divmod(len(t), 2)
			c += Y * d + X * m
	print(c)


tt = int(input())
ans = []
for t in range(tt):
	(n, m, x, y) = [int(i) for i in input().split()]
	if 2 * x > y:
		a1 = 0
		a2 = 0
		a = []
		for i in range(n):
			s = input()
			f = 0
			for j in range(m):
				if s[j] == '.':
					f += 1
				if (s[j] == '*' or j == m - 1) and f:
					a.append(f)
					f = 0
		for i in a:
			a2 += i // 2
			a1 += i % 2
		ans.append(x * a1 + y * a2)
	else:
		a = 0
		for i in range(n):
			s = input()
			f = 0
			for j in range(len(s)):
				if s[j] == '.':
					a += 1
		ans.append(a * x)
for aa in ans:
	print(aa)


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	(n, m, x, y) = read_ints()
	a = []
	cost = 0
	for i in range(n):
		row = input() + '*'
		white = 0
		for c in row:
			if c == '*':
				if white > 0:
					cost += min(white * x, white // 2 * y + white % 2 * x)
				white = 0
			else:
				white += 1
	print(cost)


def solve(n, m, x, y, grid):
	cost = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if grid[i][j] == '.':
				if j + 1 < m and grid[i][j + 1] == '.':
					cost += min(2 * x, y)
					j += 1
				else:
					cost += x
			j += 1
		i += 1
	return cost
t = int(input())
results = []
for _ in range(0, t):
	(n, m, x, y) = map(int, input().split(' '))
	grid = []
	for _n in range(0, n):
		row = input()
		grid.append(row)
	results.append(solve(n, m, x, y, grid))
for result in results:
	print(result)


t = int(input())
for _ in range(t):
	l = []
	(n, m, x, y) = map(int, input().split())
	for i in range(n):
		l.append(input())
	dp = [0] * (m + 1)
	dp[1] = x
	for i in range(2, m + 1):
		dp[i] = min(x + dp[i - 1], y + dp[i - 2])
	cost = 0
	for i in range(n):
		j = 0
		while j < m:
			k = 0
			while j < m and l[i][j] == '.':
				k += 1
				j += 1
			j += 1
			cost += dp[k]
	print(cost)


def get(n, x, y):
	S = 0
	for i in range(n):
		Str = input()
		if '..' in Str:
			A = 2 * x
			B = y
			if A > B:
				S += Str.count('..') * B
			else:
				S += Str.count('..') * A
		C = len(Str) - (2 * Str.count('..') + Str.count('*'))
		S += C * x
	return S
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	print(get(n, x, y))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(a, res1, res2) = ([], 0, 0)
	for _ in range(n):
		a.append(input().split('*'))
	for i in a:
		s = ' '.join(i)
		res1 += y * s.count('..')
		res1 += x * (s.count('.') - s.count('..') * 2)
		res2 += x * s.count('.')
	print(min(res1, res2))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	dbl = 2 * x
	if y < 2 * x:
		dbl = y
	a = list()
	for i in range(n):
		a.append(str(input()))
	price = 0
	visited = set()
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.' and (i, j) not in visited:
				visited.add((i, j))
				if (i, j - 1) not in visited and 0 <= j - 1 < m and (a[i][j - 1] == '.'):
					price += dbl
					visited.add((i, j - 1))
				elif (i, j + 1) not in visited and 0 <= j + 1 < m and (a[i][j + 1] == '.'):
					price += dbl
					visited.add((i, j + 1))
				else:
					price += x
				flag = True
	print(price)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	for _ in range(n):
		r = input()
		s = r.count('.')
		d = r.count('..')
		p += min(x * s, (s - 2 * d) * x + d * y)
	print(p)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for row in range(n):
		s = list(input())
		if 2 * x <= y:
			cost += s.count('.') * x
		else:
			j = 0
			while j < m - 1:
				if s[j] == '.' and s[j + 1] == '.':
					cost += y
					j += 2
				elif s[j] == '.':
					cost += x
					j += 1
				else:
					j += 1
			if j == m - 1:
				if s[j] == '.':
					cost += x
	print(cost)


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in inp().split()]
	tiles = []
	ans = 0
	for i in range(n):
		tiles.append(list(inp()))
	if y >= 2 * x:
		for i in range(n):
			for j in range(m):
				if tiles[i][j] == '.':
					ans += x
	else:
		for i in range(n):
			for j in range(m):
				if j != m - 1 and tiles[i][j] == '.' and (tiles[i][j + 1] == '.'):
					ans += y
					tiles[i][j + 1] = '*'
				elif tiles[i][j] == '.':
					ans += x
	print(ans)


import sys

def read():
	return list(map(int, sys.stdin.readline().split()))
for i in range(int(input())):
	(row, col, p1, p2) = read()
	arr = []
	for i in range(row):
		a = input()
		arr.append(a)
	ans = 0
	count = 0
	for i in range(row):
		if 2 * p1 > p2:
			if count & 1:
				ans += count // 2 * p2 + p1
			else:
				ans += count // 2 * p2
		else:
			ans += count * p1
		count = 0
		for j in range(col):
			if arr[i][j] == '.':
				count += 1
			else:
				if 2 * p1 > p2:
					if count & 1:
						ans += count // 2 * p2 + p1
					else:
						ans += count // 2 * p2
				else:
					ans += count * p1
				count = 0
	if 2 * p1 > p2:
		if count & 1:
			ans += count // 2 * p2 + p1
		else:
			ans += count // 2 * p2
	else:
		ans += count * p1
	print(ans)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s1 = 0
	s2 = 0
	for i in range(n):
		s = input()
		c = 0
		for i1 in range(len(s)):
			if s[i1] == '.':
				c = c + 1
			if s[i1] != '.' or i1 == len(s) - 1:
				s2 = s2 + c // 2
				s1 = s1 + c % 2
				c = 0
	if 2 * x < y:
		print(s2 * 2 * x + s1 * x)
	else:
		print(s2 * y + s1 * x)


import math
import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [str(input()) for i in range(n)]
	b = 2 * x
	ans = 0
	flag = False
	if b <= y:
		for i in a:
			for j in i:
				if j == '.':
					ans += x
	else:
		for i in a:
			for j in range(m):
				if i[j] == '.':
					if flag == True:
						ans = ans + y - x
						flag = False
					else:
						ans += x
						flag = True
				else:
					flag = False
			flag = False
	print(ans)


for _ in range(int(input())):
	(n, m, l, y) = map(int, input().split())
	c = []
	ans = 0
	mini = min(2 * l, y)
	for _ in range(n):
		c.append(input())
	for i in range(len(c)):
		x = c[i]
		j = 0
		while True:
			if x[j] != '.':
				j += 1
				if j >= len(x):
					break
			if x[j] == '.' and j == len(x) - 1:
				ans += l
				break
			if x[j] == '.' and x[j + 1] == '.':
				ans += mini
				j += 2
				if j >= len(x):
					break
				continue
			if x[j] == '.' and x[j + 1] != '.':
				ans += l
				j += 1
				if j >= len(x):
					break
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		sol = 0
		for _ in range(n):
			s = input()
			sol += s.count('.') * x
	else:
		sol = 0
		for _ in range(n):
			s = input() + '##'
			i = 0
			while i < m:
				if s[i] == '.':
					if s[i + 1] == '.':
						sol += y
						i += 1
					else:
						sol += x
				i += 1
	print(sol)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = [[''] * m] * n
	countSingle = 0
	countDouble = 0
	for _ in range(n):
		ip = input()
		countSingle += ip.count('.')
		countDouble += ip.count('..')
	ans1 = x * (countSingle - 2 * countDouble) + y * countDouble
	ans2 = x * countSingle
	print(min(ans1, ans2))


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		if 2 * x <= y:
			ans = 0
			for i in range(n):
				s = input()
				ans += s.count('.')
			print(ans * x)
		else:
			ans = 0
			for i in range(n):
				s = input()
				p = []
				for j in range(m):
					if s[j] == '.':
						p.append(j)
				p.append(-1)
				k = 0
				while k < len(p) and len(p) != 1:
					if p[k + 1] == p[k] + 1:
						ans += y
						k += 2
					else:
						k += 1
						ans += x
					if k == len(p) - 1:
						break
			print(ans)


def func(cur, x, y):
	return y * (cur // 2) + x * (cur % 2)
t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	y = min(y, 2 * x)
	ans = 0
	for i in range(n):
		s = list(input())
		cur = 0
		for j in range(len(s)):
			if s[j] == '*':
				ans += func(cur, x, y)
				cur = 0
			else:
				cur += 1
		ans += func(cur, x, y)
	print(ans)


from sys import stdin
import math
t = int(stdin.readline().strip())
for _ in range(t):
	(n, m, x, y) = (int(var) for var in stdin.readline().strip().split())
	theaterMatrix = []
	for _ in range(n):
		theaterMatrix.append([var for var in input()])
	isOneProfitable = 2 * x <= y
	cost = 0
	for tileRow in theaterMatrix:
		size = 0
		for i in range(len(tileRow)):
			if tileRow[i] == '.':
				size += 1
			else:
				if isOneProfitable:
					cost += x * size
				else:
					cost += x * (size % 2) + size // 2 * y
				size = 0
		if size != 0:
			if isOneProfitable:
				cost += x * size
			else:
				cost += x * (size % 2) + size // 2 * y
			size = 0
	print(cost)


t = int(input())
for i in range(t):
	(n, m, a, b) = [int(x) for x in input().split()]
	one = 0
	two = 0
	for j in range(n):
		li = list(input())
		li.append('*')
		k = 0
		while 1:
			if li[k] == '.' and li[k + 1] == '*':
				k += 2
				one += 1
			elif li[k] == '*':
				k += 1
			elif li[k] == '.' and li[k + 1] == '.':
				k += 2
				two += 1
			if k > m:
				break
	if a * 2 < b:
		print((one + 2 * two) * a)
	else:
		print(one * a + two * b)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.' and a[i][j + 1] == '.':
				ans += min(x << 1, y)
				j += 1
			elif a[i][j] == '.':
				ans += x
			j += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	matrix = []
	ytiles = 0
	xtiles = 0
	for j in range(n):
		row = [char for char in input()]
		previous = '*'
		for k in row:
			if k == '.' and previous == '.':
				ytiles += 1
				xtiles -= 1
				previous = '*'
			elif k == '.':
				xtiles += 1
				previous = k
			else:
				previous = k
	totalx = xtiles * x + 2 * ytiles * x
	totaly = ytiles * y + xtiles * x
	if totalx < totaly:
		print(totalx)
	else:
		print(totaly)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	r = 0
	s = 0
	y = min(y, x * 2)
	for i in range(n):
		l = input()
		r = 0
		for j in range(len(l)):
			if l[j] == '.' and r == 0:
				try:
					if l[j + 1] == '.':
						s += y
						r = 1
					else:
						s += x
				except:
					s += x
			else:
				r = 0
	print(s)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, x * 2)
	ans = 0
	for _ in range(n):
		for ln in map(len, input().split('*')):
			ans += ln % 2 * x + ln // 2 * y
	print(ans)


from math import ceil
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	ans1 = 0
	ans2 = 0
	for i in range(n):
		j = 0
		count = 0
		while j < m:
			if a[i][j] == '.':
				count += 1
			else:
				if count > 0:
					ans1 += count // 2 * y
					ans1 += count % 2 * x
					ans2 += count * x
				count = 0
			j += 1
		if count > 0:
			ans1 += count // 2 * y
			ans1 += count % 2 * x
			ans2 += count * x
	print(min(ans1, ans2))


T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	c = 0
	s = []
	for i in range(n):
		s1 = input()
		tmp = []
		for j in range(len(s1)):
			ch1 = s1[j]
			tmp.append(ch1)
		s.append(tmp)
	t = 2 * x
	if y >= t:
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.':
					c += 1
		ans = c * x
	else:
		c1 = 0
		for i in range(n):
			for j in range(m - 1):
				if s[i][j] == '.' and s[i][j + 1] == '.':
					c1 += 1
					ch = '*'
					s[i][j] = ch
					s[i][j + 1] = ch
		ans = c1 * y
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.':
					c += 1
		ans = ans + c * x
	print(ans)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())
for _ in range(T):
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	g = []
	o = 0
	e = 0
	for i in range(n):
		s = stdin.readline().strip('\n')
		cc = 0
		for j in range(m):
			if s[j] == '*':
				o += cc % 2
				e += cc // 2
				cc = 0
			else:
				cc += 1
		o += cc % 2
		e += cc // 2
	if y >= 2 * x:
		y = 2 * x
	print(e * y + o * x)


t = int(input())
while t:
	t = t - 1
	sing = 0
	dob = 0
	a = []
	b = []
	(n, m, x, y) = [int(x) for x in input().split()]
	for e in range(n):
		subl = []
		for ew in range(m):
			subl.append(0)
		b.append(subl)
	for i in range(0, n):
		a.append(input())
	for i in range(n):
		for j in range(len(a[i])):
			if a[i][j] == '.':
				sing += 1
	for i in range(n):
		for j in range(0, len(a[i]) - 1):
			if a[i][j] == '.' and a[i][j + 1] == '.' and (b[i][j] == 0):
				dob += 1
				b[i][j + 1] = 1
	if 2 * x <= y:
		price = sing * x
	else:
		sing = sing - 2 * dob
		price = dob * y + sing * x
	print(price)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [list(input()) for _ in range(n)]
	if 2 * x <= y:
		c = 0
		for i in a:
			for j in i:
				if j == '.':
					c += 1
		print(c * x)
	else:
		c = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					if j + 1 < m and a[i][j + 1] == '.':
						c += y
						a[i][j] = '*'
						a[i][j + 1] = '*'
					else:
						c += x
						a[i][j] = '*'
		print(c)


testCase = int(input())
for _ in range(testCase):
	(n, m, x, y) = map(int, input().split())
	miny = min(2 * x, y)
	ans = 0
	for i in range(n):
		sum = 0
		t = input()
		for j in range(len(t)):
			g = t[j]
			if g == '.':
				sum += 1
			elif g == '*':
				ans += int(sum / 2) * miny + sum % 2 * x
				sum = 0
			if g != '*' and j == m - 1:
				ans += int(sum / 2) * miny + sum % 2 * x
	print(int(ans))


t = int(input())
rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))

def solve(n, m, x, y, grid):
	cost = 0
	for i in range(n):
		dp = [0] * (m + 1)
		for j in range(m):
			if grid[i][j] == '.':
				dp[j + 1] = dp[j] + x
				if j >= 1 and grid[i][j - 1] == '.':
					dp[j + 1] = min(dp[j - 1] + y, dp[j + 1])
			else:
				dp[j + 1] = dp[j]
		cost += dp[-1]
	return cost
for _ in range(t):
	(n, m, x, y) = rrm()
	grid = []
	for _ in range(n):
		grid.append(list(rr()))
	print(solve(n, m, x, y, grid))


import math

def main(n, m, x, y, lst):
	countLst = [0, 0]
	for i in lst:
		j = 0
		while j < m:
			if j != m - 1 and (i[j] == '.' and i[j + 1] == '.'):
				countLst[1] += 1
				j += 1
			elif i[j] == '.':
				countLst[0] += 1
			j += 1
	if x < y / 2:
		return (2 * countLst[1] + countLst[0]) * x
	else:
		return countLst[1] * y + countLst[0] * x
for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	lst = []
	for i in range(n):
		lst.append(input())
	print(main(n, m, x, y, lst))


t = int(input())
for _ in range(t):
	(n, m, a, b) = map(int, input().split())
	price = 0
	for i in range(n):
		s = input()
		double = s.count('..')
		single = s.count('.')
		if 2 * a <= b:
			price += single * a
		else:
			price += double * b + (single - 2 * double) * a
	print(price)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	a = [list(input()) for i in range(n)]
	one = x
	two = min(2 * x, y)
	ans = 0
	for i in range(n):
		for g in range(m - 1):
			if a[i][g] == '.' and a[i][g + 1] == '.':
				ans += two
				a[i][g + 1] = '*'
			elif a[i][g] == '.' and a[i][g + 1] == '*':
				ans += one
		if a[i][m - 1] == '.':
			ans += one
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if y < 2 * x:
		for i in range(n):
			k = input()
			(ini, fi) = (0, 0)
			while ini < m:
				if k[ini] == '.':
					count = 0
					fi = ini
					while k[fi] == '.':
						count += 1
						fi += 1
						if fi == m:
							break
					ini = fi
					cost += count // 2 * y + count % 2 * x
				else:
					ini += 1
	else:
		for i in range(n):
			k = input()
			(ini, fi) = (0, 0)
			while ini < m:
				if k[ini] == '.':
					count = 0
					fi = ini
					while k[fi] == '.':
						count += 1
						fi += 1
						if fi == m:
							break
					ini = fi
					cost += count * x
				else:
					ini += 1
	print(cost)


import math
t = int(input())
while t > 0:
	inp = input()
	q = list(map(int, inp.split(' ')))
	n = q[0]
	m = q[1]
	x = q[2]
	y = q[3]
	cost = 0
	for i in range(0, n):
		s = input()
		j = 0
		while j < m:
			if s[j] == '.':
				if j + 1 < m and s[j + 1] == '.':
					if y < 2 * x:
						cost += y
					else:
						cost += 2 * x
					j += 1
				else:
					cost += x
			j += 1
	print(cost)
	t -= 1


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	y = min(y, 2 * x)
	for i in range(n):
		boolu = False
		s = input()
		for i in s:
			if i == '.' and boolu:
				cost -= x
				cost += y
				boolu = False
			elif i == '.':
				cost += x
				boolu = True
			elif i == '*':
				boolu = False
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = [int(x) for x in input().split()]
	l = []
	cost = 0
	for i in range(n):
		s = str(input())
		a = s.count('..')
		if a != 0:
			o = a * x * 2
			t = a * y
			if o > t:
				cost += t
			else:
				cost += o
		s = s.replace('..', '')
		b = s.count('.')
		cost += b * x
	print(cost)


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, m, x, y) = map(int, stdin.readline().split())
	cost = 0
	for i in range(n):
		string = stdin.readline()
		if x * 2 > y:
			while '..' in string:
				string = string.replace('..', '', 1)
				cost += y
		while '.' in string:
			string = string.replace('.', '', 1)
			cost += x
	print(cost)


def main():
	n = int(input())
	for i in range(0, n):
		solve()

def solve():
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(0, n):
		linha = input()
		l.append(linha)
	ans = 0
	if y > 2 * x:
		for i in range(0, n):
			for j in range(0, m):
				if l[i][j] == '.':
					ans += x
	else:
		for i in range(0, n):
			flag = False
			j = 0
			while j < m - 1:
				if l[i][j] == '.' and l[i][j + 1] == '.':
					ans += y
					j += 1
					if j == m - 1:
						flag = True
				elif l[i][j] == '.':
					ans += x
				j += 1
			if l[i][m - 1] == '.' and (not flag):
				ans += x
	print(ans)
main()


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	sm = 0
	for i in range(n):
		s = input()
		if y < 2 * x:
			p = s.replace('..', 'b')
			z = p.replace('.', 'a')
		else:
			z = s.replace('.', 'a')
		z = list(z)
		(v, u) = (z.count('a'), z.count('b'))
		sm += v * x + u * y
	print(sm)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	mat = ['o'] * n
	for i in range(n):
		mat[i] = input()
	ans = 0
	if m == 1:
		i = 0
		while i < n:
			if mat[i][0] == '.':
				ans = ans + x
			i += 1
	else:
		i = 0
		while i < n:
			j = 0
			while j < m:
				if mat[i][j] == '*':
					j = j + 1
				elif j == m - 1:
					ans = ans + x
					j = j + 1
				elif 2 * x < y and mat[i][j] == mat[i][j + 1]:
					ans = ans + 2 * x
					j = j + 2
				elif y <= 2 * x and mat[i][j] == mat[i][j + 1]:
					ans = ans + y
					j = j + 2
				else:
					ans = ans + x
					j += 1
			i += 1
	print(ans)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	li = []
	b = 0
	w = 0
	for i in range(n):
		st = input()
		w += st.count('.')
		li.append(st)
	if 2 * x <= y:
		print(x * w)
	else:
		p = 0
		for i in range(n):
			c = ''
			for j in range(m):
				if li[i][j] == '.':
					if li[i][j] == c:
						c = ''
						p += 1
						continue
				c = li[i][j]
		print(p * y + (w - 2 * p) * x)


for i in range(0, int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	p = []
	for j in range(0, n):
		s = input()
		p.append(s)
	cx = 0
	cy = 0
	for j in range(0, n):
		k = 0
		while k < m:
			if p[j][k] == '.':
				if k + 1 < m and p[j][k + 1] == '.':
					if 2 * x >= y:
						k = k + 2
						cy = cy + 1
					else:
						k = k + 2
						cx = cx + 2
				else:
					k = k + 1
					cx = cx + 1
			else:
				k = k + 1
	print(cy * y + cx * x)


from collections import deque
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	o = 0
	r = 0
	for j in range(n):
		s = input()
		o += s.count('.')
		r += s.count('..')
	print(min((o - 2 * r) * x + r * y, o * x))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	tot = 0
	for i in range(n):
		row = input().split('*')
		for z in row:
			if len(z) > 0:
				if len(z) == 1:
					tot += x
				elif y > 2 * x:
					tot += len(z) * x
				else:
					tot += len(z) // 2 * y + x * (len(z) & 1)
	print(tot)


from collections import Counter

def check(arr, a, b, m, n):
	result = 0
	brr = []
	for i in range(m):
		j = 0
		while j < n:
			if j + 1 == n and arr[i][j] == '.':
				brr.append(1)
				j += 1
			elif j + 1 == n and arr[i][j] == '*':
				j += 1
			elif arr[i][j] == arr[i][j + 1] and arr[i][j] == '.':
				brr.append(2)
				j += 2
			elif arr[i][j] == '.':
				brr.append(1)
				j += 1
			else:
				j += 1
	d = {}
	d[1] = a
	d[2] = min(2 * a, b)
	for i in brr:
		result += d[i]
	return result
t = int(input())
while t:
	(m, n, a, b) = list(map(int, input().split()))
	brr = []
	arr = []
	for i in range(m):
		arr.append(list(input()))
	print(check(arr, a, b, m, n))
	t -= 1


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	l = []
	for i in range(a):
		l.append(list(input()))
	x = 0
	for i in range(a):
		for j in range(b):
			if j != b - 1 and l[i][j] == '.' and (l[i][j + 1] == '.') and (2 * c > d):
				l[i][j + 1] = '*'
				x = x + d
				l[i][j] = '*'
			elif j != b - 1 and l[i][j] == '.' and (l[i][j + 1] == '.') and (2 * c < d):
				l[i][j] = '*'
				l[i][j + 1] = '*'
				x = x + 2 * c
			elif l[i][j] == '.':
				x = x + c
	print(x)


T = int(input())
while T > 0:
	T -= 1
	(cost, count) = (0, 0)
	(n, m, x, y) = map(int, input().strip().split())
	arr = []
	for i in range(n):
		arr.append([])
		arr[i] = [x for x in input()]
	(i, j) = (0, 0)
	while i < n:
		j = 0
		while j < m:
			if arr[i][j] == '.':
				count += 1
			if arr[i][j] == '*':
				j += 1
				continue
			if j < m - 1:
				if arr[i][j + 1] == '.':
					count += 1
					cost += y
					j += 2
					continue
			cost += x
			j += 1
		i += 1
	print(min(cost, count * x))


def main():
	t = int(input())
	for _ in range(t):
		floor = []
		(n, m, x, y) = map(int, input().split(' '))
		for i in range(n):
			floor.append(input())
		ct = 0
		if 2 * x <= y:
			for f in floor:
				ct += f.count('.')
			print(ct * x)
		else:
			for f in floor:
				st = []
				for j in range(len(f)):
					if f[j] == '.':
						if st:
							st.pop()
							ct += y
						else:
							st.append('.')
					elif st:
						ct += x
						st.pop()
				if len(st) == 2:
					ct += y
				elif len(st) == 1:
					ct += x
			print(ct)
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	large = False
	d = dict()
	d[1] = x
	d[2] = y
	if d[1] * 2 > d[2]:
		large = True
	cost = 0
	for row in range(n):
		s = input()
		m1 = m
		while m1:
			temp = s[0]
			while temp == '*':
				m1 -= 1
				s = s[1:]
				if m1 > 0:
					temp = s[0]
				else:
					break
			if m1 >= 2 and s[:2] == '..':
				if large:
					cost += d[2]
				else:
					cost += d[1] * 2
				m1 -= 2
				s = s[2:]
			else:
				if m1 > 0 and s[0] == '.':
					cost += d[1]
				if m1 > 0:
					m1 -= 1
				s = s[1:]
	print(cost)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def solve():
	t = get_int_input()
	for _ in range(t):
		(n, m, c1, c2) = get_int_input_arr()
		grid = []
		for _ in range(n):
			grid.append(get_input())
		res = 0
		for i in range(n):
			paths = grid[i].split('*')
			for path in paths:
				if len(path) == 1:
					res += c1
				if len(path) > 1:
					if c1 * 2 < c2:
						res += c1 * len(path)
					else:
						p2 = len(path) // 2
						p_mod = len(path) % 2
						res += c1 * p_mod + c2 * p2
		cout << res << endl

def main():
	solve()
main()


t = int(input())
for i in range(t):
	(n, m, one, two) = [int(x) for x in input().split()]
	a = []
	for i in range(n):
		l = input()
		mm = list(l)
		a.append(mm)
	to = min(two, one + one)
	c = 0
	i = 0
	j = 0
	while i < n:
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j + 1 < m and a[i][j + 1] == '.':
					j += 1
					c += to
				else:
					c += one
			j += 1
		i += 1
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(x * 2, y)
	l = [list(input().split('*')) for _ in range(n)]
	c = 0
	for i in l:
		for j in i:
			s = len(j)
			c += s // 2 * y + s % 2 * x
	print(c)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	(n, m, x, y) = map(int, fastinput().split())
	y = min(y, 2 * x)
	z = 0
	for _ in range(n):
		s = list(input())
		count = 0
		for i in range(m - 1):
			if s[i] == '.' and s[i + 1] == '.':
				s[i] = '*'
				s[i + 1] = '*'
				count += y
			elif s[i] == '.':
				count += x
				s[i] = '*'
		if s[m - 1] == '.':
			count += x
		z += count
	print(z)


from sys import stdin
input = lambda : stdin.readline()
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	if y > 2 * x:
		y = 2 * x
	for i in range(n):
		a = list(input())
		j = 0
		while j < len(a):
			if a[j] == '.':
				if j < len(a) - 1 and a[j + 1] == '.':
					p += y
					j += 1
				else:
					p += x
			j += 1
	print(p)


t = int(input())
while t:
	t = t - 1
	(n, m, x, y) = map(int, input().split())
	pc = x * 2
	rate = 0
	for i in range(n):
		a = input()
		if pc > y:
			twos = a.count('..')
			ones = a.count('.') - twos * 2
			rate = rate + twos * y + ones * x
		else:
			rate = rate + a.count('.') * x
	print(rate)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for i in range(n):
		row = list(input())
		j = 0
		while j < len(row):
			if row[j] == '.':
				if j != len(row) - 1:
					if row[j + 1] == '.':
						cost += min(2 * x, y)
						j += 2
						continue
				cost += x
			j += 1
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	con2 = min(2 * x, y)
	con1 = x
	cost = 0
	for i in range(n):
		s = input()
		i = 0
		while i < m:
			if s[i] == '.':
				if i + 1 < m and s[i + 1] == '.':
					cost += con2
					i += 1
				else:
					cost += con1
			i += 1
	print(cost)


def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		y = min(y, 2 * x)
		total = 0
		for __ in range(n):
			row = input()
			row_total = 0
			z = 0
			while z < m:
				tile = row[z]
				if tile == '.':
					if z < m - 1 and row[z + 1] == '.':
						row_total += y
						z += 2
					else:
						row_total += x
						z += 1
				else:
					z += 1
			total += row_total
		print(total)
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = 0
	for i in range(n):
		f = input()
		if x * 2 <= y:
			for el in f:
				if el == '.':
					s += x
		else:
			fl = 0
			for el in f:
				if el == '.':
					if fl == 1:
						fl = 0
						s += y
					else:
						fl = 1
				elif fl == 1:
					fl = 0
					s += x
			if fl == 1:
				s += x
	print(s)


from sys import stdout, stdin
(_input, _print) = (stdin.readline, stdout.write)
(_int, _range, _str) = (int, range, str)

def solution():
	for _ in _range(_int(_input())):
		(n, m, x, y) = [_int(i) for i in _input().split()]
		ans = 0
		if 2 * x <= y:
			for i in _range(n):
				line = _input().rstrip('\n')
				ans += line.count('.')
			print(ans * x)
			continue
		else:
			arr = []
			for i in _range(n):
				arr.append(_input().rstrip('\n'))
			for i in _range(n):
				counter = 0
				for j in _range(m):
					if arr[i][j] == '.':
						counter += 1
					elif counter != 0:
						ans += counter // 2 * y + counter % 2 * x
						counter = 0
				else:
					if counter != 0:
						ans += counter // 2 * y + counter % 2 * x
			print(ans)
solution()


import math
test_cases = int(input())

def testCase():
	(n, m, x, y) = [int(inp) for inp in input().strip().split()]

	def inputSquare():
		square = []
		for i in range(n):
			row = list([str(inp) for inp in input().strip().split()][0])
			square.append(row)
		return square
	square = inputSquare()

	def getPricingForConseqDots(amount_of_conseq_dots):
		if 2 * x <= y:
			return amount_of_conseq_dots * x
		else:
			return math.floor(amount_of_conseq_dots / 2) * y + amount_of_conseq_dots % 2 * x
	total_cost = 0
	for row in square:
		dot_count = 0
		for val in row:
			if val == '.':
				dot_count += 1
			else:
				total_cost += getPricingForConseqDots(dot_count)
				dot_count = 0
		total_cost += getPricingForConseqDots(dot_count)
	print(total_cost)
[testCase() for t in range(test_cases)]


import sys
readline = sys.stdin.buffer.readline

def even(n):
	return 1 if n % 2 == 0 else 0
t = int(readline())
for _ in range(t):
	(h, w, x, y) = map(int, readline().split())
	single = 0
	double = 0
	flag = 0
	for i in range(h):
		s = readline().rstrip().decode('utf-8')
		j = 0
		while j < w:
			if j + 1 < w and s[j] == '.' and (s[j + 1] == '.'):
				double += 1
				j += 1
			elif s[j] == '.':
				single += 1
			j += 1
	print(min(x * (single + 2 * double), single * x + double * y))


testes = int(input())
for i in range(testes):
	price = 0
	entrada = input()
	(linhas, colunas, p1, p2) = entrada.split(' ')
	azulejo = ['' for l in range(int(linhas))]
	for l in range(int(linhas)):
		entLinha = input()
		azulejo[l] = [c for c in entLinha]
	countWhite = 0
	for l in azulejo:
		count = 0
		for j in range(len(l)):
			if l[j] == '.':
				count += 1
				countWhite += 1
			else:
				if count == 1:
					price += int(p1)
					l[j - 1] = '#'
				count = 0
			if count == 2:
				l[j] = '#'
				l[j - 1] = '#'
				price += int(p2)
				count = 0
		if count == 1:
			price += int(p1)
			l[j] = '#'
	if price > countWhite * int(p1):
		print(countWhite * int(p1))
	else:
		print(price)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	grid = []
	if x * 2 >= y:
		for i in range(n):
			row = input()
			grid.append(list(row))
		ans = 0
		for i in range(n):
			for j in range(m - 1):
				if grid[i][j] == grid[i][j + 1] == '.':
					ans += y
					grid[i][j] = '*'
					grid[i][j + 1] = '*'
		for i in range(n):
			for j in range(m):
				if grid[i][j] == '.':
					ans += x
					grid[i][j] = '*'
		print(ans)
	else:
		ans = 0
		for i in range(n):
			row = input()
			ans += row.count('.')
		print(ans * x)


def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		tiles = []
		ans = 0
		for _ in range(n):
			tiles.append(input())
		if 2 * x <= y:
			ans = x * sum([a.count('.') for a in tiles])
		else:
			for a in tiles:
				double_tile = a.count('..')
				p = a.count('.')
				single_tile = p - 2 * double_tile
				ans += x * single_tile + y * double_tile
		print(ans)
main()


q = int(input())
for z in range(q):
	(n, m, x, y) = map(int, input().split(' '))
	if x * 2 < y:
		y = x * 2
	res = 0
	for i in range(n):
		s = input()
		lw = False
		for j in range(m):
			if s[j] == '*':
				if lw:
					res += x
					lw = False
			elif lw:
				res += y
				lw = False
			else:
				lw = True
		if lw:
			res += x
	print(res)


for request in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	d = 0
	for line in range(n):
		li = input()
		if x * 2 > y:
			p = False
			for s in li:
				if p and s == '.':
					d += y
					p = False
				elif p:
					d += x
					p = False
				elif s == '.':
					p = True
			if p:
				d += x
		else:
			d += li.count('.') * x
	print(d)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c1 = 0
	c2 = 0
	for j in range(n):
		s = input()
		t1 = s.count('.')
		t2 = s.count('..')
		c1 += t2 * y + (t1 - 2 * t2) * x
		c2 += t1 * x
	print(min(c1, c2))


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))
tcmax = num()

def mainloop(tcid=1):
	(n, m, x, y) = nums()
	a = lines(n)
	wht = '.'
	if 2 * x < y:
		y = 2 * x
	ans = 0
	for aa in a:
		ii = 0
		while ii < m:
			bb = aa[ii]
			if bb == wht:
				if ii + 1 < m and aa[ii + 1] == wht:
					ans += y
					ii += 1
				else:
					ans += x
			ii += 1
	print(ans)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


dy = [0]
dx = [1]

def bfs(y, x):
	if arrived[y][x]:
		return
	arrived[y][x] = True
	global ans
	ans += a
	for k in range(1):
		ny = y + dy[k]
		nx = x + dx[k]
		if not (0 <= ny < n and 0 <= nx < m):
			continue
		if arrived[ny][nx] or block[ny][nx] == '*':
			continue
		if block[ny][nx] == '.' and block[y][x] == '.' and (k == 0):
			ans -= a
			ans += min(a * 2, b)
			arrived[ny][nx] = True
for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	block = [input() for _ in range(n)]
	arrived = [[False] * m for _ in range(n)]
	ans = 0
	(sy, sx) = (0, 0)
	po = False
	for y in range(n):
		for x in range(m):
			if block[y][x] == '.' and (not arrived[y][x]):
				(sy, sx) = (y, x)
				bfs(sy, sx)
	print(ans)


for I in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	result = 0
	for i in range(n):
		s = input()
		if 2 * x <= y:
			for i in range(m):
				if s[i] == '.':
					result += x
		else:
			flag = ''
			i = 0
			while i < m - 1:
				if s[i] == '.' and s[i + 1] == '.':
					result += y
					if i + 1 == m - 1:
						flag = 'done'
					i += 1
				elif s[i] == '.':
					result += x
					if i == m - 1:
						flag = 'done'
				i += 1
			if len(s) > 1:
				if flag == '':
					if s[-1] == '.':
						result += x
			elif s[0] == '.':
				result += x
	print(result)


t = int(input())
for i in range(t):
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	lst_2d = [[1] * m for i in range(n)]
	for i in range(len(lst_2d)):
		w = list(input())
		for p in range(len(lst_2d[i])):
			if w[p] == '*':
				lst_2d[i][p] = 1
			if w[p] == '.':
				lst_2d[i][p] = 0
	count = 0
	count1 = 0
	count2 = 0
	for i in range(len(lst_2d)):
		j = 0
		while j < m:
			if j + 1 >= m and lst_2d[i][j] == 0:
				count1 += 1
				j += 1
			elif lst_2d[i][j] == 0 and lst_2d[i][j + 1] == 0:
				count2 += 1
				j += 2
			elif lst_2d[i][j] == 0:
				count1 += 1
				j += 1
			else:
				j += 1
	if 2 * x <= y:
		count = x * (count1 + count2 * 2)
	else:
		count = x * count1 + y * count2
	print(count)


from sys import stdin
from sys import stdout
sprint = stdout.write
sinput = stdin.readline

def func():
	(n, m, x, y) = [int(x) for x in sinput().split()]
	arr = []
	dic = {}
	for i in range(n):
		arr.append(input().replace('\n', ''))
	si = 0
	do = 0
	for i in range(n):
		for j in range(m):
			if dic.get((i, j)) is None and arr[i][j] == '.':
				if j + 1 <= m - 1:
					if arr[i][j + 1] == '.':
						do += 1
						dic[i, j + 1] = True
					else:
						si += 1
				else:
					si += 1
	print(min(x * si + y * do, (si + 2 * do) * x))
t = int(sinput())
while t != 0:
	t -= 1
	func()


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	price = 0
	for i in range(n):
		temp = 0
		for j in range(m):
			if mat[i][j] == '.':
				temp += 1
			else:
				if 2 * x < y:
					price += x * temp
				elif temp % 2 == 0:
					price += temp // 2 * y
				else:
					price += temp // 2 * y + x
				temp = 0
		if 2 * x < y:
			price += x * temp
		elif temp % 2 == 0:
			price += temp // 2 * y
		else:
			price += temp // 2 * y + x
		temp = 0
	print(price)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split(' '))
	ans = 0
	for i in range(n):
		stri = input().split('*')
		for j in stri:
			if x * 2 <= y:
				ans += x * len(j)
			else:
				ans += y * (len(j) // 2)
				ans += x * (len(j) % 2)
	print(ans)


import sys
pprint = lambda s: print(' '.join(map(str, s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(2 * x, y)
	ans = 0
	for i in range(n):
		s = list(input())
		f = False
		for i in range(m):
			if f:
				f = False
				continue
			if i + 1 == m:
				if s[i] == '.':
					ans += x
			elif s[i] == '.':
				if s[i + 1] == '.':
					ans += y
					f = True
				else:
					ans += x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for _ in range(n):
		s = list(map(str, input().split()))
		arr.append(s)
	if y <= x:
		two = 0
		one = 0
		for i in arr:
			row = i[0]
			j = 0
			while j < len(row) - 1:
				if row[j] == '.' and row[j + 1] == '.':
					two += 1
					j += 2
				elif row[j] == '.':
					one += 1
					j += 1
				else:
					j += 1
			if j == len(row) - 1:
				if row[j] == '.':
					one += 1
		print(one * x + two * y)
	else:
		check = y - x
		if check <= x:
			two = 0
			one = 0
			for i in arr:
				row = i[0]
				j = 0
				while j < len(row) - 1:
					if row[j] == '.' and row[j + 1] == '.':
						two += 1
						j += 2
					elif row[j] == '.':
						one += 1
						j += 1
					else:
						j += 1
				if j == len(row) - 1:
					if row[j] == '.':
						one += 1
			print(one * x + two * y)
		else:
			one = 0
			for i in arr:
				for j in i[0]:
					if j == '.':
						one += 1
			print(one * x)


t = int(input())
for _ in range(t):
	no_of_x = 0
	no_of_y = 0
	(n, m, x, y) = tuple(map(int, input().split()))
	for j in range(n):
		row_of = list(input())
		element = 0
		while element < m:
			if row_of[element] == '.' and element <= len(row_of) - 2:
				if row_of[element + 1] == '.':
					no_of_y += 1
				else:
					no_of_x += 1
				element += 2
			elif row_of[element] == '.' and element == len(row_of) - 1:
				no_of_x += 1
				element += 1
			else:
				element += 1
	if 2 * x < y:
		no_of_x += 2 * no_of_y
		print(no_of_x * x)
	else:
		print(no_of_x * x + no_of_y * y)


for test_i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if y >= x * 2:
		for r in range(n):
			for ch in input():
				if ch == '.':
					ans += x
	else:
		for r in range(n):
			prev = '*'
			for ch in input() + '*':
				if prev == '.':
					if ch == '.':
						ans += y
						prev = '*'
					else:
						ans += x
						prev = '*'
				else:
					prev = ch
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, p_1, p_2) = map(int, input().split())
	price = 0
	if p_2 < p_1 * 2:
		for i in range(n):
			row = input()
			prev_bloc = 0
			b_loc = row.find('*')
			while b_loc != -1:
				price += p_2 * ((b_loc - prev_bloc) // 2) + p_1 * ((b_loc - prev_bloc) % 2)
				prev_bloc = b_loc + 1
				b_loc = row.find('*', b_loc + 1)
			if prev_bloc < m:
				price += p_2 * ((m - prev_bloc) // 2) + p_1 * ((m - prev_bloc) % 2)
		print(price)
	else:
		for i in range(n):
			price += input().count('.') * p_1
		print(price)


for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	(y, result) = (min(y, 2 * x), 0)
	for _ in range(n):
		for j in map(len, input().split('*')):
			result += j % 2 * x + j // 2 * y
	print(result)


for _ in range(int(input())):
	(N, M, One, Two) = map(int, input().split())
	Matrix = []
	for i in range(N):
		Temp = input()
		Matrix.append(Temp)
	Ans = 0
	for i in range(N):
		Space = []
		count = 0
		Cost = 0
		for j in range(M):
			if Matrix[i][j] == '.':
				count += 1
			else:
				Space.append(count)
				count = 0
		if Matrix[i][-1] == '.':
			Space.append(count)
		Fix = 0
		for k in Space:
			Fix += k % 2
			Cost += (k >> 1) * Two
		Ans += Fix * One + min(Cost, (sum(Space) - Fix) * One)
	print(Ans)


import math
for i in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	rows = []
	for _ in range(n):
		rows.append(list(input()))
	price = 0
	if x <= y / 2:
		for row in rows:
			for c in row:
				if c == '.':
					price += x
	else:
		for i in range(n):
			for j in range(m - 1):
				if rows[i][j] == '.' and rows[i][j + 1] == '.':
					price += y
					rows[i][j] = '*'
					rows[i][j + 1] = '*'
		for row in rows:
			for c in row:
				if c == '.':
					price += x
	print(price)


t = int(input())
for _ in range(t):
	(n, m, x, ty) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	y = ty / 2
	cost = 0
	if x < y:
		for i in a:
			for j in i:
				if j == '.':
					cost += x
		print(cost)
	else:
		for i in a:
			d = 1
			flag = 0
			j = 0
			while j < m:
				if j < m - 1 and i[j] == '.' and (i[j + 1] == '.'):
					j += 2
					cost += ty
				elif j < m - 1 and i[j] == '.':
					cost += x
					j += 1
				else:
					if i[j] == '.':
						cost += x
					j += 1
		print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	(v, s) = ([], 0)
	if x * 2 > y:
		for i in range(n):
			(k, v) = (1, input())
			for j in range(1, m):
				if v[j] == v[j - 1]:
					k = k + 1
				else:
					if v[j - 1] == '.':
						s = s + k // 2 * y + k % 2 * x
					k = 1
			if v[m - 1] == '.':
				s = s + k // 2 * y + k % 2 * x
		print(s)
	else:
		for i in range(n):
			v = input()
			for j in range(m):
				if v[j] == '.':
					s = s + x
		print(s)


t = int(input())
for _ in range(t):
	(n, m, t1, t2) = map(int, input().split())
	M = [input() for _ in range(n)]
	from itertools import groupby
	ans = 0
	t2 = min(t2, t1 * 2)
	for a in M:
		l = [[k, len(list(g))] for (k, g) in groupby(a)]
		for (k, i) in l:
			if k == '*':
				continue
			ans += i // 2 * t2
			ans += i % 2 * t1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		y = 2 * x
	a = [] * n
	ans = 0
	for i in range(n):
		xo = input()
		xo += '*'
		j = 0
		while j < m:
			if xo[j] == '.':
				if xo[j + 1] == '*':
					ans += x
				else:
					ans += y
				j += 2
			else:
				j += 1
	print(ans)


from math import *
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	q = []
	for i in range(n):
		q.append(input())
	f = 0
	if y / 2 > x:
		f = 1
	if f == 1:
		c = 0
		for i in range(n):
			for j in range(m):
				if q[i][j] == '.':
					c = c + 1
		print(c * x)
	else:
		c = 0
		d = 0
		for i in range(n):
			j = 0
			while j < m:
				if j == m - 1:
					if q[i][j] == '.':
						c = c + 1
					j = j + 1
				elif q[i][j] == '.':
					if q[i][j + 1] == '.':
						d = d + 1
						j = j + 2
					else:
						c = c + 1
						j = j + 1
				else:
					j = j + 1
		print(c * x + d * y)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	if x * 2 <= y:
		flag = True
	else:
		flag = False
	cnt = 0
	for i in range(n):
		s = list(input()[:-1])
		if flag:
			cnt += s.count('.') * x
		else:
			ccnt = 1
			q = []
			for j in range(1, len(s)):
				if s[j - 1] == s[j]:
					ccnt += 1
				elif s[j - 1] != s[j]:
					q.append(ccnt)
					ccnt = 1
			q.append(ccnt)
			if s[0] == '.':
				for k in range(0, len(q), 2):
					cnt += q[k] // 2 * y + q[k] % 2 * x
			else:
				for k in range(1, len(q), 2):
					cnt += q[k] // 2 * y + q[k] % 2 * x
	print(cnt)


import sys
import math as mt
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, m, x, y) = I()
	a = 0
	b = 0
	t = 0
	for i in range(n):
		t = list(input())
		for j in range(m - 1):
			if t[j] == '.' and t[j + 1] == '.':
				b += 1
				t[j] = '*'
				t[j + 1] = '*'
			elif t[j] == '.' and t[j + 1] != '.':
				a += 1
		if t[-1] == '.':
			a += 1
	if 2 * x < y:
		print(a * x + b * (2 * x))
	else:
		print(a * x + b * y)


_ = int(input())
for __ in range(_):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		gg = False
		s = input()
		j = 0
		while j < m - 1:
			if s[j] == '.' and s[j + 1] == '.':
				ans += min(2 * x, y)
				if j == m - 2:
					gg = True
				j += 1
			elif s[j] == '.':
				ans += x
			j += 1
		if not gg:
			if s[m - 1] == '.':
				ans += x
	print(ans)


for i in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	twos = 0
	ones = 0
	for i in range(n):
		s = input()
		range_iter = iter(range(m))
		for i in range_iter:
			if s[i] == '.':
				if i == m - 1:
					ones += 1
				elif s[i + 1] == '.':
					twos += 1
					next(range_iter)
				else:
					ones += 1
	print(min([y * twos + x * ones, x * 2 * twos + x * ones]))


from collections import defaultdict as dd

def lcm(a, b):
	return a * b // gcd(a, b)

def inin():
	return int(input())

def inar():
	return list(map(int, input().split()))

def ar(element, size):
	return [element for i in range(size)]

def digitsum(num):
	su = 0
	while num:
		su += num % 10
		num //= 10
	return su

def yes():
	print('YES')

def no():
	print('NO')

def calcseg(l, x, y):
	an1 = l * x
	an2 = l // 2 * y + l % 2 * x
	return min(an1, an2)
Testcase = inin()
for _ in range(Testcase):
	(n, m, x, y) = inar()
	ans = 0
	for i in range(n):
		ss = input()
		j = 0
		seg = 0
		while j < m:
			if ss[j] == '.':
				seg += 1
			else:
				ans += calcseg(seg, x, y)
				seg = 0
			j += 1
		ans += calcseg(seg, x, y)
	print(ans)


T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	if 2 * x <= y:
		count = 0
		for i in range(n):
			for j in arr[i]:
				if j == '.':
					count += 1
		print(count * x)
	else:
		ans = 0
		for i in range(n):
			for j in range(m - 1, -1, -1):
				if arr[i][j] == '.':
					if j - 1 >= 0 and arr[i][j - 1] == '.':
						ans += y
						arr[i][j - 1] = 'S'
					else:
						ans += x
		print(ans)


t = int(input())
for i in range(t):
	(rows, columns, px, py) = map(int, input().strip().split())
	cost = 0
	for i in range(rows):
		a = list(input())
		if len(a) == 1:
			if a[0] == '.':
				cost += px
		else:
			for i in range(len(a) - 1):
				if a[i] == '.' and a[i + 1] == '.':
					if py <= 2 * px:
						cost += py
						a[i] = '*'
						a[i + 1] = '*'
					else:
						cost += 2 * px
						a[i] = '*'
						a[i + 1] = '*'
				elif a[i] == '.':
					cost += px
			if a[len(a) - 1] == '.':
				cost += px
	print(cost)


t = int(input())
for T in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	l = []
	ans = 0
	c1 = 0
	tt = 0
	for i in range(n):
		l.append(list(input()))
		tt += l[i].count('.')
	c1 = tt * x
	l1 = []
	for i in range(n):
		val = 0
		for j in range(m):
			if l[i][j] == '.':
				val += 1
			if l[i][j] == '*' or j == m - 1:
				l1.append(val)
				val = 0
	c2 = 0
	for i in l1:
		c2 += min(i // 2 * y, i * x) + i % 2 * x
	if c2 == 0:
		print(c1)
		continue
	print(min(c1, c2))


def solution(sq, p1, p2):
	odd = []
	even = []
	for row in sq:
		for column in row:
			if len(column) % 2 == 0:
				even.append(len(column))
			else:
				odd.append(len(column))
	if p1 * 2 <= p2:
		res = (sum(odd) + sum(even)) * p1
	else:
		even += [i // 2 * 2 for i in odd]
		odd = [i % 2 for i in odd]
		res = sum(odd) * p1 + sum(even) // 2 * p2
	return res

def main():
	for test in range(int(input())):
		(leng, wid, tile1, tile2) = map(int, input().split())
		square = [input().split('*') for _ in range(leng)]
		result = solution(square, tile1, tile2)
		print(result)
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lst = []
	for v in range(n):
		temp = input()
		lst.append(list(temp))
	ans = 0
	for i in range(n):
		temp = lst[i]
		j = 0
		while True:
			if j >= m:
				break
			if temp[j] == '.':
				if j + 1 < m:
					if temp[j + 1] == '.':
						ans += min(2 * x, y)
					else:
						ans += x
					j += 2
				else:
					ans += x
					j += 1
			else:
				j += 1
	print(ans)


t = int(input())
for t in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if 2 * x < y:
		for i in range(n):
			list1 = list(input())
			for c in list1:
				if c == '.':
					cost += x
		print(cost)
	else:
		for i in range(n):
			list1 = list(input())
			j = 0
			while j < m - 1:
				if list1[j] == '.':
					if list1[j + 1] == '.':
						cost += y
						j += 1
					else:
						cost += x
				j += 1
			if j == m - 1 and list1[j] == '.':
				cost += x
		print(cost)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = [len(x) for x in input().split('*')]
		ans += sum([min(l // 2 * y + l % 2 * x, l * x) for l in row])
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	tSp = 0
	if 2 * x < y:
		y = 2 * x
	for i in range(n):
		m = input()
		j = 0
		st = -1
		ed = -1
		while j < len(m):
			if m[j] == '.':
				if st == -1:
					st = j
				ed = -1
			else:
				ed = j
				if st > -1:
					reqSp = ed - st
					if reqSp == 1:
						tSp = tSp + x
					elif reqSp % 2 == 0:
						double = reqSp // 2
						tSp += double * y
					else:
						double = reqSp // 2
						tSp += double * y
						tSp += x
				st = -1
			j = j + 1
		if ed == -1:
			reqSp = j - st
			if reqSp == 1:
				tSp = tSp + x
			elif reqSp % 2 == 0:
				double = reqSp // 2
				tSp += double * y
			else:
				double = reqSp // 2
				tSp += double * y
				tSp += x
	print(tSp)


import sys
input = sys.stdin.readline
import math

def inp():
	return int(input())

def inlt():
	return list(input())

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().strip().split())

def from_file(f):
	return f.readline

def count(row, x, y):
	m = len(row)
	index = 0
	l = 0
	res = 0
	while index < m:
		if row[index] == '*':
			if l > 0:
				if y >= 2 * x:
					res += l * x
				else:
					res += l // 2 * y + (x if l & 1 else 0)
			l = 0
		else:
			l += 1
		index += 1
	if l > 0:
		if y >= 2 * x:
			res += l * x
		else:
			res += l // 2 * y + (x if l & 1 else 0)
	return res
t = inp()
for _ in range(t):
	(n, m, x, y) = invr()
	res = 0
	for _ in range(n):
		row = input().strip()
		res += count(row, x, y)
	print(res)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	summ = 0
	for i in range(n):
		s = input()
		if '..' in s:
			p_x = 2 * x
			p_y = y
			if p_x > p_y:
				summ += s.count('..') * p_y
			else:
				summ += s.count('..') * p_x
		cnt_dot = len(s) - (2 * s.count('..') + s.count('*'))
		summ += cnt_dot * x
	print(summ)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if 2 * x <= y:
		for i in range(n):
			s = input()
			cost += s.count('.') * x
	else:
		for i in range(n):
			s = input()
			cnt = s.count('.')
			cnt2 = s.count('..')
			cost += (cnt - cnt2 * 2) * x + cnt2 * y
	print(cost)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c1 = 0
	c2 = 0
	for j in range(n):
		s = input()
		t1 = s.count('.')
		t2 = s.count('..')
		c1 += t2 * y + (t1 - 2 * t2) * x
		c2 += t1 * x
	print(min(c1, c2))


import math
t = int(input())
while t:
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for i in range(n):
		s = list(input())
		a.append(s)
	s = 0
	c11 = x
	c12 = min(2 * x, y)
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if j + 1 < m:
					if a[i][j + 1] == '.':
						s += c12
						a[i][j + 1] = '*'
					else:
						s += c11
						a[i][j] = '*'
				else:
					s += c11
					a[i][j] = '*'
	print(s)
	t -= 1


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = [[] for i in range(n)]
	cnt = 0
	for i in range(n):
		a[i] = list(input())
		cnt += a[i].count('.')
	if 2 * x <= y:
		print(x * cnt)
	else:
		cost = 0
		for i in range(n):
			t = 0
			while t < m:
				if a[i][t] == '.':
					if t != m - 1 and a[i][t + 1] == '.':
						cost += y
						t += 2
					else:
						cost += x
						t += 1
				else:
					t += 1
		print(cost)


def N():
	return int(input())

def NM():
	return map(int, input().split())

def L():
	return list(NM())

def LN(n):
	return [N() for i in range(n)]

def LL(n):
	return [L() for i in range(n)]
t = N()

def f():
	(n, m, x, y) = NM()
	y = min(y, x * 2)
	ans = 0
	for i in range(n):
		l = input() + '*'
		j = 0
		while j < m:
			if l[j] == '.':
				if l[j + 1] == '.':
					ans += y
				else:
					ans += x
				j += 1
			j += 1
	print(ans)
for i in range(t):
	f()


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for i in range(n):
		a1 = input()
		a.append(a1)
	count1 = 0
	count2 = 0
	if x * 2 <= y:
		for i in a:
			count1 += i.count('.')
		print(count1 * x)
	else:
		for i in a:
			count1 += i.count('..')
			count2 += i.count('.') - i.count('..') * 2
		print(count1 * y + count2 * x)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	A = []
	for i in range(n):
		A.append(input() + '*')
	ans = 0
	for i in range(n):
		j = 0
		s = -1
		while j <= m:
			while A[i][j] == '.':
				j += 1
			else:
				z = j - s - 1
				if z > 0 and 2 * x > y:
					ans += z // 2 * y
					ans += z % 2 * x
				elif z > 0 and 2 * x <= y:
					ans += z * x
				s = j
				j += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [input() for _ in range(n)]
	res = 0
	for row in a:
		c = 0
		for i in row:
			if i is '.':
				c += 1
			else:
				if y / 2 < x:
					res += c // 2 * y + c % 2 * x
				else:
					res += c * x
				c = 0
		if y / 2 < x:
			res += c // 2 * y + c % 2 * x
		else:
			res += c * x
	print(res)


from sys import stdin, stdout
t = int(stdin.readline())
for ii in range(t):
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	a = [[0] * m for i in range(n)]
	sum1 = 0
	for i in range(n):
		s = stdin.readline().rstrip()
		count = 0
		for j in range(len(s)):
			if s[j] == '.':
				count += 1
			else:
				if count == 1:
					sum1 += x
				elif count % 2 == 0:
					sum1 += min(count * x, count // 2 * y)
				else:
					sum1 += min(count * x, count // 2 * y + x)
				count = 0
		if count > 0:
			if count == 1:
				sum1 += x
			elif count % 2 == 0:
				sum1 += min(count * x, count // 2 * y)
			else:
				sum1 += min(count * x, count // 2 * y + x)
	stdout.write(str(sum1))
	stdout.write('\n')


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		s = input()
		arr.append(s)
	if 2 * x <= y:
		ans = 0
		for i in arr:
			ans += i.count('.')
		ans *= x
	else:
		ans = 0
		for i in arr:
			j = 0
			while j < m:
				if j + 1 < m:
					if i[j] == '.' and i[j + 1] == '.':
						ans += y
						j += 2
					elif i[j] == '.' and i[j + 1] != '.':
						ans += x
						j += 1
					else:
						j += 1
				elif len(i) == 1 and i[j] == '.':
					ans += x
					j += 1
				elif i[j] == '.' and j == m - 1:
					ans += x
					j += 1
				else:
					j += 1
	print(ans)


def task(n, m, x, y, a):
	ones = 0
	twos = 0
	for i in range(0, n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j < m - 1 and a[i][j + 1] == '.':
					twos += 1
				else:
					ones += 1
				j += 2
			else:
				j += 1
	ans = min(ones * x + twos * y, (ones + twos * 2) * x)
	print(ans)
t = int(input())
for i in range(0, t):
	a = []
	(n, m, x, y) = map(int, input().split())
	for i in range(0, n):
		a.append(input())
	task(n, m, x, y, a)


def solution():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		l = []
		for i in range(n):
			s = list(input())
			l.append(s)
		ans = 0
		i = 0
		while i < n:
			j = 0
			while j < m:
				if l[i][j] == '.':
					if j != m - 1:
						if l[i][j] == l[i][j + 1]:
							if 2 * x > y:
								ans += y
								j += 1
							else:
								ans += x
						else:
							ans += x
					else:
						ans += x
				j += 1
			i += 1
		print(ans)
solution()


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	(n, m, x, y) = map(int, I().split())
	final = 0
	for i in range(n):
		total1 = 0
		total2 = 0
		s = I().strip()
		for j in s.split('*'):
			if j:
				total1 += len(j) * x
				if len(j) % 2:
					total2 += len(j) // 2 * y + x
				else:
					total2 += len(j) // 2 * y
		final += min(total1, total2)
	ans += str(final) + '\n'
print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	grid = [input() for _ in range(n)]
	res = 0
	if x * 2 <= y:
		for r in grid:
			total = r.count('.')
			res += total * x
	else:
		for r in grid:
			total = r.count('.')
			double = r.count('..')
			single = total - 2 * double
			res += single * x + double * y
	print(res)


import math

def fun(tiles, x, y):
	flag = False
	if x * 2 <= y:
		flag = True
	count = 0
	for i in range(len(tiles)):
		if len(tiles[i]) == 1 and tiles[i][0] == '.':
			count += x
			continue
		else:
			j = 0
			while j < len(tiles[0]):
				if tiles[i][j] == '.' and j < len(tiles[0]) - 1 and (tiles[i][j + 1] == '.'):
					if not flag:
						count += y
						j += 2
					else:
						count += 2 * x
						j += 2
				elif tiles[i][j] != '.':
					j += 1
				else:
					count += x
					j += 1
	return count
for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	tiles = []
	for i in range(n):
		row = []
		inp = input()
		for i in inp:
			row.append(i)
		tiles.append(row)
	print(fun(tiles, x, y))


T = int(input())
for t in range(T):
	(n, m, x, y) = map(int, input().rstrip().split())
	(cost1, cost2) = (0, 0)
	for row in range(n):
		r = input()
		counter = 0
		for char in r:
			if char == '.':
				counter += 1
			else:
				cost1 += counter * x
				cost2 += counter // 2 * y + counter % 2 * x
				counter = 0
		cost1 += counter * x
		cost2 += counter // 2 * y + counter % 2 * x
	print(min(cost1, cost2))


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	mat = []
	for i in range(n):
		mat.append(input())
	flag = False
	if x * 2 <= y:
		y = x * 2
	total = 0
	for i in range(n):
		j = 0
		while j < m:
			if mat[i][j] == '.':
				if j + 1 < m and mat[i][j + 1] == '.':
					total += y
					j += 1
				else:
					total += x
			j += 1
	print(total)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	tab = []
	for _ in range(n):
		tab.append(input())
	if 2 * x <= y:
		ans = 0
		for i in range(n):
			for j in range(m):
				if tab[i][j] == '.':
					ans += 1
		print(ans * x)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if j + 1 < m:
					if tab[i][j] == '.':
						if tab[i][j + 1] == '.':
							ans += y
							j += 2
						else:
							ans += x
							j += 1
					else:
						j += 1
				elif tab[i][j] == '.':
					ans += x
					j += 1
				else:
					j += 1
		print(ans)


from sys import stdin, stdout

def find(arr, N, M, X, Y):
	if 2 * X <= Y:
		return sum([x.count('.') for x in arr] * X)
	r = 0
	for i in arr:
		j = 0
		while j < len(i):
			if i[j] == '*':
				j += 1
				continue
			if j < len(i) - 1 and i[j + 1] == '.':
				r += Y
				j += 2
			else:
				r += X
				j += 1
	return r

def main():
	for _ in range(int(stdin.readline())):
		(N, M, X, Y) = list(map(int, stdin.readline().split()))
		arr = []
		for __ in range(N):
			arr.append(stdin.readline().strip())
		print(find(arr, N, M, X, Y))
main()


import math
el = int(input())
for k in range(el):
	ar = []
	for i in input().split(' '):
		ar.append(int(i))
	matrix = []
	for i in range(ar[0]):
		matrix.append([char for char in input()])
	count = 0
	ch = 0
	if ar[2] * 2 > ar[3]:
		ch = 1
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == '*':
				continue
			if ch == 0:
				count = count + ar[2]
			elif j == len(matrix[0]) - 1:
				count = count + ar[2]
			elif matrix[i][j + 1] == '.':
				count = count + ar[3]
				matrix[i][j + 1] = '*'
			else:
				count = count + ar[2]
	print(count)


q = int(input())
for _ in range(q):
	(n, m, x, y) = map(int, input().split())
	d = [input() for i in range(n)]
	ans = 0
	if x * 2 < y:
		y = 2 * x
	for i in range(n):
		j = 0
		while j < m:
			if d[i][j] == '.':
				if j < m - 1 and d[i][j + 1] == '.':
					ans += y
					j += 1
				else:
					ans += x
			j += 1
	print(ans)


from math import *
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	c = min(x * 2, y)
	ans = 0
	for i in range(n):
		a = input()
		flag = 3
		j = 0
		k = 0
		while j < len(a) - 1:
			if a[j] == '.':
				if a[j + 1] == '.':
					flag = 1
					ans += c
					k = j
					j += 2
				else:
					flag = 3
					ans += x
					j += 1
			else:
				flag = 3
				j += 1
		if a[-1] == '.':
			if flag == 3 or k != m - 2:
				ans += x
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
v = []
w = []
for i in range(t):
	v1 = list(map(int, input().split()))
	v2 = [list(input()) for i in range(v1[0])]
	v.append(v1)
	w.append(v2)
ans = [0] * t
for i in range(t):
	n = v[i][0]
	m = v[i][1]
	x = v[i][2]
	y = v[i][3]
	cnt = 0
	if x * 2 <= y:
		for j in range(n):
			for k in range(m):
				if w[i][j][k] == '.':
					cnt += x
	else:
		for j in range(n):
			for k in range(m - 1):
				if w[i][j][k] == '.':
					if w[i][j][k + 1] == '.':
						w[i][j][k + 1] = '*'
						cnt += y
					else:
						cnt += x
			if w[i][j][-1] == '.':
				cnt += x
	ans[i] = cnt
for i in range(t):
	print(ans[i])


from itertools import groupby
t = int(input())
for _ in range(0, t):
	(n, m, x, y) = map(int, input().split())
	ss = 0
	for i in range(0, n):
		s = [i for i in input()]
		gb = [list(g) for (k, g) in groupby(s)]
		for gg in gb:
			if gg[0] == '.':
				ll = len(gg)
				if ll == 1:
					ss += x
				else:
					ss += min(ll * x, ll // 2 * y + ll % 2 * x)
	print(ss)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		c = input()
		b = [x for x in c]
		a.append(b)
	c = 0
	if 2 * x <= y:
		c = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					c += 1
		print(c * x)
	else:
		for i in range(n):
			j = 0
			while j < m:
				if a[i][j] == '*':
					j += 1
					continue
				elif j + 1 < m:
					if a[i][j + 1] == '.':
						c += y
						j += 2
					else:
						c += x
						j += 1
				else:
					c += x
					j += 1
		print(c)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for _ in range(n):
		row = input().split('*')
		for stretch in row:
			if x * 2 <= y:
				cost += len(stretch) * x
			else:
				cost += len(stretch) // 2 * y + (x if len(stretch) % 2 else 0)
	print(cost)


import sys
from bisect import bisect
from math import sqrt, floor, factorial, gcd
from collections import deque, Counter, defaultdict
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def main():
	ans = ''
	for _ in range(int(inp())):
		(n, m, x, y) = read()
		cost = min(2 * x, y)
		tot = 0
		for i in range(n):
			arr = [len(j) for j in inp().strip().split('*') if j]
			for i in arr:
				tot += cost * (i // 2) + x * (i % 2)
		ans += str(tot) + '\n'
	print(ans)
main()


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def seive():
	l = 100002
	c = [1, 1] + [0] * l
	for i in range(2, l):
		if 1 - c[i]:
			for j in range(i * i, l, i):
				c[j] = 1

def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
P = 1000000007

def main():
	for t in range(I()):
		(n, m, x, y) = In()
		sm = 0
		for i in range(n):
			s = input()
			j = 0
			while j < m:
				if s[j] == '.':
					if j + 1 < m and s[j + 1] == '.':
						sm += min(2 * x, y)
						j += 1
					else:
						sm += x
				j += 1
		print(sm)
main()


def main():
	for _ in range(int(input())):
		(n, m, single, double) = map(int, input().split())
		grid = []
		ans = 0
		for i in range(n):
			grid.append(input())
			j = 0
			while j < m:
				if grid[i][j] == '*':
					j += 1
					continue
				if j == m - 1:
					ans += single
					j += 1
					break
				if grid[i][j + 1] == '.':
					ans += min(2 * single, double)
					j += 2
				else:
					ans += single
					j += 1
		print(ans)
main()


for i in range(int(input())):
	list = [int(u) for u in input().split()]
	n = list[0]
	m = list[1]
	x = list[2]
	y = list[3]
	sum = 0
	for z in range(n):
		l = input()
		w = ''
		for k in range(len(l)):
			ch = l[k]
			if ch != '*':
				w += ch
			if ch == '.' and k == len(l) - 1 or ch == '*':
				a = w[:].count('.')
				if a % 2 == 0:
					if y > 2 * x:
						sum += a * x
					else:
						sum += a / 2 * y
					w = ''
				else:
					if y > 2 * x:
						sum += a * x
					else:
						d = x + (a - 1) / 2 * y
						sum += d
					w = ''
	print(int(sum))


from sys import stdin, stdout

def solve():
	(n, m, x, y) = map(int, input().split())
	y = min(y, x << 1)
	ans = 0
	for _ in range(n):
		s = input() + '*'
		i = 0
		if m == 1:
			if s[0] == '.':
				ans += x
		else:
			while i < m:
				if s[i] == '.':
					i += 1
					if s[i] == '.':
						ans += y
					else:
						ans += x
				i += 1
	return ans
for _ in range(int(stdin.readline())):
	stdout.write(str(solve()) + '\n')


import math
a = int(input())
for i in range(a):
	price = 0
	b = [int(x) for x in input().split()]
	if b[3] > 2 * b[2]:
		b[3] = 2 * b[2]
	for i in range(b[0]):
		j = 0
		c = input()
		while j < b[1]:
			if j != b[1] - 1 and c[j] == '.' and (c[j + 1] == '.'):
				price += b[3]
				j += 2
			elif c[j] == '.':
				price += b[2]
				j += 1
			else:
				j += 1
	print(price)


from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline
sqrt = math.sqrt

def LI():
	return list(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def LI_():
	return list(map(lambda x: int(x) - 1, input().split()))

def II():
	return int(input())

def IF():
	return float(input())

def S():
	return input().rstrip()

def LS():
	return S().split()

def IR(n):
	return [II() for _ in range(n)]

def LIR(n):
	return [LI() for _ in range(n)]

def FR(n):
	return [IF() for _ in range(n)]

def LFR(n):
	return [LI() for _ in range(n)]

def LIR_(n):
	return [LI_() for _ in range(n)]

def SR(n):
	return [S() for _ in range(n)]

def LSR(n):
	return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

def solve():
	ans = []
	for i in range(II()):
		(n, m, X, Y) = LI()
		field = SR(n)
		if X * 2 <= Y:
			tmp = 0
			for i in range(n):
				tmp += field[i].count('.')
			ans.append(tmp * X)
		else:
			tmp = 0
			for y in range(n):
				f = 0
				for x in range(m):
					if f:
						f = 0
					elif x < m - 1:
						if field[y][x] == '.' and field[y][x + 1] == '.':
							tmp += Y
							f = 1
						elif field[y][x] == '.':
							tmp += X
					elif field[y][x] == '.':
						tmp += X
			ans.append(tmp)
	for ai in ans:
		print(ai)
	return
solve()


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	final_cost = 0
	for i in range(n):
		line = input()
		count = 0
		for i in range(m):
			if line[i] == '*':
				if count > 0:
					if x <= y:
						if 2 * x <= y:
							final_cost += x * count
						elif count % 2 == 0:
							final_cost += y * (count // 2)
						else:
							final_cost += y * (count // 2) + x
					elif count % 2 == 1:
						final_cost += y * (count // 2) + x
					else:
						final_cost += y * (count // 2)
				count = 0
			else:
				count += 1
		if count > 0:
			if x <= y:
				if 2 * x <= y:
					final_cost += x * count
				elif count % 2 == 0:
					final_cost += y * (count // 2)
				else:
					final_cost += y * (count // 2) + x
			elif count % 2 == 1:
				final_cost += y * (count // 2) + x
			else:
				final_cost += y * (count // 2)
	print(final_cost)


n = int(input())
arr = []
for i in range(n):
	kekq = True
	(n, m, x, y) = map(int, input().split())
	if y > 2 * x:
		kekq = False
	F = []
	cost = 0
	for j in range(n):
		F.append(list(input()))
	for j in range(n):
		for k in range(m):
			if kekq and k < m - 1 and (F[j][k] == '.') and (F[j][k + 1] == '.'):
				cost += y
				(F[j][k], F[j][k + 1]) = ('*', '*')
			elif F[j][k] == '.':
				cost += x
	arr.append(cost)
for i in range(len(arr)):
	print(arr[i])


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for _ in range(n):
		arr.append(list(input()) + ['*'])
	cost = 0
	if 2 * x < y:
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '.':
					cost += x
	else:
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '.' and arr[i][j + 1] == '.':
					arr[i][j] = '*'
					arr[i][j + 1] = '*'
					cost += y
				elif arr[i][j] == '.':
					arr[i][j] = '*'
					cost += x
	print(cost)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	k = []
	t = 0
	f = 0
	for i in range(a):
		s = input()
		k.append(s)
	for i in range(a):
		t = t + k[i].count('.')
		f = f + k[i].count('..')
	q = t - 2 * f
	if f != 0:
		print(min(c * q + f * d, t * c))
	else:
		print(t * c)


import math
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	rows = ['' for x in range(n)]
	for i in range(n):
		rows[i] = input()
	if 2 * x <= y:
		count = 0
		for row in rows:
			for ch in row:
				if ch == '.':
					count += 1
		print(count * x)
	else:
		currentCount = 0
		totalCountY = 0
		totalCountX = 0
		cost = 0
		for row in rows:
			for ch in row:
				if ch == '.':
					currentCount += 1
				else:
					totalCountY += int(currentCount // 2)
					if currentCount % 2 != 0:
						totalCountX += 1
					currentCount = 0
			if currentCount != 0:
				totalCountY += int(currentCount // 2)
				if currentCount % 2 != 0:
					totalCountX += 1
			cost += x * totalCountX + y * totalCountY
			currentCount = 0
			totalCountX = 0
			totalCountY = 0
		print(cost)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for i in range(n):
		b = input()
		j = 0
		while j < m:
			if y < 2 * x:
				if j != m - 1:
					if b[j] == b[j + 1] == '.':
						cost += y
						j += 1
					elif b[j] == '.':
						cost += x
				elif b[j] == '.':
					cost += x
			elif b[j] == '.':
				cost += x
			j += 1
	print(cost)


def pave(n, m, x, y):
	res = 0
	if y > x * 2:
		y = x * 2
	for i in range(n):
		line = input()
		ps = line.split('*')
		for p in ps:
			pc = len(p)
			pv = pc // 2 * y + pc % 2 * x
			res += pv
	return res

def solve():
	case = int(input())
	for i in range(case):
		(n, m, x, y) = list(map(int, input().split()))
		res = pave(n, m, x, y)
		print(res)
solve()


def main():
	t = int(input())
	for __ in range(t):
		(n, m, x, y) = map(int, input().split())
		single = 0
		double = 0
		for i in range(n):
			s = input()
			j = 0
			while j < m:
				if s[j] == '.' and j + 1 < m and (s[j + 1] == '.'):
					double += 1
					j += 2
				elif s[j] == '.':
					single += 1
					j += 1
				else:
					j += 1
		cost = single * x + min((2 * x * double, double * y))
		print(cost)
main()


t = int(input())
for _ in range(t):
	(n, m, x, y1) = map(int, input().split(' '))
	matrix = []
	two = 0
	one = 0
	for j in range(n):
		s = input() + '*'
		y = []
		for i in s:
			y.append(i)
		i = 1
		while i <= m:
			if y[i] == '.':
				if y[i - 1] == '.':
					two += 1
					i += 1
				elif y[i + 1] != '.':
					one += 1
					i += 1
			elif y[i - 1] == '.':
				one += 1
				i += 1
			i += 1
		matrix.append(y)
	ans = one * x
	ans1 = min(x * (2 * two), y1 * two)
	print(ans + ans1)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if x * 2 > y:
		r = 0
		for i in range(n):
			c = 0
			l = input()
			for j in range(m):
				if l[j] == '.':
					c += 1
					if c == 2:
						c = 0
						r += y
				else:
					if c == 1:
						r += x
					c = 0
			if c == 1:
				r += x
	else:
		r = 0
		for i in range(n):
			l = input()
			for j in range(m):
				if l[j] == '.':
					r += x
	print(r)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	total = 0
	for a in range(n):
		a = input()
		l = list(a)
		while len(l) > 0:
			l_aux = 0
			while len(l) > 0 and l.pop() == '.':
				l_aux += 1
			if l_aux == 1:
				total += x
			elif l_aux != 0 and l_aux % 2 == 0:
				if 2 * x > y:
					total += l_aux / 2 * y
				else:
					total += l_aux * x
			elif l_aux != 0:
				if 2 * x > y:
					total += l_aux // 2 * y
					total += x
				else:
					total += l_aux * x
	print(int(total))


import math
T = int(input())
for tt in range(0, T):
	(n, m, x, y) = map(int, input().split())
	a = []
	c = 0
	d = []
	e = []
	for i in range(0, n):
		b = input()
		cc = b.count('.')
		e.append(b.count('..'))
		d.append(cc)
		c = c + cc
		a.append(b)
	if 2 * x <= y:
		print(c * x)
		continue
	s = 0
	for i in range(0, n):
		l = d[i] - 2 * e[i]
		s = s + l * x + e[i] * y
	print(s)


def f(ele):
	if ele == '.':
		return 'W'
	return 'B'
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(map(f, input())))
	ans = 0
	flag = True
	if 2 * x < y:
		flag = False
	for i in range(n):
		for j in range(m):
			if l[i][j] == 'B':
				continue
			if j == 0:
				l[i][j] = 'S'
				ans += x
				continue
			if l[i][j - 1] == 'S' and flag == True:
				ans = ans - x + y
			else:
				ans += x
				l[i][j] = 'S'
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c1 = 0
	c2 = 0
	ans = 0
	for i in range(n):
		s = input()
		c1 = s.count('.')
		c2 = s.count('..')
		if 2 * x <= y:
			ans += x * c1
		else:
			ans += (c1 - 2 * c2) * x + c2 * y
	print(ans)


def main(mat, x, y):
	(m, n) = (len(mat), len(mat[0]))
	if 2 * x <= y:
		return sum((v == '.' for r in mat for v in r)) * x
	else:
		ans = 0
		for r in mat:
			p = 0
			for v in r + ['*']:
				if v == '.':
					p += 1
				else:
					(dv, md) = divmod(p, 2)
					ans += dv * y + md * x
					p = 0
		return ans
N = int(input())
for _ in range(N):
	(n, m, x, y) = list(map(int, input().split(' ')))
	mat = []
	for _ in range(n):
		mat.append(list(input()))
	print(main(mat, x, y))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = y
	if y >= 2 * x:
		s = 2 * x
	p = 0
	for i in range(n):
		a = input()
		for j in range(m):
			if '..' in a:
				p += s
				a = a.replace('..', '', 1)
				continue
			elif '.' in a:
				p += x
				a = a.replace('.', '', 1)
	print(p)


t = int(input())
while t:
	(n, m, x, y) = map(int, input().split())
	mat = []
	cnt = 0
	for i in range(n):
		temp = list(input())
		for i in range(m):
			if temp[i] == '.':
				cnt += 1
		mat.append(temp)
	if 2 * x <= y:
		print(x * cnt)
	else:
		cnt1 = 0
		for i in range(n):
			cnt2 = 0
			j = 0
			while j < m - 1:
				if mat[i][j] == '.' and mat[i][j + 1] == '.':
					cnt1 += 1
					j += 1
				j += 1
		ans = cnt1 * y + (cnt - 2 * cnt1) * x
		print(ans)
	t -= 1


from math import sqrt
t = int(input())
for qq in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	p1 = 0
	p2 = 0
	for i in range(n):
		cur = 0
		row = input()
		for j in range(m):
			if row[j] == '.':
				cur += 1
			else:
				p1 += cur * x
				p2 += cur // 2 * y + cur % 2 * x
				cur = 0
		p1 += cur * x
		p2 += cur // 2 * y + cur % 2 * x
	print(min(p1, p2))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [[j for j in input()] for i in range(n)]
	ans = 0
	if y >= 2 * x:
		for i in range(n):
			ans += a[i].count('.') * x
	else:
		for i in range(n):
			for j in range(m - 1):
				if a[i][j] == '.' and a[i][j + 1] == '.':
					a[i][j] = '*'
					a[i][j + 1] = '*'
					ans += y
		for i in range(n):
			ans += a[i].count('.') * x
	print(ans)


inp = lambda : map(int, input().split())
t = int(input())
for _ in range(t):
	(n, m, x, y) = inp()
	cost = 0
	for i in range(n):
		s = input()
		l = list(s)
		j = 0
		while j < len(l):
			if l[j] == '.':
				if j != len(l) - 1:
					if l[j + 1] == '.':
						if 2 * x >= y:
							cost += y
							j += 2
						else:
							cost += 2 * x
							j += 2
					else:
						cost += x
						j += 1
				else:
					cost += x
					j += 1
			else:
				j += 1
	print(cost)


t = int(input())
for k in range(0, t):
	entry = input().split(' ')
	n = int(entry[0])
	m = int(entry[1])
	x = int(entry[2])
	y = int(entry[3])
	one_by_one = 0
	one_by_two = 0
	tiles = []
	tiles = [[] for i in range(0, n)]
	for i in range(0, n):
		l = input()
		for s in range(0, len(l)):
			if l[s] == '.':
				tiles[i].append('white')
			elif l[s] == '*':
				tiles[i].append('black')
		j = 0
		while j < m:
			if tiles[i][j] == 'white':
				if j < m - 1 and tiles[i][j + 1] == 'white':
					price_one_by_one = x * 2
					price_one_by_two = y * 1
					if price_one_by_one >= price_one_by_two:
						one_by_two += 1
						j += 1
					else:
						one_by_one += 1
				else:
					one_by_one += 1
			j += 1
	price = x * one_by_one + y * one_by_two
	print(price)


import sys
input = sys.stdin.readline

def solve(n, m, x, y, Map):
	cnt = 0
	for M in Map:
		cnt += M.count('.')
	if 2 * x <= y:
		return x * cnt
	double_cnt = 0
	for M in Map:
		flag = M[0] == '.'
		for i in range(1, m):
			s = M[i]
			if s == '*':
				flag = False
			elif flag:
				double_cnt += 1
				flag = False
			else:
				flag = True
	return x * (cnt - double_cnt * 2) + y * double_cnt

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		Map = [list(input().strip()) for _ in range(n)]
		ans = solve(n, m, x, y, Map)
		sys.stdout.write(str(ans) + '\n')
main()


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(n, m, x, y) = int_array()
	a = []
	for __ in range(n):
		a.append(list(input().strip()))
	ans = 0
	for i in a:
		j = 0
		while j < m:
			if j == m - 1 and i[j] == '.':
				ans += x
				j += 1
			elif i[j] == '.' and i[j + 1] == '.':
				ans += min(2 * x, y)
				j += 2
			elif i[j] == '.' and i[j + 1] != '.':
				ans += x
				j += 1
			else:
				j += 1
	print(ans)


tc = int(input())
while tc != 0:
	tc -= 1
	(n, m, x, y) = map(int, input().strip().split())
	if 2 * x <= y:
		y = 2 * x
	a = []
	for i in range(n):
		p = input()
		a.append(p)
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j == m - 1:
					ans += x
				elif a[i][j + 1] == '.':
					ans += y
					j += 1
				else:
					ans += x
			j += 1
	print(ans)


q = int(input())
for _ in range(q):
	(n, m, x, y) = list(map(int, input().split(' ')))
	arr = []
	for i in range(n):
		arr.append(list(input()))
	(i, j, cost) = (0, 0, 0)
	while True:
		if j >= m:
			j = 0
			i += 1
			if i >= n:
				break
		if arr[i][j] == '.':
			if j + 1 < m:
				if arr[i][j + 1] == '.':
					if y <= x * 2:
						cost += y
					else:
						cost += 2 * x
					j += 2
				else:
					cost += x
					j += 1
			else:
				cost += x
				j += 1
		else:
			j += 1
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = [int(x) for x in input().split()]
	ans = 0
	for i in range(n):
		s = input()
		pp = s.count('..')
		p = s.count('.') - 2 * pp
		ans += min(pp * y + p * x, (pp * 2 + p) * x)
	print(ans)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def bit_combination(n, base=2):
	rt = []
	for tb in range(base ** n):
		s = [tb // base ** bt % base for bt in range(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class Tree:

	def __init__(self, inp_size=None, ls=None, init=True, index=0):
		self.LCA_init_stat = False
		self.ETtable = []
		if init:
			if ls == None:
				self.stdin(inp_size, index=index)
			else:
				self.size = len(ls) + 1
				(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def stdin(self, inp_size=None, index=1):
		if inp_size == None:
			self.size = int(input())
		else:
			self.size = inp_size
		(self.edges, _) = GI(self.size, self.size - 1, index=index)
		return

	def listin(self, ls, index=0):
		self.size = len(ls) + 1
		(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def __str__(self):
		return str(self.edges)

	def dfs(self, x, func=lambda prv, nx, dist: prv + dist, root_v=0):
		q = deque()
		q.append(x)
		v = [-1] * self.size
		v[x] = root_v
		while q:
			c = q.pop()
			for (nb, d) in self.edges[c]:
				if v[nb] == -1:
					q.append(nb)
					v[nb] = func(v[c], nb, d)
		return v

	def EulerTour(self, x):
		q = deque()
		q.append(x)
		self.depth = [None] * self.size
		self.depth[x] = 0
		self.ETtable = []
		self.ETdepth = []
		self.ETin = [-1] * self.size
		self.ETout = [-1] * self.size
		cnt = 0
		while q:
			c = q.pop()
			if c < 0:
				ce = ~c
			else:
				ce = c
				for (nb, d) in self.edges[ce]:
					if self.depth[nb] == None:
						q.append(~ce)
						q.append(nb)
						self.depth[nb] = self.depth[ce] + 1
			self.ETtable.append(ce)
			self.ETdepth.append(self.depth[ce])
			if self.ETin[ce] == -1:
				self.ETin[ce] = cnt
			else:
				self.ETout[ce] = cnt
			cnt += 1
		return

	def LCA_init(self, root):
		self.EulerTour(root)
		self.st = SparseTable(self.ETdepth, init_func=min, init_idl=inf)
		self.LCA_init_stat = True
		return

	def LCA(self, root, x, y):
		if self.LCA_init_stat == False:
			self.LCA_init(root)
		(xin, xout) = (self.ETin[x], self.ETout[x])
		(yin, yout) = (self.ETin[y], self.ETout[y])
		a = min(xin, yin)
		b = max(xout, yout, xin, yin)
		id_of_min_dep_in_et = self.st.query_id(a, b + 1)
		return self.ETtable[id_of_min_dep_in_et]

class SparseTable:

	def __init__(self, ls, init_func=min, init_idl=float('inf')):
		self.func = init_func
		self.idl = init_idl
		self.size = len(ls)
		self.N0 = self.size.bit_length()
		self.table = [ls[:]]
		self.index = [list(range(self.size))]
		self.lg = [0] * (self.size + 1)
		for i in range(2, self.size + 1):
			self.lg[i] = self.lg[i >> 1] + 1
		for i in range(self.N0):
			tmp = [self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) for j in range(self.size)]
			tmp_id = [self.index[i][j] if self.table[i][j] == self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) else self.index[i][min(j + (1 << i), self.size - 1)] for j in range(self.size)]
			self.table += [tmp]
			self.index += [tmp_id]

	def query(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		return self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))])

	def query_id(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		(a, b) = (self.index[N][l], self.index[N][max(0, r - (1 << N))])
		if self.table[0][a] == self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))]):
			b = a
		return b

	def __str__(self):
		return str(self.table[0])

	def print(self):
		for i in self.table:
			print(*i)
show_flg = False
show_flg = True
ans = 0

def solve():
	return
T = I()
for _ in range(T):
	(n, m, x, y) = LI()
	t = []
	ans = 0
	for i in range(n):
		s = [1 if i == '*' else 0 for i in input()]
		t += [s]
	if y > 2 * x:
		y = x * 2
	for i in range(n):
		for j in range(m):
			if t[i][j] == 0:
				if j < m - 1 and t[i][j + 1] == 0:
					ans += y
					t[i][j] = 1
					t[i][j + 1] = 1
				else:
					ans += x
					t[i][j] = 1
	print(ans)


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	(n, m, x, y) = nm()
	y = min(y, 2 * x)
	ans = 0
	for _ in range(n):
		s = ns().split('*')
		for f in s:
			ans += len(f) // 2 * y + len(f) % 2 * x
	print(ans)
	return
T = ni()
for _ in range(T):
	solve()


from heapq import *
from collections import *
import os
import sys
from io import BytesIO, IOBase
import math
ins = lambda : [int(x) for x in input()]
inp = lambda : int(input())
inps = lambda : [int(x) for x in input().split()]

def main():
	t = inp()
	for i in range(t):
		(n, m, x, y) = inps()
		tb = []
		for i in range(n):
			tb.append(input())
		cost = 0
		if y < x * 2:
			(adj2, adj1) = (0, 0)
			for i in range(n):
				r = 0
				while r < m:
					if tb[i][r] == '*':
						r += 1
						continue
					if r + 1 < m and tb[i][r] == tb[i][r + 1]:
						adj2 += 1
						r += 2
					else:
						adj1 += 1
						r += 1
			cost = adj2 * y + adj1 * x
		else:
			adj1 = 0
			for i in range(n):
				for j in range(m):
					if tb[i][j] == '.':
						adj1 += 1
			cost = adj1 * x
		print(cost)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math

def solve():
	(n, m, x, y) = map(int, input().split())
	tot = 0
	costOne = x
	costTwo = min(2 * x, y)
	for i in range(n):
		l = input().split('*')
		for i in l:
			if len(i) == 0:
				continue
			if len(i) <= 1:
				tot += costOne
			else:
				tot += len(i) // 2 * costTwo + len(i) % 2 * costOne
	print(tot)
	return
t = 1
t = int(input())
while t > 0:
	t -= 1
	solve()


strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	(n, m, x, y) = intInp()
	total = 0
	arr = []
	for i in range(n):
		arr.append(list(input()))
	if y < 2 * x:
		for i in range(n):
			for j in range(m - 1):
				if arr[i][j] == '.' and arr[i][j + 1] == '.':
					arr[i][j] = '*'
					arr[i][j + 1] = '*'
					total += y
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.':
				total += x
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if y < 2 * x:
		g = 1
	else:
		g = 0
	for i in range(n):
		h = list(str(input()))
		h.append('*')
		if g == 0:
			ans += h.count('.') * x
		else:
			(j, p, q) = (0, 0, 0)
			while j < m:
				if h[j] == '.':
					if h[j + 1] == '.':
						(p, j) = (p + 1, j + 1)
					else:
						q += 1
				j += 1
			ans += p * y + q * x
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = []
	for i in range(n):
		f = input()
		s.append(f)
	price = 0
	for f in s:
		seg = 0
		for i in range(m):
			if f[i] == '.':
				seg += 1
			if f[i] == '*' or i == m - 1:
				if 2 * x < y:
					price += seg // 2 * 2 * x
				else:
					price += seg // 2 * y
				seg = seg % 2
				price += seg * x
				seg = 0
	print(price)


import sys
import os.path
if os.path.isfile('in.txt'):
	sys.stdin = open('in.txt', 'r', encoding='utf-8')
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	layout = ['' for i in range(n)]
	for i in range(n):
		layout[i] = input()
	if x * 2 > y:
		cost = 0
		for i in range(n):
			space = 0
			for j in range(m):
				if layout[i][j] == '.':
					space += 1
				else:
					cost += (space >> 1) * y
					cost += (space & 1) * x
					space = 0
			cost += (space >> 1) * y
			cost += (space & 1) * x
		print(cost)
	else:
		cost = 0
		for i in range(n):
			for j in range(m):
				if layout[i][j] == '.':
					cost += x
		print(cost)


for test_var in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	string = []
	for _ in range(a):
		string.append(input())
	(two, single) = (0, 0)
	for one in string:
		i = 0
		while i < b:
			if one[i] == '.':
				if i < b - 1:
					if one[i + 1] == '.':
						two += 1
						i += 2
					else:
						single += 1
						i += 2
				else:
					i += 1
					single += 1
			else:
				i += 1
	if c * 2 > d:
		print(two * d + single * c)
	else:
		print(two * 2 * c + c * single)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	q = 0
	s = 0
	for i in range(n):
		l = str(input())
		if 2 * x < y:
			q += l.count('.')
		else:
			for j in range(len(l)):
				if l[j] == '.':
					s += 1
				else:
					p += s // 2
					q += s - 2 * (s // 2)
					s = 0
			if s > 0:
				p += s // 2
				q += s - 2 * (s // 2)
				s = 0
	print(q * x + p * y)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	res = 0
	for index in range(n):
		l = input()
		i = 0
		while i < m:
			if l[i] == '*':
				i += 1
				continue
			elif i + 1 < m and l[i + 1] == '.':
				res += min(2 * x, y)
				i += 2
			else:
				res += x
				i += 1
	print(res)


ans = []
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		y = 2 * x
	u = []
	ansi = 0
	for i in range(n):
		u.append(list(input()))
		for j in range(1, m):
			if u[i][j] == u[i][j - 1] == '.':
				u[i][j] = u[i][j - 1] = '*'
				ansi += y
		for j in range(m):
			if u[i][j] == '.':
				ansi += x
	ans.append(ansi)
print('\n'.join(map(str, ans)))


from math import *
a = int(input())
for x in range(a):
	(b, c, d, e) = map(int, input().split())
	n = 0
	for y in range(b):
		f = input()
		p = 0
		while p < c:
			if p == c - 1 and f[p] == '*':
				p += 1
			elif p == c - 1 and f[p] == '.':
				n += d
				p += 1
			elif f[p] == '*':
				p += 1
				continue
			elif f[p] == '.' and f[p + 1] == '.':
				if 2 * d < e:
					p += 2
					n += 2 * d
				else:
					p += 2
					n += e
			elif f[p] == '.':
				p += 1
				n += d
	print(n)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if 2 * x <= y:
		for _ in range(n):
			s = input()
			ans += s.count('.') * x
	else:
		for _ in range(n):
			s = input()
			c = 0
			i = 0
			while i < len(s):
				if s[i] == '.':
					c += 1
					i += 1
				else:
					ans += c // 2 * y + c % 2 * x
					c = 0
					i += 1
			ans += c // 2 * y + c % 2 * x
	print(ans)


def main():
	(n, m, x, y) = map(int, input().split())
	res = 0
	for i in range(n):
		line = input()
		k = 0
		two = 0
		one = 0
		while k < m:
			if line[k] == '.':
				if k + 1 < m and line[k + 1] == '.':
					two += 1
					k += 2
				else:
					one += 1
					k += 1
			else:
				k += 1
		if 2 * x <= y:
			res += x * (one + two * 2)
		else:
			res += one * x
			res += two * y
	print(res)
t = int(input())
for i in range(t):
	main()


q = int(input())
f = 'd'
for k in range(q):
	f = 'd'
	s = 0
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(input()))
	if x * 2 < y:
		f = 'u'
	for i in range(n):
		for j in range(m):
			if l[i][j] == '.':
				l[i][j] = '69'
				if f == 'u':
					s += x
				elif j + 1 < m and l[i][j + 1] == '.':
					s += y
					l[i][j + 1] = '69'
				else:
					s += x
	print(s)


def length(row):
	count1 = 0
	count2 = 0
	consecutive = 0
	for i in row:
		if i == '.':
			consecutive += 1
		else:
			count1 += consecutive % 2
			count2 += consecutive // 2
			consecutive = 0
	count1 += consecutive % 2
	count2 += consecutive // 2
	return (count1, count2)
CASES = int(input())
for i in range(CASES):
	(m, n, x, y) = map(int, input().split())
	cost = 0
	flag = 2 * x < y
	for j in range(m):
		(count1, count2) = length(input())
		if flag:
			cost += count1 * x + 2 * count2 * x
		else:
			cost += count1 * x + count2 * y
	print(cost)


t = int(input())
for i in range(t):
	(n, m, a, b) = [int(el) for el in input().split()]
	if 2 * a <= b:
		s = 0
		for j in range(n):
			s += input().count('.')
		print(a * s)
	else:
		s = 0
		for j in range(n):
			c = [el for el in input()]
			for i in range(m):
				if c[i] == '.' and i <= m - 2 and (c[min(i + 1, m - 1)] == '.'):
					c[i + 1] = '*'
					s += b
				elif c[i] == '.':
					s += a
		print(s)


from sys import stdin
input = stdin.readline
MX = int(200000.0 + 5)
MOD = int(1000000000.0 + 7)

def main():
	test = int(input())
	for _ in range(test):
		(n, m, x, y) = map(int, input().split())
		ans = 0
		for _ in range(n):
			row = input()
			last = '*'
			for ch in row:
				if ch == '.':
					if last == '.':
						ans -= x
						ans += min(2 * x, y)
						last = '*'
					else:
						ans += x
						last = '.'
				else:
					last = '*'
		print(ans)
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	(N, M, A, B) = map(int, input().split())
	if B > 2 * A:
		B = 2 * A
	X = [[0 if s == '*' else 1 for s in input()] for _ in range(N)]
	s = 0
	for i in range(N):
		prev = 0
		for j in range(M):
			if X[i][j]:
				if prev:
					s += B - A
					prev = 0
				else:
					s += A
					prev = 1
			else:
				prev = 0
	print(s)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for _ in range(n):
		curr = list(input())
		arr.append(curr)
	b = '*'
	w = '.'
	cost = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if arr[i][j] == w:
				if j + 1 < m:
					if arr[i][j + 1] == w:
						cost += min(2 * x, y)
						j += 1
					else:
						cost += x
						j += 1
				else:
					cost += x
					j += 1
			j += 1
		i += 1
	print(cost)


t = int(input())
for garbage in range(t):
	(n, m, x, y) = [int(i) for i in input().split(' ')]
	count_x = 0
	count_y = 0
	total_dots = 0
	for j in range(n):
		s = input()
		s = s.split('*')
		for i in s:
			if len(i) != 0:
				total_dots += len(i)
				count_y += len(i) // 2
				count_x += len(i) % 2
	sum1 = count_x * x + count_y * y
	if total_dots * x < sum1:
		print(total_dots * x)
	else:
		print(sum1)


import math
t = int(input())
for z in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	l = []
	for i in range(n):
		l.append(input())
	f = False
	if 2 * x < y:
		f = True
	cost = 0
	if f:
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					cost += 1
		print(cost * x)
	else:
		c1 = c2 = 0
		for i in range(n):
			j = 0
			while j < m:
				if j < m - 1:
					if l[i][j] == '.' and l[i][j + 1] == '.':
						c2 += 1
						j += 1
					elif l[i][j] == '.':
						c1 += 1
				elif l[i][j] == '.':
					c1 += 1
				j += 1
		print(int(c1 * x + c2 * y))


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	for _ in range(ii()):
		(n, m, x, y) = mi()
		ans = 0
		a = []
		for i in range(n):
			a.append(si())
		x1 = []
		for i in range(n):
			y1 = []
			c = 0
			for j in range(m):
				if a[i][j] == '*':
					if c > 0:
						y1.append(c)
					c = 0
				else:
					c += 1
			if c > 0:
				y1.append(c)
			x1.append(y1)
		for i in range(n):
			for j in x1[i]:
				if y < 2 * x:
					ans += j // 2 * y
					ans += j % 2 * x
				else:
					ans += j * x
		print(ans)
solve()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	tot = 0
	sin = 0
	dual = 0
	for k in range(n):
		s = input() + '#'
		t = 0
		for i in range(m + 1):
			if s[i] == '.':
				t += 1
			else:
				sin += t % 2
				dual += t // 2
				tot += t
				t = 0
	if y >= 2 * x:
		print(tot * x)
	else:
		print(dual * y + sin * x)


t = int(input())
for _ in range(0, t):
	(n, m, x, y) = map(int, input().split())
	f = 0
	c = '*'
	ans = 0
	if 2 * x <= y:
		f = 1
	for i in range(0, n):
		s = input()
		for j in range(0, m):
			if c == '.' and s[j] == '.':
				if f == 1:
					ans += x
				else:
					ans += y - x
				c = '*'
			elif s[j] == '.':
				c = '.'
				ans += x
			else:
				c = '*'
				continue
		c = '*'
	print(ans)


from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect
import math
import heapq
import sys
import io, os
for ii in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		temp = list(input())
		arr.append(temp)
	ans = 0
	for i in range(n):
		for j in range(m - 1):
			if arr[i][j] == '.' and arr[i][j + 1] == '.':
				ans += min(x * 2, y)
				arr[i][j] = '*'
				arr[i][j + 1] = '*'
			elif arr[i][j] == '.':
				ans += x
				arr[i][j] = '*'
		if arr[i][m - 1] == '.':
			ans += x
			arr[i][m - 1] = '*'
	print(ans)


t = int(input())
while t != 0:
	(n, m, x, y) = map(int, input().split())
	a = [input() for i in range(n)]
	k = 0
	l = 0
	for i in range(n):
		b = a[i]
		k += b.count('.') - 2 * b.count('..')
		l += b.count('..')
	if k * x + 2 * l * x < k * x + l * y:
		print(k * x + 2 * l * x)
	else:
		print(k * x + l * y)
	t -= 1


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	rows = []
	c2 = 0
	c1 = 0
	for i in range(n):
		inp = input()
		rows.append(inp)
		c2 += inp.count('..')
		c1 += inp.count('.')
	c1only = c1 - c2 * 2
	p1 = c1 * x
	p2 = c2 * y + c1only * x
	print(min(p1, p2))


t = int(input())
results = []
for case in range(t):
	(n, m, x, y) = map(int, input().split())
	singles = 0
	pairs = 0
	for i in range(n):
		row = input()
		j = 0
		while j < m:
			if row[j] == '.':
				singles += 1
				if j + 1 < m:
					if row[j + 1] == '.':
						singles -= 1
						pairs += 1
						j += 1
			j += 1
	cost = 0
	if x * 2 <= y:
		cost = x * (singles + 2 * pairs)
	else:
		cost = x * singles + y * pairs
	results.append(cost)
for result in results:
	print(result)


t = int(input())
for k in range(t):
	nmxy = input().split()
	n = int(nmxy[0])
	m = int(nmxy[1])
	x = int(nmxy[2])
	y = int(nmxy[3])
	arr = []
	count = 0
	for i in range(n):
		temp = input()
		arr.append(temp)
	for i in range(n):
		j = 0
		while j < m:
			try:
				if arr[i][j] == '.' and arr[i][j + 1] == '.':
					count += min(2 * x, y)
					j += 2
				else:
					if arr[i][j] == '.':
						count += x
					j += 1
			except:
				if arr[i][j] == '.':
					count += x
				j += 1
	print(count)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	k = [0] * n
	for i in range(n):
		k[i] = input()
	total = 0
	for i in range(n):
		flag = 0
		j = 0
		while j < m - 1:
			if k[i][j] == '.' and k[i][j + 1] == '.':
				if 2 * x < y:
					total += 2 * x
				else:
					total += y
				if j + 1 == m - 1:
					flag = 1
				j += 1
			elif k[i][j] == '.' and k[i][j + 1] != '.':
				total += x
				if j + 1 == m - 1:
					flag = 1
				j += 1
			j += 1
		if k[i][m - 1] == '.' and flag == 0:
			total += x
	print(total)


import sys
inf = float('inf')
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [input() for _ in range(n)]
	ans = 0
	for i in range(n):
		ct = 0
		for j in range(m):
			if a[i][j] == '*':
				if ct % 2 == 0:
					ans += min(ct * x, ct // 2 * y)
				elif ct >= 2:
					ans += min(y * (ct // 2) + x, ct * x)
				else:
					ans += ct * x
				ct = 0
			else:
				ct += 1
		if ct != 0:
			if ct % 2 == 0:
				ans += min(ct * x, ct // 2 * y)
			elif ct >= 2:
				ans += min(y * (ct // 2) + x, ct * x)
			else:
				ans += ct * x
	print(ans)


import collections

def solve(n, m, x, y, grid):
	ans = 0
	for i in range(n):
		dp = [0 for _ in range(m + 1)]
		lastVal = 0
		for j in range(m):
			if grid[i][j] == '.':
				if j > 0 and grid[i][j - 1] == '.':
					dp[j + 1] = min(y + dp[j + 1 - 2], x + dp[j])
				else:
					dp[j + 1] = x + dp[j]
			else:
				dp[j + 1] = dp[j]
		ans += dp[-1]
	return ans
t = int(input().strip())
for i in range(t):
	[n, m, x, y] = list(map(int, input().strip().split()))
	grid = []
	for _ in range(n):
		s = input().strip()
		grid.append([ch for ch in s])
	print(solve(n, m, x, y, grid))


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	l = []
	for qqq in range(n):
		l = input()
		arr.append(l)
	(a, b, c, p, q) = (0, 0, 0, -10, 0)
	for i in range(n):
		p = -10
		for j in range(m):
			if arr[i][j] == '.':
				if p + 1 == j:
					a = 1
					if 2 * x <= y:
						q += x
					else:
						q = q - x
						q += y
				else:
					q += x
					a = 0
				if a == 1:
					p = -10
				else:
					p = j
	print(q)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if x * 2 < y:
		y = x * 2
	ans = 0
	for __ in range(n):
		a = list(input())
		j = 0
		while j < m:
			if a[j] == '.':
				if j + 1 < m and a[j + 1] == '.':
					ans += y
					j += 2
				else:
					ans += x
					j += 2
			else:
				j += 1
	print(ans)


import sys
import heapq

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, sys.stdin.readline().split())
		theatre = []
		for i in range(n):
			theatre.append(sys.stdin.readline().strip())
		if 2 * x <= y:
			ans = 0
			for i in range(n):
				for j in range(m):
					if theatre[i][j] == '.':
						ans += 1
			print(ans * x)
		else:
			table = []
			twoCnt = 0
			oneCnt = 0
			for i in range(n):
				table.append([])
				for j in range(m):
					table[-1].append(theatre[i][j])
			for i in range(n):
				for j in range(m):
					if table[i][j] == '.':
						if j < m - 1 and table[i][j + 1] == '.':
							table[i][j] = '*'
							table[i][j + 1] = '*'
							twoCnt += 1
							continue
						table[i][j] = '*'
						oneCnt += 1
			print(oneCnt * x + twoCnt * y)
	return
main()


SUM = []
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	area = []
	NEW_SUM = 0
	for i in range(n):
		area.append(list(input()))
	for q in range(n):
		j = 0
		while j < m:
			if area[q][j] == '.':
				if j + 1 < m:
					if area[q][j + 1] == '.':
						if 2 * x < y:
							NEW_SUM += 2 * x
							j += 1
						else:
							NEW_SUM += y
							j += 1
					else:
						NEW_SUM += x
				else:
					NEW_SUM += x
			j += 1
	print(NEW_SUM)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	g = [input() for _ in range(n)]
	if y >= x * 2:
		print(x * sum((r.count('.') for r in g)))
		continue
	ans = 0
	for r in g:
		twocount = r.count('..')
		ans += y * twocount
		ans += x * (r.count('.') - twocount * 2)
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = input().split()
	m = int(m)
	n = int(n)
	x = int(x)
	y = int(y)
	a = []
	two = 0
	one = 0
	for _ in range(n):
		a = input()
		prev = 0
		for i in range(len(a) - 1):
			if prev == 1:
				prev = 0
				continue
			if a[i] == '.' and a[i + 1] == '.':
				two += 1
				prev = 1
			elif a[i] == '.':
				one += 1
		if a[-1] == '.' and prev == 0:
			one += 1
	if two * y + one * x < (two * 2 + one) * x:
		price = two * y + one * x
	else:
		price = (two * 2 + one) * x
	print(price)


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, m, x, y) = get_ints()
	p = []
	for i in range(n):
		s = input()
		p.append(s)
	val = 0
	f = 1
	for i in range(n):
		c = 0
		for j in p[i]:
			if j == '.':
				c = c + 1
				f = 0
			elif c != 0:
				if c * x < c // 2 * y + c % 2 * x:
					val += c * x
				else:
					val += c // 2 * y + c % 2 * x
				c = 0
				f = 1
		if f == 0:
			if c != 0:
				if c * x < c // 2 * y + c % 2 * x:
					val += c * x
				else:
					val += c // 2 * y + c % 2 * x
				c = 0
	print(val)


def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for i in range(inin()):
	(n, m, x, y) = cin()
	su = 0
	for i in range(n):
		s = sin().rstrip()
		c = s.count('..')
		c1 = s.count('.')
		if c == 0:
			su += c1 * x
		else:
			su += min(c * y + (c1 - 2 * c) * x, c1 * x)
	print(su)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	c1 = x
	if 2 * x < y:
		c2 = 2 * x
	else:
		c2 = y
	ans = 0
	for i in range(n):
		l = list(input())
		co = 0
		for i in range(len(l)):
			if l[i] == '.':
				co += 1
			if l[i] == '*':
				if co % 2 == 0:
					ans += c2 * (co // 2)
				else:
					ans += c2 * (co // 2) + c1 * (co % 2)
				co = 0
			elif i == m - 1:
				if co % 2 == 0:
					ans += c2 * (co // 2)
				else:
					ans += c2 * (co // 2) + c1 * (co % 2)
				co = 0
	print(ans)


import sys
import math
from collections import defaultdict, deque
import heapq
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, sys.stdin.readline().split())
	grid = []
	cost = 0
	for i in range(n):
		l = list(sys.stdin.readline()[:-1])
		j = 0
		while j < m:
			if l[j] == '.' and j + 1 < m and (l[j + 1] == '.'):
				if y < 2 * x:
					cost += y
					j += 2
				else:
					cost += x
					j += 1
				continue
			if l[j] == '.':
				cost += x
				j += 1
				continue
			j += 1
	print(cost)


def min_row(row, x, y):
	i = 0
	non_cnt = 0
	cnt = 0
	while i < len(row):
		if i < len(row) - 1 and row[i] == row[i + 1] == '.':
			cnt += 1
			i += 1
		elif row[i] == '.':
			non_cnt += 1
		i += 1
	return x * non_cnt + cnt * min(2 * x, y)
for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split(' ')))
	cost = 0
	for i in range(n):
		cost += min_row(list(input()), x, y)
	print(cost)


import math, sys, bisect
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])
for _ in range(int(input())):
	(n, m, x, y) = ilele()
	A = []
	for i in range(n):
		s = input()
		A.append(s)
	Ans = 0
	if y > 2 * x:
		for i in A:
			Ans += i.count('.') * x
	else:
		for i in A:
			B = [list(k) for (j, k) in groupby(i)]
			for j in B:
				if j[0] == '.':
					Ans += len(j) // 2 * y
					Ans += len(j) % 2 * x
	print(Ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		a = input()
		qq = a.count('..')
		q = a.count('.')
		if 2 * x > y:
			ans += qq * y + (q - 2 * qq) * x
		else:
			ans += q * x
	print(ans)


def twos(a):
	v = 0
	for i in range(len(a)):
		for j in range(len(a[i]) - 1):
			if a[i][j] == '.' and a[i][j + 1] == '.':
				v += 1
				a[i][j] = '*'
				a[i][j + 1] = '*'
	return v

def ones(a):
	v = 0
	for i in range(len(a)):
		for j in range(len(a[i])):
			if a[i][j] == '.':
				v += 1
	return v
n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	z = []
	for g in range(a):
		y = list(input())
		z.append(y)
	if 2 * c > d:
		x = d * twos(z)
		e = c * ones(z)
		print(x + e)
	else:
		e = c * ones(z)
		print(e)


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline().rstrip()
res = ['YES', 'NO']
test_case = ii()
while test_case:
	test_case -= 1
	(n, m, x, y) = mi()
	a = []
	for _ in range(n):
		a.append(lsi())
	if 2 * x <= y:
		s = 0
		for i in a:
			s += i.count('.')
		ans = s * x
	else:
		s = d = 0
		for i in range(n):
			j = 0
			while j < m:
				if a[i][j] == '.':
					if j < m - 1 and a[i][j + 1] == '.':
						d += 1
						j += 1
					else:
						s += 1
				j += 1
		ans = s * x + d * y
	print(ans)


def solve():
	l = list(map(int, input().rstrip().split()))
	tt = min(2 * l[2], l[3])
	ot = l[2]
	cost = 0
	for i in range(l[0]):
		row = str(input())
		row = row.replace('..', 'y')
		row = row.replace('.', 'x')
		cost += row.count('x') * ot + row.count('y') * tt
	print(cost)
test = int(input())
for i in range(test):
	solve()


t = int(input())
while t > 0:
	t -= 1
	s = 0
	(n, m, x, y) = map(int, input().strip().split())
	if x <= y / 2:
		for _ in range(n):
			s += x * list(input()).count('.')
		print(s)
	else:
		for _ in range(n):
			l = list(input())
			i = 0
			while i < m:
				if i + 1 < m and l[i] == '.' and (l[i + 1] == '.'):
					s += y
					i += 2
				else:
					if l[i] == '.':
						s += x
					i += 1
		print(s)


def read_ints():
	return [int(x) for x in input().split()]

def read():
	t = int(input())
	data = []
	for i in range(t):
		(n, m, x, y) = read_ints()
		s = [input() for _ in range(n)]
		data.append((x, y, s))
	return data

def solve(data):
	ret = []
	for (x, y, s) in data:
		y = min(y, x * 2)
		total = 0
		for line in s:
			for tile in line.split('*'):
				l = len(tile)
				total += l // 2 * y + l % 2 * x
		ret.append(total)
	return ret

def print_out(ret):
	for x in ret:
		print(x)
data = read()
ret = solve(data)
print_out(ret)


import math
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	r = 0
	for i in range(n):
		s = input()
		cnt = 0
		for i in range(m):
			if s[i] == '.':
				cnt += 1
			else:
				if cnt % 2 == 0:
					r += min(cnt // 2 * y, cnt * x)
				else:
					r += min(cnt // 2 * y + x, cnt * x)
				cnt = 0
		if cnt % 2 == 0:
			r += min(cnt // 2 * y, cnt * x)
		else:
			r += min(cnt // 2 * y + x, cnt * x)
	print(r)


tc = int(input())
while tc > 0:
	(n, m, x, y) = map(int, input().split())
	y = min(2 * x, y)
	cost = 0
	for _ in range(n):
		count_in_row = 0
		for c in input():
			if c == '.':
				count_in_row += 1
			else:
				(q, r) = divmod(count_in_row, 2)
				cost += count_in_row // 2 * y + count_in_row % 2 * x
				count_in_row = 0
		cost += count_in_row // 2 * y + count_in_row % 2 * x
	print(cost)
	tc -= 1


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	matrix = []
	for i in range(n):
		matrix.append([i for i in input()])
	ans = 0
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == '.':
				if j == m - 1:
					ans += x
					matrix[i][j] = '*'
				elif j + 1 < m:
					if matrix[i][j + 1] == '.':
						ans += min(2 * x, y)
						matrix[i][j] = '*'
						matrix[i][j + 1] = '*'
					else:
						ans += x
						matrix[i][j] = '*'
				else:
					ans += x
					matrix[i][j] = '*'
	print(ans)


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	(n, m, x, y) = mInt()
	ans = 0
	for i in range(n):
		s = input()
		f = s.find('.')
		s = list(s)
		i = f
		while i < m:
			c = 0
			while i < m:
				if s[i] == '.':
					c += 1
					i += 1
				else:
					break
			if c != 0:
				if c % 2 == 1:
					ans += x
					c -= 1
				if c // 2 * y < c * x:
					ans += c // 2 * y
				else:
					ans += c * x
			i += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(r) for r in input().split()]
	grid = []
	for i in range(n):
		grid.append(list(input()))
	white = []
	for i in range(n):
		temp = 0
		for j in range(m):
			if grid[i][j] == '*':
				if temp:
					white.append(temp)
					temp = 0
			else:
				temp += 1
			if j == m - 1 and temp:
				white.append(temp)
	cost = 0
	for i in range(len(white)):
		cost += min(white[i] * x, white[i] // 2 * y + white[i] % 2 * x)
	print(cost)


for _ in range(int(input())):
	(r, c, f1, f2) = map(int, input().split())
	arr = []
	ans = 0
	for i in range(r):
		a = input()
		arr.append(a)
	if 2 * f1 < f2:
		f2 = 2 * f1
	for i in range(r):
		if c > 1:
			j = 0
			while c - 1 > j:
				if arr[i][j] == '.' and arr[i][j + 1] == '.':
					ans += f2
					j += 1
				elif arr[i][j] == '.':
					ans += f1
				j += 1
			if j == c - 1:
				if arr[i][j] == '.':
					ans += f1
		elif arr[i][0] == '.':
			ans += f1
	print(ans)


import math
t = int(input())
for i in range(t):
	price = 0
	(n, m, x, y) = list(map(int, input().split()))
	for j in range(n):
		data = input()
		dot = 0
		combo = 0
		combo_list = []
		for k in range(m):
			if data[k] == '.':
				dot = dot + 1
				combo = combo + 1
			elif k != 0 and data[k] == '*':
				combo_list.append(combo)
				combo = 0
		combo_list.append(combo)
		if 2 * x <= y:
			price = price + x * dot
		else:
			for l in range(len(combo_list)):
				price = price + math.floor(combo_list[l] / 2) * y + combo_list[l] % 2 * x
	print(price)


n_testcase = int(input())
for _ in range(n_testcase):
	(n, m, c11, c12) = map(int, input().split())
	if 2 * c11 <= c12:
		count = 0
		for i in range(n):
			for j in input():
				if j == '.':
					count += 1
		sum = count * c11
	else:
		sum = 0
		for i in range(n):
			count = 0
			row = input()
			for j in range(m):
				if row[j] == '.':
					count += 1
				if row[j] == '*' or j == m - 1:
					sum += count // 2 * c12 + count % 2 * c11
					count = 0
	print(sum)


for u in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	s = 0
	c = 0
	for i in range(n):
		l.append(list(input()))
	for i in range(n):
		c = c + l[i].count('.')
		j = 0
		while j < m:
			if j < m - 1 and l[i][j] == '.' and (l[i][j + 1] == '.'):
				s = s + min(y, 2 * x)
				j = j + 2
			elif j < m - 1 and l[i][j] == '.' and (l[i][j + 1] != '.'):
				s = s + x
				j = j + 1
			elif j == m - 1 and l[i][j] == '.':
				s = s + x
				j = j + 1
			else:
				j = j + 1
	print(s)


def issafe(m, n, matrix):
	if m >= 0 and n >= 0 and (m < len(matrix)) and (n < len(matrix[0])):
		return True
	else:
		return False

def dohai(x, y, matrix):
	if issafe(x, y + 1, matrix) and matrix[x][y + 1] == '.':
		return 4
	else:
		return 0
tc = int(input())
for _ in range(tc):
	(n, m, p1, p2) = list(map(int, input().split()))
	matrix = []
	for i in range(n):
		m = list(input())
		matrix.append(m)
	if p2 >= 2 * p1:
		to_pave = 0
		for i in range(0, len(matrix)):
			for j in range(0, len(matrix[0])):
				if matrix[i][j] == '.':
					to_pave += 1
		print(p1 * to_pave)
	else:
		to_pave_2 = 0
		to_pave_1 = 0
		for i in range(0, len(matrix)):
			for j in range(0, len(matrix[0])):
				if matrix[i][j] == '.':
					hai = dohai(i, j, matrix)
					if hai == 0:
						matrix[i][j] == '*'
						to_pave_1 += 1
					elif hai == 4:
						matrix[i][j] = '*'
						matrix[i][j + 1] = '*'
						to_pave_2 += 1
		print(to_pave_1 * p1 + to_pave_2 * p2)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr1 = list(input().strip())
		arr.append(arr1)
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if arr[i][j] == '.':
				if j + 1 < m and y < 2 * x and (arr[i][j + 1] != '*'):
					ans += y
					j += 1
				else:
					ans += x
			j += 1
			if j > m - 1:
				break
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	li = []
	cntd = 0
	for i in range(n):
		li.append(input())
		li[-1] += 'x'
		cntd += li[-1].count('.')
	if y >= 2 * x:
		print(x * cntd)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if li[i][j] == '.':
					if li[i][j + 1] == '.':
						ans += y
					else:
						ans += x
					j += 2
				else:
					j += 1
		print(ans)


T = int(input())
for t in range(T):
	[n, m, x, y] = list(map(int, input().split()))
	(c1, c2) = (0, 0)
	for i in range(n):
		a = list(input())
		j = 0
		while j < m:
			if a[j] == '.' and j + 1 < m and (a[j + 1] == '.'):
				c2 += 1
				j += 1
			elif a[j] == '.':
				c1 += 1
			j += 1
	if 2 * x < y:
		print((c1 + 2 * c2) * x)
	else:
		print(c1 * x + c2 * y)


t = int(input())
for f in range(t):
	(n, m, x, y) = map(int, input().split())
	true = 0
	if 2 * x <= y:
		true = 1
	count = 0
	if true:
		for i in range(n):
			l = list(input())
			for j in range(m):
				if l[j] == '.':
					count += 1
		print(count * x)
	else:
		count = 0
		for i in range(n):
			l = list(input())
			j = 0
			while j < m:
				if j == m - 1:
					if l[j] == '.':
						count += x
					j += 1
				elif l[j] == '*':
					j += 1
				elif l[j + 1] == '.':
					count += y
					j += 2
				else:
					count += x
					j += 1
		print(count)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for _ in range(n):
		grid.append(input())
	if x * 2 <= y:
		ans = 0
		for i in range(n):
			ans += grid[i].count('.')
		print(ans * x)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m - 1:
				if grid[i][j] == grid[i][j + 1] == '.':
					ans += y
					j += 1
				elif grid[i][j] == '.':
					ans += x
				j += 1
			if j == m - 1 and grid[i][j] == '.':
				ans += x
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	single = 0
	double = 0
	for i in range(n):
		line = input().rstrip()
		place = 0
		while place < m:
			if line[place] == '.' and place + 1 < m and (line[place + 1] == '.'):
				double += 1
				place += 2
			elif line[place] == '.':
				single += 1
				place += 2
			else:
				place += 1
	if x * 2 <= y:
		ans = single * x + double * x * 2
	else:
		ans = single * x + double * y
	print(ans)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	square = [input() for i in range(n)]
	cost = 0
	for row in range(n):
		col = 0
		while col < m:
			if square[row][col] == '.':
				if col < m - 1 and square[row][col + 1] == '.':
					cost += min(y, 2 * x)
					col += 1
				else:
					cost += x
			col += 1
	print(cost)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		l = [int(i) for i in input().split(' ')]
		n = l[0]
		m = l[1]
		x = l[2]
		y = l[3]
		c = min(2 * x, y)
		val = 0
		for i in range(n):
			s = list(input().strip())
			t = 0
			for j in range(m):
				if s[j] == '*':
					val += t // 2 * c + t % 2 * x
					t = 0
				else:
					t += 1
			val += t // 2 * c + t % 2 * x
		print(val)
main()


t = int(input())
for i in range(t):
	(n, m, a, b) = map(int, input().split())
	s = 0
	ar = []
	for j in range(n):
		te = input().strip()
		ar.append(te)
	if 2 * a <= b:
		for j in range(n):
			for k in range(m):
				if ar[j][k] == '.':
					s = s + a
	else:
		f = 0
		for j in range(n):
			for k in range(m):
				if ar[j][k] == '.':
					if f == 0:
						f = 1
					else:
						s = s + b
						f = 0
				else:
					if f == 1:
						s = s + a
					f = 0
			if f == 1:
				s = s + a
				f = 0
	print(s)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for j in range(n):
		arr.append(list(input()))
	if 2 * x <= y:
		count = 0
		for j in arr:
			for k in j:
				if k == '.':
					count += 1
		print(x * count)
	else:
		prices = 0
		for j in range(n):
			k = 0
			while k < m:
				if arr[j][k] == '.':
					if k + 1 < m and arr[j][k + 1] == '.':
						prices += y
						k += 1
					else:
						prices += x
				k += 1
		print(prices)


t = int(input())
for _ in range(0, t):
	(n, m, x, y) = map(int, input().strip().split(' '))
	arr = []
	for i in range(0, n):
		arr.append(str(input()))
	ans = 0
	for i in range(0, n):
		j = 0
		while j < m:
			if j + 1 < m and arr[i][j] == arr[i][j + 1] == '.':
				ans += min(2 * x, y)
				j += 1
			elif arr[i][j] == '.':
				ans += x
			j += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	row = [''] * n
	sum = 0
	for i in range(n):
		row[i] = input()
	if 2 * x < y:
		for i in range(n):
			for j in range(m):
				if row[i][j] == '.':
					sum += x
	else:
		for r in row:
			k = 0
			while k < m:
				if m - k >= 2:
					if r[k] == '.' and r[k + 1] == '.':
						k += 1
						sum += y
					elif r[k] == '.':
						sum += x
				elif r[k] == '.':
					sum += x
				k += 1
	print(sum)


T = int(input())
for _ in range(T):
	(N, M, X, Y) = map(int, input().split())
	ans = 0
	if Y > 2 * X:
		Y = 2 * X
	for _ in range(N):
		line = input()
		i = 0
		while i < M:
			car = line[i]
			if car == '.':
				try:
					if line[i + 1] == '.':
						ans += Y
						i += 1
					else:
						ans += X
				except IndexError:
					ans += X
			i += 1
	print(ans)


import random, math, sys
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right
from collections import Counter
input = sys.stdin.readline

def solution(n, m, x, y, mat):
	if 2 * x <= y:
		c = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					c += x
		return c
	else:
		c = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					if j + 1 < m and mat[i][j + 1] == '.':
						c += y
						mat[i][j + 1] = '*'
					else:
						c += x
		return c

def input_test():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().strip().split(' '))
		mat = []
		for i in range(n):
			li = list(input())
			mat.append(li)
		out = solution(n, m, x, y, mat)
		print(out)

def test():
	pass
input_test()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	still = []
	(count1, count2) = (0, 0)
	for _ in range(n):
		still.append(input())
	for i in still:
		m = i.split('*')
		for j in m:
			if len(j) == 1:
				count1 += 1
			elif len(j) >= 2:
				count2 += len(j) // 2
				count1 += len(j) % 2
	if x * 2 <= y:
		print(x * (count1 + count2 * 2))
	else:
		print(count1 * x + count2 * y)


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	l = []
	c = 0
	cc = 0
	ccc = 0
	ans = 1000000
	for i in range(n):
		l1 = list(str(input()))
		l.append(l1)
	for i in l:
		cc = 0
		if len(i) == 1:
			if i[0] == '.':
				c += 1
		else:
			for j in range(m):
				if i[j] == '.':
					cc += 1
					c += 1
				else:
					count = cc // 2
					count = count * b
					xxx = cc % 2 * a
					ccc += count + xxx
					cc = 0
			if cc != 0:
				count = cc // 2
				count = count * b
				xxx = cc % 2 * a
				ccc += count + xxx
				cc = 0
	if ccc == 0:
		print(c * a)
	else:
		print(min(c * a, ccc))


def main():
	inp = lambda : [int(x) for x in input().split()]
	inn = lambda : int(input())
	t = inn()
	for _ in range(t):
		(n, m, price1, price2) = inp()
		ans = 0
		for __ in range(n):
			line = input()
			if price1 * 2 < price2:
				ans += line.count('.') * price1
			else:
				i = 0
				while i < m:
					j = i
					while j < m and line[j] == '.':
						j += 1
					if line[i] == '.':
						ans += (j - i) // 2 * price2 + (j - i) % 2 * price1
					i = j + 1
		print(ans)
main()


for t in range(int(input())):
	rcxy = list(map(int, input().split()))
	(r, c, x, y) = (rcxy[0], rcxy[1], rcxy[2], rcxy[3])
	mat = []
	for i in range(r):
		mat.append(input())
	cost = 0
	if 2 * x <= y:
		for i in mat:
			j = 0
			while j < c:
				if i[j] == '.':
					cost += x
				j += 1
	else:
		for i in mat:
			j = 0
			while j < c:
				if i[j] == '.':
					if j + 1 < c and i[j + 1] == '.':
						cost += y
						j += 2
					else:
						cost += x
						j += 1
				else:
					j += 1
	print(cost)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	t = I()
	rr = []
	for _ in range(t):
		(n, m, x, y) = LI()
		a = [S() for _ in range(n)]
		c = [0] * 2
		for i in range(n):
			t = 0
			ai = a[i]
			for j in range(m):
				if ai[j] == '.':
					t += 1
				else:
					c[1] += t // 2
					c[0] += t % 2
					t = 0
			c[1] += t // 2
			c[0] += t % 2
		if x * 2 > y:
			rr.append(c[1] * y + c[0] * x)
		else:
			rr.append((c[1] * 2 + c[0]) * x)
	return JA(rr, '\n')
print(main())


t = int(input())
for i in range(0, t):
	(n, m, x, y) = [int(i) for i in input().split()]
	po = 0
	pt = 0
	for j in range(0, n):
		r = input()
		wt = r.count('..')
		w = r.count('.')
		wo = w - wt * 2
		po += wt * y + wo * x
		pt += w * x
	print(min(po, pt))


import sys, math, os
from io import BytesIO, IOBase
from bisect import bisect_left as bl, bisect_right as br, insort
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1000000000.0) + 7

def main():
	for t in range(int(data())):
		(n, m, x, y) = mdata()
		mat = []
		cnt = 0
		for i in range(n):
			mat.append(data())
			for j in range(m):
				if mat[i][j] == '.':
					cnt += 1
		if x < y / 2:
			out(cnt * x)
		else:
			cnt1 = 0
			for i in range(n):
				j = 0
				while j < m - 1:
					if mat[i][j] == '.' and mat[i][j + 1] == '.':
						cnt1 += 1
						j += 1
					j += 1
			out(y * cnt1 + x * (cnt - cnt1 * 2))
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [] * n
	f = 2 * x <= y
	for i in range(n):
		a.append(list(input().strip()))
	s = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if f:
					s += x
				elif j < m - 1:
					if a[i][j + 1] == '.':
						s += y
						a[i][j + 1] = '*'
					else:
						s += x
				else:
					s += x
	print(s)


for i in range(int(input())):
	(L, A, x, y) = list(map(int, input().split()))
	ans = 0
	for i in range(L):
		lineas = input()
		cont = lineas.count('.') - lineas.count('..') * 2
		ans += min(x * 2, y) * lineas.count('..')
		ans += x * cont
	print(ans)


counter = input()
total_prices_array = []
for m in range(int(counter)):
	rows_array = []
	total_price = 0
	(number_of_rows, number_of_symbols, singles_price, doubles_price) = map(int, input().split())
	for i in range(number_of_rows):
		row = list(input())
		white_spaces_in_row = []
		white_spaces_counter = 0
		for i in range(len(row)):
			if row[i] == '.':
				white_spaces_counter += 1
			else:
				white_spaces_in_row.append(white_spaces_counter)
				white_spaces_counter = 0
		white_spaces_in_row.append(white_spaces_counter)
		rows_array.append(white_spaces_in_row)
	for i in range(len(rows_array)):
		doubles = 0
		singles = 0
		if doubles_price < singles_price * 2:
			for j in rows_array[i]:
				doubles += j // 2
				singles += j % 2
		else:
			for j in rows_array[i]:
				singles += j
		total_price += singles * singles_price + doubles * doubles_price
	total_prices_array.append(total_price)
for i in total_prices_array:
	print(i)


t = int(input())
for _ in range(t):
	(row, col, smol, big) = map(int, input().split())
	tot = 0
	for r in range(row):
		dps = input()
		if big >= 2 * smol:
			for i in dps:
				if i == '.':
					tot += smol
		else:
			i = 0
			while i < col:
				if dps[i] == '.':
					if i < col - 1 and dps[i + 1] == '.':
						tot += big
						i += 2
					else:
						tot += smol
						i += 2
				else:
					i += 1
	print(tot)


def rs():
	return input().strip()

def ri():
	return int(input())

def ria():
	return list(map(int, input().split()))

def ia_to_s(a):
	return ' '.join([str(s) for s in a])

def solve(n, m, x, y, a):
	c2 = min(y, 2 * x)
	cnt = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == 0:
				k = j
				while k < m and a[i][k] == 0:
					a[i][k] = 1
					k += 1
				r = k - j
				cnt += c2 * (r // 2) + x * (r % 2)
				j = k
	return cnt

def main():
	for _ in range(ri()):
		(n, m, x, y) = ria()
		a = []
		for __ in range(n):
			a.append([1 if x == '*' else 0 for x in rs()])
		print(solve(n, m, x, y, a))
main()


import math
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		cnt = 0
		for i in range(n):
			s = input()
			for j in range(len(s)):
				if s[j] == '.':
					cnt += 1
		print(cnt * x)
	else:
		cnt = 0
		cnt1 = 0
		for i in range(n):
			s = input()
			j = 0
			while j <= len(s) - 1:
				if j <= len(s) - 2:
					if s[j] == '.' and s[j + 1] == '.':
						cnt1 += 1
						j += 2
					elif s[j] == '.':
						cnt += 1
						j += 1
					else:
						j += 1
				elif s[j] == '.':
					cnt += 1
					j += 1
				else:
					j += 1
		print(cnt * x + cnt1 * y)


def mapit():
	temp = list(map(int, input().split()))
	return temp
import bisect

def solution():
	(n, m, x, y) = mapit()
	arr = []
	for i in range(n):
		arr.append(input())
	i = 0
	j = 0
	ans = 0
	while i < n:
		j = 0
		while j < m:
			if arr[i][j] == '.':
				if j < m - 1 and arr[i][j + 1] == '.':
					j += 2
					ans = ans + min(2 * x, y)
				else:
					ans = ans + x
					j += 1
			else:
				j += 1
		i += 1
	print(ans)
	return
t = int(input())
while t:
	t -= 1
	solution()


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
t = Int()
for _ in range(t):
	(n, m, x, y) = value()
	a = []
	for i in range(n):
		a.append(list(input()))
	if y > 2 * x:
		y = 2 * x
	cost = 0
	for row in a:
		i = 0
		while i < m:
			if row[i] == '.':
				c = 1
				i += 1
				while i < m and row[i] == '.':
					i += 1
					c += 1
				cost += y * (c // 2) + x * (c % 2)
			else:
				i += 1
	print(cost)


import sys

def theatheSquare(matrix, tile, twoTiles):
	minPrice = 0
	if tile * 2 < twoTiles:
		for row in range(len(matrix)):
			for col in range(len(matrix[0])):
				if matrix[row][col] == '.':
					minPrice += tile
	else:
		for row in range(len(matrix)):
			left = 0
			while left < len(matrix[row]):
				if matrix[row][left] == '.':
					if left + 1 < len(matrix[row]) and matrix[row][left + 1] == '.':
						minPrice += twoTiles
						left += 2
					else:
						minPrice += tile
						left += 2
				else:
					left += 1
	return minPrice
casos = sys.stdin.readline()
for i in range(int(casos)):
	line = sys.stdin.readline().split(' ')
	mat = []
	for j in range(int(line[0])):
		lineMat = sys.stdin.readline()
		lineMat = lineMat.rstrip('\n')
		mat.append(lineMat)
	price = theatheSquare(mat, int(line[2]), int(line[3]))
	sys.stdout.write(str(price) + '\n')


a = int(input())
for i in range(a):
	(n, m, x, y) = list(map(int, input().split()))
	price_for_two = min(2 * x, y)
	total = 0
	for j in range(n):
		stroka = input()
		prev = ''
		sum_stroka = 0
		for s in stroka:
			if prev == '.' and s == '.':
				sum_stroka += price_for_two - x
				prev = ''
				continue
			elif s == '.':
				sum_stroka += x
			prev = s
		total += sum_stroka
	print(total)


def solution():
	(n, m, x, y) = list(map(int, input().split()))
	if 2 * x < y:
		y = 2 * x
	count = 0
	for i in range(n):
		row = input()
		skip = False
		for j in range(m):
			if skip:
				skip = False
				continue
			cell = row[j]
			if j == m - 1:
				if cell == '.':
					count += x
				continue
			nextCell = row[j + 1]
			if cell == '.' and nextCell == '.':
				count += y
				skip = True
			elif cell == '.':
				count += x
	print(count)
T = int(input())
while T:
	solution()
	T -= 1


import sys
import math
cases = int(input())
for i in range(cases):
	(n, c, t1, t2) = map(int, input().split())
	if 2 * t1 <= t2:
		t2 = 2 * t1
	price = 0
	for i2 in range(n):
		s = input()
		i = 0
		while i < len(s):
			if i + 1 < len(s) and s[i] == s[i + 1]:
				if s[i] == '.':
					price += t2
				i = i + 2
			elif s[i] == '.':
				price += t1
				i += 1
			else:
				i += 1
	print(price)


y = int(input())
money = [0] * y
for i in range(y):
	money[i] = 0
d = 0
for i in range(y):
	q = input()
	a = [int(x) for x in q.split(' ')]
	one = 0
	two = 0
	for n in range(a[0]):
		z = input()
		b = list(z)
		for m in range(a[1]):
			if '.' in b[m] and m == a[1] - 1:
				d = d + 1
				two = d // 2 + two
				one = d % 2 + one
				d = 0
			elif '.' in b[m]:
				d = d + 1
			else:
				two = d // 2 + two
				one = d % 2 + one
				d = 0
		if a[3] > 2 * a[2]:
			money[i] = (2 * two + one) * a[2]
		else:
			money[i] = two * a[3] + one * a[2]
for i in range(y):
	print(money[i])


for _ in range(0, int(input())):
	(n, m, x, y) = map(int, input().split())
	rows = []
	for i in range(0, n):
		rows.append(input())
	if 2 * x <= y:
		counter = 0
		for i in rows:
			for j in i:
				if j == '.':
					counter += 1
		print(x * counter)
	else:
		cost = 0
		counter = 0
		for i in range(0, n):
			for j in range(0, m):
				if rows[i][j] == '.':
					counter += 1
					if j == m - 1:
						if counter % 2 == 0:
							cost += counter / 2 * y
						else:
							cost += counter // 2 * y + x
						counter = 0
				else:
					if counter % 2 == 0:
						cost += counter / 2 * y
					else:
						cost += counter // 2 * y + x
					counter = 0
		print(round(cost))


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	ans = 0
	if 2 * a < b:
		b = 2 * a
	i = 0
	while i < n:
		l = list(input())
		j = 0
		while j < m:
			if l[j] == '.':
				if j < m - 1 and l[j + 1] == '.':
					j += 1
					ans += b
				else:
					ans += a
			j += 1
		i += 1
	print(ans)


outputs = []
for __ in range(int(input())):
	(n, m, x, y) = [int(num) for num in input().split()]
	u = 0
	for i in range(n):
		input1 = input()
		countp = input1.count('..')
		counts = input1.count('.')
		cost = min(countp * y + (counts - countp * 2) * x, counts * x)
		u += cost
	outputs.append(u)
for output in outputs:
	print(output)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().rstrip().split())
	(a, c) = ([], 0)
	for i in range(n):
		g = list(input())
		c += g.count('.')
		a.append(g)
	if 2 * x <= y:
		print(c * x)
		continue
	(i, ans) = (0, 0)
	while i < n:
		j = 0
		while j < m:
			if j != m - 1 and a[i][j] == '.' and (a[i][j + 1] == '.'):
				ans += y
				j += 2
			elif a[i][j] == '.':
				ans += x
				j += 1
			else:
				j += 1
		i += 1
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def solution(n, m, x, y, square):
	cost = 0
	for i in range(n):
		for j in range(m):
			if j < m - 1 and square[i][j] == '.' and (square[i][j + 1] == '.'):
				cost += min(2 * x, y)
				square[i][j] = '*'
				square[i][j + 1] = '*'
			elif square[i][j] == '.':
				cost += x
	write(cost)

def main():
	for _ in range(r_int()):
		(n, m, x, y) = r_array()
		square = []
		for _ in range(n):
			square.append([x for x in input()])
		solution(n, m, x, y, square)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


def solve():
	(n, m, x, y) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append(input())
	ans = 0
	if x * 2 <= y:
		for s in a:
			ans += s.count('.') * x
	else:
		for s in a:
			n2 = s.count('..')
			n1 = s.count('.') - n2 * 2
			ans += x * n1 + y * n2
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	tiles = []
	for i in range(n):
		tiles.append(input())
	ans = 0
	for i in range(n):
		f = False
		for j in range(m):
			if tiles[i][j] == '*':
				continue
			if f:
				f = False
				continue
			if j != m - 1 and tiles[i][j] == tiles[i][j + 1] == '.':
				ans += min(2 * x, y)
				f = True
			else:
				ans += x
	print(ans)


n = int(input())
s = []
for i in range(n):
	m = list(map(int, input().split()))
	a = []
	for i in range(m[0]):
		b = input()
		d = []
		for j in b:
			d.append(j)
		a.append(d)
	f = []
	if m[2] * 2 <= m[3]:
		k = 0
		for i in a:
			k += i.count('.')
		s.append(k * m[2])
	else:
		k = 0
		p = 0
		for j in a:
			count = 0
			for d in j:
				if d == '.':
					count += 1
					p += 1
				else:
					count = 0
				if count == 2:
					k += 1
					count = 0
					p -= 2
		s.append(k * m[3] + p * m[2])
for i in s:
	print(i)


def solv():
	(x, y, on, tw) = map(int, input().split())
	ans = 0
	for n in range(x):
		s = input()
		tot = 0
		for n in s:
			if n == '.':
				tot += 1
			else:
				a = tot * on
				b = tot // 2 * tw + tot % 2 * on
				ans += min(a, b)
				tot = 0
		a = tot * on
		b = tot // 2 * tw + tot % 2 * on
		ans += min(a, b)
	print(ans)
for n in range(int(input())):
	solv()


cases = int(input())
for i in range(cases):
	line1 = input().split(' ')
	n = int(line1[0])
	m = int(line1[1])
	price1 = int(line1[2])
	price2 = int(line1[3])
	if price1 * 2 < price2:
		neveruseprice2 = True
	else:
		neveruseprice2 = False
	total = 0
	for j in range(n):
		row = list(input())
		if neveruseprice2:
			white = row.count('.')
			total += white * price1
		else:
			for k in range(len(row)):
				tile = row[k]
				if tile == '*':
					continue
				elif k != len(row) - 1:
					tile_next = row[k + 1]
					if tile == tile_next:
						row[k] = row[k + 1] = '*'
						total += price2
					elif tile != tile_next:
						total += price1
						row[k] = '*'
				elif k == len(row) - 1:
					total += price1
	print(total)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for _ in range(n):
		s = str(input())
		single = s.count('.')
		multiple = s.count('..')
		if 2 * x <= y or multiple == 0:
			cost = cost + x * single
		else:
			cost = cost + y * multiple + x * (single - 2 * multiple)
	print(cost)


I = input
t = I()
for _ in range(int(t)):
	arr = []
	(n, m, x, y) = list(map(int, I().split()))
	ans = 0
	for i in range(n):
		inp = I()
		d = inp.count('..')
		s = inp.count('.') - 2 * d
		if 2 * x < y:
			ans = ans + x * s + x * (d * 2)
		else:
			ans = ans + x * s + y * d
	print(ans)


for _ in range(int(input())):
	cost = 0
	v = False
	(n, m, x, y) = map(int, input().split())
	if x * 2 < y:
		y = 0
	for i in range(n):
		str = input()
		for i in range(m):
			if str[i] == '.':
				cost += x
				if i != 0 and y != 0 and (v == True):
					cost = cost - 2 * x + y
					v = False
				else:
					v = True
				continue
			else:
				v = False
	print(cost)


testcases = int(input())
for testcase in range(testcases):
	(v, h, p1, p2) = [int(i) for i in input().split()]
	r = 0
	for v in range(v):
		tiles = input()
		double_dot = tiles.count('..')
		single_dot = tiles.count('.')
		r += min(single_dot * p1, double_dot * p2 + (single_dot - double_dot * 2) * p1)
	print(r)


def solve(n, m, x, y, mp):
	sol = 0
	for r in range(n):
		c = 0
		while c < m:
			if mp[r][c] == '.':
				if c + 1 < m and mp[r][c + 1] == '.':
					sol += min(2 * x, y)
					c += 1
				else:
					sol += x
			c += 1
	return sol
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	mp = []
	for _ in range(n):
		mp.append(input())
	print(solve(n, m, x, y, mp))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [input() for _ in range(n)]

	def sVal(s: str) -> int:
		(cnt, i) = (0, 0)
		while i < m:
			if s[i] == '.':
				cnt += x if i + 1 == m or s[i + 1] == '*' else y
				i += 1
			i += 1
		return cnt
	print(sum(map((lambda s: x * s.count('.')) if x * 2 <= y else sVal, a)))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	t = []
	for z in range(n):
		t.append(input())
	c = 0
	if y < 2 * x:
		for j in t:
			c += y * j.count('..')
			j = j.replace('..', '**')
			c += x * j.count('.')
		print(c)
	else:
		for j in t:
			c += x * j.count('.')
		print(c)


for ii in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	r = 0
	for i in range(n):
		a.append(list(input()))
	for i in range(n):
		for j in range(m):
			try:
				if a[i][j] == a[i][j + 1] == '.' and y < 2 * x:
					r += y
					a[i][j] = a[i][j + 1] = '*'
			except:
				pass
			if a[i][j] == '.':
				r += x
				a[i][j] = '*'
	print(r)


t = int(input(''))
while t > 0:
	(n, m, x, y) = map(int, input().split())
	major_list = []
	doubles_count = 0
	singles_count = 0
	for i in range(n):
		l = list(input())
		major_list.append(l)
	for row in major_list:
		covered_index = []
		for checker in range(m):
			if row[checker] == '.':
				singles_count += 1
				if checker != m - 1 and row[checker + 1] == '.':
					if checker not in covered_index:
						doubles_count += 1
						covered_index.append(checker)
						covered_index.append(checker + 1)
	ans1 = x * singles_count
	ans2 = y * doubles_count + x * (singles_count - 2 * doubles_count)
	print(min(ans1, ans2))
	t -= 1


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(input())
	c = 0
	for i in range(n):
		j = 0
		while j < m:
			if l[i][j] == '.':
				if j < m - 1:
					if l[i][j + 1] == '.':
						if 2 * x > y:
							c = c + y
						else:
							c = c + 2 * x
						j = j + 2
					else:
						c = c + x
						j = j + 2
				else:
					c = c + x
					j = m
					break
			else:
				j = j + 1
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	a = []
	for i in range(n):
		s = input()
		tmp = []
		for i in s:
			tmp.append(i)
		a.append(tmp)
	if m == 1:
		total_x = 0
		for i in range(n):
			if a[i][0] == '.':
				total_x += 1
		print(total_x * x)
		continue
	total_x = 0
	total_y = 0
	if 2 * x <= y:
		y = 2 * x
	for i in range(n):
		j = 0
		while j < m - 1:
			if a[i][j] == '*':
				j += 1
				continue
			else:
				if a[i][j + 1] == '.':
					total_y += 1
				else:
					total_x += 1
				j += 2
		if j == m - 1:
			if a[i][j] == '.':
				total_x += 1
	print(total_x * x + total_y * y)


for _ in range(1, int(input()) + 1):
	(n, m, x, y) = map(int, input().split())
	s = 0
	s2 = 0
	if y >= x * 2:
		for i in range(n):
			ch = list(input())
			s += ch.count('.')
		print(s * x)
	else:
		for i in range(n):
			ch = list(input())
			if len(ch) == 1:
				s += ch[0] == '.'
			else:
				if ch[0] == '.' and ch[1] == '.':
					s2 += 1
					ch[0] = '*'
					ch[1] = '*'
				elif ch[0] == '.' and ch[1] == '*':
					s += 1
					ch[0] = '*'
				j = 1
				while j <= len(ch) - 2:
					if ch[j] == '.' and ch[j + 1] == '.':
						s2 += 1
						ch[j] = '*'
						ch[j + 1] = '*'
						j += 2
					elif ch[j] == '.' and ch[j + 1] == '*':
						s += 1
						ch[j] = '*'
						j += 2
					else:
						j += 1
				if ch[-1] == '.':
					s += 1
		print(x * s + y * s2)


def solve():
	(n, m, x, y) = map(int, input().split())
	a = [input() for _ in range(n)]
	ans = 0
	if y < x * 2:
		for ai in a:
			(*l,) = ai.split('*')
			for li in l:
				ans += y * (len(li) // 2)
				ans += x * (len(li) % 2)
	else:
		for ai in a:
			(*l,) = ai.split('*')
			for li in l:
				ans += len(li) * x
	print(ans)
t = int(input())
for _ in range(t):
	solve()


from queue import LifoQueue

def print_sol(c, sol):
	print('Case #{}: {}'.format(c, sol))

def rd_int():
	return int(input())

def rd_str():
	return input()

def rd_str_lst():
	return input().split(' ')

def sp_str_lst():
	return list(input())

def rd_int_lst():
	return [int(i) for i in rd_str_lst()]

def sp_int_lst():
	return [int(i) for i in sp_str_lst()]

def dfs(g, i, v):
	v[i] = True
	for j in g[i]:
		if not v[j]:
			dfs(g, j, v)
	print(i)

def dfs_iter(g, i, n):
	v = [False] * n
	stack = LifoQueue()
	stack.put(i)
	while stack.not_empty:
		u = stack.get()
		v[u] = True
		print(u)
		for i in g[u]:
			if not v[i]:
				stack.put(i)

def main():
	t = rd_int()
	for c in range(1, t + 1):
		(n, m, x, y) = rd_int_lst()
		ts = [[0] * (m + 1) for _ in range(n)]
		for i in range(n):
			tc = rd_str()
			for j in range(m):
				if tc[j] == '.':
					ts[i][j] = 1
		y = min(y, 2 * x)
		sol = 0
		for i in range(n):
			j = 0
			while j < m:
				if ts[i][j]:
					if ts[i][j + 1]:
						sol += y
						j += 1
					else:
						sol += x
				j += 1
		print(sol)
main()


def find_min(n, m, x, y, s):
	if 2 * x <= y:
		y = 2 * x
	price = 0
	for i in range(n):
		j = 0
		while j <= m - 1:
			if s[i][j] == '.':
				if j != m - 1 and s[i][j + 1] == '.':
					price += y
					j += 2
				else:
					price += x
					j += 1
			else:
				j += 1
	return price
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = [input() for i in range(n)]
	print(find_min(n, m, x, y, s))


t = int(input())
for i in range(t):
	s = input().split()
	n = int(s[0])
	m = int(s[1])
	x = int(s[2])
	y = int(s[3])
	l = []
	for j in range(n):
		s = input()
		l.append(s)
	cd = 0
	cp = 0
	for j in range(n):
		cd += l[j].count('.')
		cp += l[j].count('..')
	if 2 * x <= y:
		print(cd * x)
	else:
		d = cd - 2 * cp
		c = x * d + y * cp
		print(c)


for testcase in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = 0
	b = 0
	lt = []
	for h in range(n):
		for t in list(input()):
			lt.append(t)
		lt.append('a')
	i = 0
	while i < len(lt):
		if lt[i] == '.' and lt[i + 1] == '.':
			b += 1
			i += 2
		elif lt[i] == '.':
			a += 1
			i += 1
		else:
			i += 1
	if 2 * x <= y:
		print((a + 2 * b) * x)
	else:
		print(a * x + b * y)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for k in range(n):
		s = input()
		c = 0
		for i in range(m):
			if s[i] == '*':
				if c % 2 == 0:
					ans += min(x * c, y * (c // 2))
				else:
					ans += x + min(x * (c - 1), y * ((c - 1) // 2))
				c = 0
			else:
				c += 1
		if c % 2 == 0:
			ans += min(x * c, y * (c // 2))
		else:
			ans += x + min(x * (c - 1), y * ((c - 1) // 2))
	print(ans)


for ttt in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	on = x * 2 <= y
	if on:
		xx = 0
		for i in l:
			for j in i:
				xx += j == '.'
		print(xx * x)
	else:
		xx = 0
		for i in l:
			bb = 0
			for j in i:
				if j == '.':
					if bb == 2:
						xx += y
						bb = 0
					bb += 1
				else:
					if bb == 2:
						xx += y
						bb = 0
					if bb == 1:
						xx += x
						bb = 0
			if bb == 1:
				xx += x
			if bb == 2:
				xx += y
		print(xx)


def analyze_line(row):
	temp_cont_white = []
	for i in row.split('*'):
		temp_cont_white.append(len(i))
	temp_cont_white = list([i for i in temp_cont_white if i != 0])
	return temp_cont_white

def func():
	(n, m, x, y) = map(int, input().split())
	cont_white = []
	matrix = []
	for i in range(n):
		line = input() + '*'
		matrix.append(line)
	for i in range(n):
		cont_white += analyze_line(matrix[i])
	cost_1 = x
	cost_2 = min(2 * x, y)
	total_cost = 0
	for i in cont_white:
		total_cost += i // 2 * cost_2 + i % 2 * cost_1
	print(total_cost)
t = int(input())
for _ in range(t):
	func()


def calculate(arr, s, d):
	cost = 0
	for (i, val) in enumerate(arr):
		if val == '*':
			continue
		if i + 1 < len(arr) and d < 2 * s and (val + arr[i + 1] == '..'):
			cost += d
			arr[i + 1] = '*'
			continue
		cost += s
	return cost
t = int(input())
for _ in range(t):
	(n, m, single, double) = map(int, input().split())
	res = 0
	for _ in range(n):
		res += calculate(list(input()), single, double)
	print(res)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	if 2 * x < y:
		y = 2 * x
	c = 0
	f = 0
	for i in range(n):
		for j in range(m):
			if l[i][j] == '*':
				continue
			if f:
				f = 0
				continue
			if j + 1 < m:
				if l[i][j + 1] == '.':
					c += y
					f = 1
					continue
				else:
					c += x
					continue
			else:
				c += x
	print(c)


a = int(input())
for b in range(a):
	h = 0
	(c, d, e, f) = map(int, input().split())
	for i in range(c):
		g = input().split('*')
		for j in g:
			l = len(j)
			h += l * e if l == 1 else min(l * e, l // 2 * f + l % 2 * e)
	print(h)


for i in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	if a * 2 < b:
		x = a * 2
	else:
		x = b
	i = 0
	c = 0
	p = []
	while i < n:
		s = input()
		j = 0
		s1 = ''
		while j < m:
			if s[j] == '.':
				if len(s1) == 2:
					p.append(s1)
					s1 = ''
				s1 += s[j]
			else:
				p.append(s1)
				s1 = ''
			j += 1
		p.append(s1)
		i += 1
	for i in p:
		if len(i) == 2:
			c += x
		elif len(i) == 1:
			c += a
	print(c)


t = int(input())
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	prev = False
	x_n = 0
	y_n = 0
	for i in range(n):
		prev = False
		for j in range(m):
			if a[i][j] == '*':
				prev = False
			else:
				x_n += 1
				if prev == True:
					prev = False
					x_n -= 2
					y_n += 1
				else:
					prev = True
	print(min(x * x_n + x * 2 * y_n, x * x_n + y * y_n))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input().split('*')
		for z in s:
			ans += min(x * len(z), y * (len(z) // 2) + x * (len(z) & 1))
	print(ans)


testes = int(input())
for i in range(testes):
	(n, m, x, y) = [int(i) for i in input().split()]
	tiles = []
	value = 0
	valueOneTile = 0
	for i in range(n):
		tiles.append(input())
		totalDoubleTiles = tiles[i].count('..')
		totalSingleTiles = tiles[i].count('.')
		value += totalDoubleTiles * y + (totalSingleTiles - totalDoubleTiles * 2) * x
		valueOneTile += totalSingleTiles * x
	print(min(value, valueOneTile))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	bag = []
	ans1 = 0
	trav = [[0 for x in range(m)] for y in range(n)]
	for _ in range(n):
		temp = input()
		t = list(temp)
		bag.append(t)
	for q in range(n):
		for w in range(m):
			if bag[q][w] == '.':
				if trav[q][w] == 0:
					try:
						if bag[q][w + 1] == '.':
							trav[q][w + 1] = 1
							ans1 += y
						else:
							ans1 += x
					except:
						ans1 += x
	ans2 = 0
	for q in range(n):
		for w in range(m):
			if bag[q][w] == '.':
				ans2 += x
	ans = min(ans1, ans2)
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	c = 0
	for i in range(n):
		n = 0
		for j in range(m):
			if a[i][j] == '.':
				n += 1
			elif n != 0:
				c += min(x * n, y * (n // 2) + x * (n % 2))
				n = 0
		if n != 0:
			c += min(x * n, y * (n // 2) + x * (n % 2))
	print(c)


def solve(mat, x, y):
	if y >= 2 * x:
		res = 0
		for i in mat:
			res += i.count('.') * x
		return res
	else:
		res = 0
		for i in mat:
			res += i.count('..') * y + (i.count('.') - 2 * i.count('..')) * x
		return res
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for _ in range(n):
		mat.append(input())
	print(solve(mat, x, y))


import sys
import math
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, c1, c2) = map(int, input().split())
	pat = min(c1 * 2, c2)
	ans = 0
	stack = 0
	for _ in range(n):
		line = input()
		for char in line:
			if char == '.':
				stack += 1
				if not stack % 2:
					ans += stack // 2 * pat
					stack = 0
			else:
				if not stack % 2:
					ans += stack // 2 * pat
				else:
					ans += c1
				stack = 0
	if not stack % 2:
		ans += stack // 2 * pat
	else:
		ans += c1
	print(ans)


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
dt = {'*': 0, '.': 1}
for _ in range(inp()):
	(n, m, a, b) = ip()
	x = []
	for i in range(n):
		x.append([dt[j] for j in input()])
	two = min(2 * a, b)
	cost = 0
	for row in x:
		if 1 not in row:
			continue
		if 0 not in row:
			cost += two * (m // 2) + a * (m % 2)
			continue
		prev = -1
		ct = 0
		for i in range(m):
			if i == m - 1 and row[i] == 1:
				ct += 1
				cost += two * (ct // 2) + a * (ct % 2)
				continue
			if row[i] == 1:
				ct += 1
			elif row[i] == 0 and prev == 1:
				cost += two * (ct // 2) + a * (ct % 2)
				ct = 0
			prev = row[i]
	print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ones = 0
	twos = 0
	for j in range(n):
		a = input()
		if x * 2 > y:
			two = a.count('..')
			twos += two
			ones += a.count('.') - 2 * two
		else:
			ones += a.count('.')
	print(x * ones + y * twos)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	unicos = 0
	duplos = 0
	unicosTotal = 0
	aux = 0
	for i in range(n):
		s = input()
		duplo = s.count('..')
		unico = s.count('.')
		unicosTotal += unico
		if unico >= duplo:
			unico = unico - duplo * 2
		unicos += unico
		duplos += duplo
	precoUnicoTotal = unicosTotal * x
	precoUnico = unicos * x
	precoDuplo = duplos * y
	if duplos > 0:
		if precoUnicoTotal < precoDuplo + precoUnico:
			print(precoUnicoTotal)
		else:
			print(precoDuplo + precoUnico)
		continue
	else:
		print(precoUnicoTotal)
		continue


for t in range(int(input())):
	(a, b, x, y) = [int(z) for z in input().split()]
	c = 0
	for l in range(a):
		line = input()
		o1 = line.count('.')
		o2 = line.count('..')
		if y < 2 * x:
			c += x * (o1 - 2 * o2) + y * o2
		else:
			c += x * o1
	print(c)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(c) for c in input().split()]
	theat = []
	count = 0
	for i in range(n):
		row = [x for x in input()]
		count += row.count('.')
		theat.append(row)
	if 2 * x <= y:
		print(count * x)
	else:
		ans = 0
		for row in theat:
			i = 0
			while i < len(row):
				if row[i] == '.':
					if i != len(row) - 1 and row[i + 1] == '.':
						ans += y
						i += 2
					else:
						ans += x
						i += 1
				else:
					i += 1
		print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	one = x
	two = min(2 * x, y)
	cost = 0
	for _ in range(n):
		row = input()
		i = 0
		while i < len(row):
			if i == len(row) - 1:
				if row[i] == '.':
					cost += one
			elif row[i] == '.':
				if row[i + 1] == '.':
					cost += two
					i += 1
				else:
					cost += one
					i += 1
			i += 1
	print(cost)


from __future__ import division, print_function
mod = int(1000000000.0 + 7)
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
from collections import Counter
import collections
import math
import heapq
import re

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def most_frequent(list):
	return max(set(list), key=list.count)

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def Convert(string):
	li = list(string.split(''))
	return li

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f[-1]
prime = []
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j

def nextPerfectSquare(N):
	nextN = math.floor(math.sqrt(N)) + 1
	return nextN * nextN

def main():
	for _ in range(inin()):
		(n, m, x, y) = map(int, input().split())
		count = 0
		temp = min(2 * x, y)
		arr = []
		for i in range(n):
			arr.append(list(sin()))
		i = 0
		j = 0
		for i in range(n):
			for j in range(m):
				if j != m - 1 and arr[i][j] == '.':
					if arr[i][j + 1] == '.':
						count += temp
						arr[i][j + 1] = '*'
					else:
						count += x
				if j == m - 1 and arr[i][j] == '.':
					count += x
		print(count)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arrs = [input() for _ in range(n)]
	ans = 0
	for s in arrs:
		l = [len(x) for x in s.split('*') if x]
		for length in l:
			if (y + 1) // 2 > x:
				ans += x * length
			else:
				ans += x * (length % 2)
				ans += y * (length // 2)
	print(ans)


t = int(input())
ans = []
for ti in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	square = [[] for i in range(n)]
	for i in range(n):
		square[i] = input()
	if x <= y / 2:
		count = 0
		for i in range(n):
			for j in square[i]:
				if j == '.':
					count += 1
		ans.append(count * x)
	else:
		count_x = 0
		count_y = 0
		z = 0
		for i in range(n):
			j = 0
			while j < m:
				if square[i][j] == '*':
					count_y += z // 2
					count_x += z % 2
					z = 0
				else:
					z += 1
				j += 1
			count_y += z // 2
			count_x += z % 2
			z = 0
		ans.append(count_x * x + count_y * y)
for a in ans:
	print(a)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ones = 0
	twos = 0
	for i in range(n):
		s = input()
		j = 0
		while j < m:
			if s[j] == '*':
				j += 1
				continue
			if j + 1 < m and s[j + 1] == '.':
				twos += 1
				j += 2
			else:
				ones += 1
				j += 1
	if y > 2 * x:
		print((ones + twos * 2) * x)
	else:
		print(x * ones + y * twos)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
from io import BytesIO
import threading
import bisect
import heapq
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = sys.stdin.readline
ara = [0] * 100005
listOdd = list()

def aratosfen():
	global ara
	for i in range(2, int(math.sqrt(len(ara)))):
		if ara[i] == 0:
			for j in range(i * 2, len(ara), i):
				ara[j] = 1
	global listOdd
	for i in range(2, len(ara)):
		if ara[i] == 0:
			listOdd.append(i)
for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	o2 = min(x * 2, y)
	answer = 0
	for i in range(n):
		str1 = str(input()) + '1'
		j = 0
		while j < len(str1):
			if str1[j] == '.' and str1[j + 1] == '.':
				answer += o2
				j += 2
				continue
			elif str1[j] == '.':
				answer += x
			j += 1
	print(answer)


t = int(input())
while t:
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		k = input()
		a.append(k)
	cn1 = 0
	cn2 = 0
	for i in range(n):
		if '.' in a[i]:
			cn1 += a[i].count('.')
		if '..' in a[i]:
			cn2 += a[i].count('..')
	n1 = cn1 - 2 * cn2
	n2 = cn2
	if x == y:
		print((n1 + n2) * x)
	elif 2 * x < y:
		print(x * cn1)
	else:
		print(n1 * x + n2 * y)
	t -= 1


t = int(input())
for j in range(t):
	(n, m, x, y) = map(int, input().split())
	c_adj = 0
	c_sin = 0
	for i in range(n):
		z = input()
		k = 0
		while k != m:
			if z[k] == '*':
				k += 1
				continue
			elif k == m - 1:
				c_sin += 1
				k += 1
			elif z[k] == z[k + 1]:
				c_adj += 1
				k += 2
			else:
				c_sin += 1
				k += 1
	if y >= 2 * x:
		print((c_sin + 2 * c_adj) * x)
	else:
		print(c_sin * x + c_adj * y)


def solve():
	(n, m, x, y) = [int(x) for x in input().split()]
	li = []
	for i in range(n):
		li.append(input())
	white = 0
	for i in range(n):
		s = li[i]
		for ele in s:
			if ele == '.':
				white += 1
	vl = white * x
	(single, double) = (0, 0)
	for i in range(n):
		s = li[i]
		j = 0
		while j < m:
			if j == m - 1:
				if s[j] == '.':
					single += 1
					j += 1
				else:
					j += 1
			elif s[j] == '.' and s[j + 1] == '.':
				double += 1
				j += 2
			elif s[j] == '.' and s[j + 1] != '.':
				single += 1
				j += 1
			elif s[j] == '*':
				j += 1
	v2 = single * x + double * y
	print(min(vl, v2))
t = int(input())
for i in range(t):
	solve()


def cost(count, x, y):
	(q, r) = divmod(count, 2)
	return q * y + r * x
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	info = list()
	for _ in range(n):
		info.append(input())
	if 2 * x <= y:
		count = 0
		for s in info:
			for char in s:
				if char == '.':
					count += 1
		ans = count * x
	else:
		L = [[0] * m for i in range(n)]
		for i in range(n):
			for j in range(m):
				if info[i][j] == '.':
					if j == 0:
						L[i][j] = 1
					else:
						L[i][j] = L[i][j - 1] + 1
		ans = 0
		for i in range(n):
			for j in range(m - 1):
				if L[i][j] > 0 and L[i][j + 1] == 0:
					ans += cost(L[i][j], x, y)
			if L[i][m - 1] > 0:
				ans += cost(L[i][m - 1], x, y)
	print(ans)


import sys
import os
import heapq
import math
try:
	path = './file/input.txt'
	if os.path.exists(path):
		sys.stdin = open(path, 'r')
except:
	pass
t = int(input())

def printd(value):
	pass
for _ in range(t):
	arr = list(map(int, input().split(' ')))
	(n, m, x, y) = (arr[0], arr[1], arr[2], arr[3])
	if x * 2 < y:
		y = x * 2
	result = 0
	for _ in range(n):
		row = input()
		i = 0
		while i < m:
			if row[i] == '*':
				i += 1
				continue
			elif i < m - 1 and row[i + 1] == '.':
				result += y
				i += 2
			else:
				result += x
				i += 1
	print(result)


import re
T = int(input())
for _ in range(T):
	(n, m, x, y) = list(map(lambda i: int(i), input().split()))
	mtx = []
	cost = 0
	for r in range(n):
		tiles = re.split('\\*+', input())
		for t in tiles:
			dots = len(t)
			if dots == 1:
				cost += x
			else:
				first = dots // 2 * y + dots % 2 * x
				second = dots * x
				cost += min([first, second])
	print(cost)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]
for _ in range(val()):
	(n, m, x, y) = li()
	y = min(y, 2 * x)
	l = []
	for i in range(n):
		l.append(st())
	visited = {}
	ans = 0
	for i in range(n):
		for j in range(m):
			if (i, j) in visited:
				continue
			if l[i][j] == '.' and j < m - 1 and (l[i][j + 1] == '.'):
				ans += y
				visited[i, j + 1] = 1
			elif l[i][j] == '.':
				ans += x
	print(ans)


def main():
	t = int(input())
	for case in getCases(t):
		print(smallestTotalPrice(case))

def getCases(t):
	cases = []
	for i in range(t):
		data = input().split()
		grid = []
		for j in range(int(data[0])):
			grid.append(input())
		cases.append((data, grid))
	return cases

def smallestTotalPrice(case):
	(m, x, y) = list(map(lambda x: int(x), case[0][1:]))
	grid = case[1]
	if x * 2 <= y:
		only1x1 = True
	else:
		only1x1 = False
	return getPaveCost(grid, only1x1, m, x, y)

def getPaveCost(grid, only1x1, m, x, y):
	cost = 0
	for row in grid:
		if only1x1:
			cost += row.count('.') * x
		else:
			row += '*'
			lineOfWhites = 0
			for i in range(m + 1):
				if row[i] == '.':
					lineOfWhites += 1
				else:
					double = lineOfWhites // 2
					single = lineOfWhites % 2
					cost += single * x + double * y
					lineOfWhites = 0
	return cost
main()


t = int(input())
while t:
	(n, m, x, y) = [int(a) for a in input().split()]
	cost = 0
	state = 2 * x <= y
	while n:
		row = input()
		conWhite = row.split('*')
		for string in conWhite:
			l = len(string)
			if state:
				if l > 0:
					cost += l * x
			elif l > 0:
				if l % 2 == 0:
					cost += y * l / 2
				else:
					cost += y * (l - 1) / 2 + x
		n -= 1
	print(int(cost))
	t -= 1


def solve(n, m, x, y, a):
	ap2 = y < 2 * x
	s = 0
	for l in a:
		for i in range(m):
			if l[i]:
				if ap2 and i < m - 1 and l[i + 1]:
					s += y
					l[i + 1] = 0
				else:
					s += x
	return s
t = int(input())
for i_t in range(t):
	[n, m, x, y] = list(map(int, input().split(' ')))
	a = [None] * n
	for i in range(n):
		a[i] = list(map(lambda c: 1 if c == '.' else 0, input()))
	print(solve(n, m, x, y, a))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		a = s.count('.')
		b = s.count('..')
		if y < 2 * x:
			ans += y * b + (a - 2 * b) * x
		else:
			ans += x * a
	print(ans)


import atexit
import io
import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	mi = min(2 * x, y)
	cost = 0
	for j in range(n):
		a = input()
		flag = 0
		for k in range(m - 1):
			if flag == 1:
				flag = 0
				continue
			if a[k] == '.':
				if a[k + 1] == '.':
					flag = 1
					cost += mi
				else:
					cost += x
		if flag == 0:
			if a[-1] == '.':
				cost += x
	print(cost)


from math import *
t = int(input())
while t > 0:
	ip = [int(x) for x in input().split()]
	n = ip[0]
	m = ip[1]
	x = ip[2]
	y = ip[3]
	a = []
	for i in range(n):
		s = input()
		s += 'e'
		a.append(s)
	y = min(2 * x, y)
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == a[i][j + 1] and a[i][j] == '.':
				ans += y
				j += 1
			elif a[i][j] == '.':
				ans += x
			j += 1
	print(ans)
	t -= 1


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	counting = []
	double = []
	matrix = []
	count = 0
	for i in range(n):
		string = input()
		matrix.append(string)
		counting.append(string.count('.'))
		double.append(string.count('..'))
	if y >= 2 * x:
		print(sum(counting) * x)
	else:
		single = sum(counting) - sum(double) * 2
		print(sum(double) * y + single * x)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l2 = []
		for j in input():
			l2.append(j)
		l.append(l2)
	i = 0
	cost = 0
	while i < n:
		j = 0
		while j < m:
			if l[i][j] == '.':
				if 2 * x > y and j + 1 < m and (l[i][j + 1] == '.'):
					j += 2
					cost += y
				else:
					j += 1
					cost += x
			else:
				j += 1
		i += 1
	print(cost)


def main():
	test_cases = int(input())
	for i in range(test_cases):
		case_input = str(input()).split()
		n = int(case_input[0])
		m = int(case_input[1])
		px = int(case_input[2])
		py = int(case_input[3])
		x = 0
		y = 0
		price = 0
		flag = False
		for i in range(n):
			picture = str(input())
			for j in range(m):
				simbolo_atual = picture[j]
				if flag:
					flag = False
					continue
				elif simbolo_atual == '.' and j + 1 < m and (picture[j + 1] == '.'):
					price += min(2 * px, py)
					flag = True
					continue
				elif simbolo_atual == '.':
					price += px
		print(price)
main()


def solve():
	inp = input().split()
	matrix = []
	inp = [int(i) for i in inp]
	(n, m, x, y) = inp
	for _ in range(n):
		col = []
		s = input()
		for i in range(m):
			col.append(s[i])
		matrix.append(col)
	cost = 0
	if y < 2 * x:
		for i in range(n):
			j = 0
			while j < m:
				if matrix[i][j] == '.' and j < m - 1 and (matrix[i][j + 1] == '.'):
					cost += y
					j += 1
				elif j < m and matrix[i][j] == '.':
					cost += x
				j += 1
	else:
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == '.':
					cost += x
	print(cost)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
import math
from sys import stdin, stdout

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def main():
	for t in range(int(input())):
		(n, m, x, y) = get_ints()
		l = []
		e = 0
		for i in range(n):
			s = input()
			l.append(s)
		for i in range(n):
			j = 0
			while j < m:
				if l[i][j] == '*':
					j += 1
					continue
				elif j + 1 < m and l[i][j] == l[i][j + 1]:
					e = e + min(x * 2, y)
					j += 2
				else:
					e = e + x
					j += 1
		print(e)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


INT_MAX = 10 ** 30 + 7
MOD = 10 ** 12 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]
from collections import deque

def se(ele, x, y):
	q = deque()
	w = 0
	for i in range(len(ele)):
		if ele[i] == '.':
			q.append('.')
		else:
			l = len(q)
			if 2 * x <= y:
				w += x * l
			else:
				w += l // 2 * y + l % 2 * x
			q.clear()
	l = len(q)
	if 2 * x <= y:
		w += x * l
	else:
		w += l // 2 * y + l % 2 * x
	q.clear()
	return w

def calc(n, m, x, y, A):
	ans = 0
	for i in range(n):
		ele = A[i]
		ans += se(ele, x, y)
	return ans
for i in range(int(input())):
	(n, m, x, y) = INPUT()
	A = []
	for i in range(n):
		s = list(input())
		A.append(s)
	print(calc(n, m, x, y, A))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(dot2, dot1, a) = (0, 0, 0)
	for i in range(n):
		s = str(input())
		a = 0
		for j in s:
			if j == '.':
				a += 1
			else:
				dot2 += a // 2
				dot1 += a % 2
				a = 0
		dot2 += a // 2
		dot1 += a % 2
	if 2 * x < y:
		print(dot2 * 2 * x + dot1 * x)
	else:
		print(dot1 * x + dot2 * y)


def solve():
	(n, m, x, y) = list(map(int, input().split()))
	mundo = []
	y = min(y, 2 * x)
	price = 0
	for _ in range(n):
		row = input()
		i = 0
		while i < m:
			if row[i] == '.':
				if i + 1 < m and row[i + 1] == '.':
					price += y
					i += 1
				else:
					price += x
			i += 1
	print(price)
t = int(input())
for test in range(t):
	solve()


def solve():
	(n, m, x, y) = [int(x) for x in input().split()]
	s = 0
	if 2 * x <= y:
		for _ in range(n):
			for c in input():
				if c == '.':
					s += x
	else:
		for _ in range(n):
			row = input()
			i = 0
			while i < len(row):
				if row[i] == '.':
					if i < len(row) - 1:
						if row[i + 1] == '.':
							s += y
							i += 2
						else:
							s += x
							i += 1
					else:
						s += x
						i += 1
				else:
					i += 1
	print(s)
t = int(input())
for _ in range(t):
	solve()


WHITE = '.'
BLACK = '*'
t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	theater = [[i for i in input()] for _ in range(n)]
	one = 0
	two = 0
	use_one = x * 2 < y
	for i in range(n):
		for j in range(0, m - 1):
			if theater[i][j] == WHITE and theater[i][j + 1] == WHITE:
				if use_one:
					one += 2
				else:
					two += 1
				theater[i][j] = theater[i][j + 1] = BLACK
		one += theater[i].count(WHITE)
	print(one * x + two * y)


t = int(input())
for _ in range(t):
	(n, m, p1, p2) = [int(x) for x in input().split()]
	cost = 0
	for j in range(n):
		s = list(input().split('*'))
		for i in s:
			if i == '':
				continue
			x = len(i) // 2
			y = len(i) % 2
			if p2 < 2 * p1:
				cost += x * p2 + y * p1
			else:
				cost += len(i) * p1
	print(cost)


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	(n, m, x, y) = Ri()
	s = []
	for i in range(n):
		temp = ri()
		s.append(temp)
	if 2 * x <= y:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.':
					cnt += 1
		print(cnt * x)
	else:
		cnt = 0
		flag = list2d(n, m, False)
		for i in range(n):
			for j in range(m - 1):
				if not flag[i][j]:
					if s[i][j] == '.' and s[i][j + 1] == '.':
						cnt += y
						flag[i][j + 1] = True
						flag[i][j] = True
					elif s[i][j] == '.':
						cnt += x
						flag[i][j] = True
			if not flag[i][m - 1] and s[i][m - 1] == '.':
				cnt += x
				flag[i][m - 1] = True
		print(cnt)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, m, x, y, mat):
	tot = 0
	for i in range(n):
		dot1 = mat[i].count('.')
		dot2 = mat[i].count('..')
		tot += x * (dot1 - dot2 * 2) + min(2 * x, y) * dot2
	print(tot)
for _ in range(int(input())):
	(n, m, x, y) = get_int()
	mat = []
	for _ in range(n):
		mat.append(str(input())[:-1])
	solve(n, m, x, y, mat)


import sys, math
from collections import deque

def solve():
	return
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	pave = []
	for _ in range(n):
		pave.append(list(input()))
	if 2 * x < y:
		count = 0
		for i in range(n):
			count += pave[i].count('.')
		print(count * x)
	else:
		cost = 0
		for i in range(n):
			for j in range(m - 1):
				if pave[i][j] == pave[i][j + 1] and pave[i][j] == '.':
					pave[i][j] = '*'
					pave[i][j + 1] = '*'
					cost += y
		count = 0
		for i in range(n):
			count += pave[i].count('.')
		cost += count * x
		print(cost)


t = int(input())
while t:
	(n, m, x, y) = map(int, input().split())
	layout = []
	for _ in range(n):
		layout.append(input())
	maps = {'11': 0, '12': 0}
	for i in range(n):
		while '..' in layout[i]:
			layout[i] = layout[i].replace('..', '', 1)
			maps['12'] = maps['12'] + 1
			maps['11'] = maps['11'] + 2
	for i in range(n):
		while '.' in layout[i]:
			layout[i] = layout[i].replace('.', '', 1)
			maps['11'] = maps['11'] + 1
	ones = maps['11'] * x
	twos = maps['12'] * y + (maps['11'] - maps['12'] * 2) * x
	print(min(ones, twos))
	t = t - 1


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	result = 0
	if y >= 2 * x:
		for i in range(n):
			for j in input():
				if j == '.':
					result += x
	else:
		for i in range(n):
			A = input()
			X = A
			A = A.replace('..', '')
			if A != X:
				result += (m - len(A)) // 2 * y
			result += A.count('.') * x
	print(result)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = [input().split() for y in range(n)]
	ans = 0
	for s in mat:
		i = 0
		while i < m:
			if i < m - 1 and (not y > x * 2) and (s[0][i] == '.') and (s[0][i + 1] == '.'):
				ans += y
				i += 1
			elif s[0][i] == '.':
				ans += x
			i += 1
	print(ans)


def answer(n, m, x, y, A):
	option = min(2 * x, y)
	price = 0
	for i in range(n):
		count = 0
		for j in range(m):
			if A[i][j] == '.':
				count += 1
			else:
				if count % 2 == 0:
					price += option * (count // 2)
				else:
					price += option * (count // 2) + x
				count = 0
		if count % 2 == 0:
			price += option * (count // 2)
		else:
			price += option * (count // 2) + x
	return price
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	print(answer(n, m, x, y, l))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = 0
	if y >= 2 * x:
		for i in range(n):
			l = list(str(input()))
			for z in l:
				if z == '.':
					s += 1
		print(s * x)
	else:
		for i in range(n):
			l = list(str(input()))
			i = 0
			if l[0] == '.':
				op = True
				d = 1
			else:
				op = False
				d = 0
			for i in range(1, m):
				if l[i] == '.':
					if op:
						d += 1
					else:
						d = 1
						op = True
				else:
					if op:
						s += y * (d // 2) + x * (d % 2)
					op = False
			if op:
				s += y * (d // 2) + x * (d % 2)
		print(s)


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
t = int(input())
for tc in range(t):
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	disregard = 2 * x <= y
	res = 0
	for nc in range(n):
		instr = stdin.readline().strip()
		last = False
		for char in instr:
			if disregard:
				if char == '.':
					res += x
			elif char == '.' and last == True:
				res += y
				last = False
			elif char == '.' and last == False:
				last = True
			elif char != '.' and last == True:
				res += x
				last = False
			elif char == '.' and last == False:
				pass
		if last == True:
			res += x
	stdout.write(str(res) + '\n')


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	c = []
	for i in range(n):
		c.append([i for i in input()])
	if n == 1 and m == 1:
		if c[0][0] == '.':
			print(x)
		else:
			print(0)
	elif m == 1:
		ct = 0
		for i in c:
			if i[0] == '.':
				ct += 1
		print(ct * x)
	elif 2 * x < y:
		ct = 0
		for i in c:
			for j in i:
				if j == '.':
					ct += 1
		print(ct * x)
	else:
		ct = 0
		for i in range(n):
			it = 0
			while it < m - 1:
				if c[i][it] == '.' and c[i][it + 1] == '.':
					ct += y
					it += 2
				elif c[i][it] == '.':
					ct += x
					it += 2
				else:
					it += 1
			if it == m - 1 and c[i][it] == '.':
				ct += x
		print(ct)


import math
num = int(input())
for i in range(num):
	(n, m, x, y) = [int(x) for x in input().split()]
	cost = 0
	for j in range(n):
		a = input()
		if y > 2 * x:
			cost = cost + a.count('.') * x
		else:
			flag = False
			twice = 0
			once = 0
			for k in range(m):
				if a[k] == '*':
					continue
				if not flag:
					if k + 1 > m - 1:
						once += 1
						continue
					elif a[k + 1] == '.':
						flag = True
					else:
						once += 1
				else:
					twice += 1
					flag = False
			cost = cost + twice * y + once * x
	print(cost)


def solve():
	(n, m, x, y) = map(int, input().split())
	flag = False
	cost = 0
	if 2 * x < y:
		flag = True
	for _ in range(n):
		tiles = input().split('*')
		for tile_set in tiles:
			l = len(tile_set)
			if not l:
				continue
			if flag:
				cost += l * x
			else:
				cost += x * (l % 2) + y * (l // 2)
	print(cost)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		result = 0
		for _ in range(n):
			row = list(input())
			index = 0
			while index < m:
				if row[index] == '.':
					if index + 1 < m and row[index + 1] == '.':
						result += min(2 * x, y)
						index += 2
					else:
						result += x
						index += 1
				else:
					index += 1
		print(result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	if x * 2 <= y:
		count = 0
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '.':
					count += 1
		print(count * x)
	else:
		count1 = 0
		count2 = 0
		for i in range(n):
			curr = 0
			for j in range(m):
				if arr[i][j] == '.':
					count1 += 1
					curr += 1
				else:
					count2 += curr // 2
					curr = 0
			count2 += curr // 2
		print(count2 * y + (count1 - count2 * 2) * x)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		sin = s.count('.')
		dou = s.count('..')
		left = sin - 2 * dou
		ans = ans + min(min(dou * y, dou * 2 * x) + left * x, sin * x)
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l1 = []
	l2 = []
	sum = 0
	for i in range(n):
		s = input()
		l1.append(s)
		l2.extend(s)
	if 2 * x <= y:
		c = l2.count('.')
		print(x * c)
	else:
		for i in range(n):
			j = 0
			while j < m:
				if l1[i][j] == '.':
					if j == m - 1:
						sum = sum + x
						j = j + 1
					elif l1[i][j + 1] == '.':
						j = j + 2
						sum = sum + y
					else:
						sum = sum + x
						j = j + 1
				else:
					j = j + 1
		print(sum)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = [input() + '*' for _ in range(n)]
	y = min(y, 2 * x)
	ans = 0
	for i in range(n):
		j = 0
		cons = 0
		while j < m + 1:
			if grid[i][j] == '.':
				cons += 1
			else:
				ans += cons // 2 * y
				ans += cons % 2 * x
				cons = 0
			j += 1
	print(ans)


for tc in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	flag = True if 2 * x > y else False
	for row in range(n):
		s = list(input())
		s.append('*')
		for i in range(m):
			if s[i] == '.':
				if s[i + 1] == '.':
					if flag:
						ans += y
					else:
						ans += 2 * x
					s[i + 1] = '*'
				else:
					ans += x
	print(ans)


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	res = 0
	ans = res
	krish = 1
	for i in range(n):
		s = input()
		j = 0
		nnnn = True
		while j < m:
			c = 0
			if s[j] == '.':
				while j < m and s[j] == '.':
					c += 1
					j += 1
				a = c // 2
				b = c % 2
				if 2 * x <= y:
					res += x * c
				else:
					res += x * b + y * a
			j += 1
	print(res)


def main():
	(n, m, x, y) = map(int, input().split())
	a = [input() + '*' for _ in range(n)]
	if x * 2 <= y:
		c = sum(map(lambda ai: ai.count('.'), a))
		print(c * x)
		return
	res = 0
	i = 0
	while i < n:
		j = 0
		curr = 0
		while j <= m:
			if a[i][j] == '*':
				res += y * (curr // 2)
				if curr % 2:
					res += x
				curr = 0
			else:
				curr += 1
			j += 1
		i += 1
	print(res)
for _ in range(int(input())):
	main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(d, o) = (0, 0)
	for i in range(n):
		j = 0
		t = input()
		while j < m:
			if t[j] == '.':
				if j < m - 1:
					if t[j + 1] == '.':
						d += 1
						j += 2
					else:
						o += 1
						j += 1
				else:
					o += 1
					j += 1
			else:
				j += 1
	if y > 2 * x:
		print(x * o + 2 * x * d)
	else:
		print(y * d + x * o)


def solve():
	(n, m, x, y) = map(int, input().split())
	y = min(y, x * 2)
	a = [input() for _ in range(n)]
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '*':
				j += 1
				continue
			k = j
			while k < m and a[i][k] == '.':
				k += 1
			w = k - j
			ans += w // 2 * y + w % 2 * x
			j = k
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for t in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	j = 0
	p = 0
	q = 0
	for i in range(n):
		s = input()
		r = 0
		for j in range(m):
			if s[j] == '*':
				p += r // 2
				q += r % 2
				r = 0
				continue
			r += 1
		p += r // 2
		q += r % 2
	if p * y > p * 2 * x:
		r = p * 2 * x
	else:
		r = p * y
	r += q * x
	print(r)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		arr.append(input())
	c = 0
	if 2 * x <= y:
		for i in range(n):
			c += arr[i].count('.') * x
	else:
		for i in range(n):
			all = arr[i].count('.')
			need = arr[i].count('..')
			c += need * y + (all - need * 2) * x
	print(c)


for _ in range(int(input())):
	(h, w, p1, p2) = map(int, input().split())
	pl = [input().split('*') for _ in range(h)]
	if p1 * 2 <= p2:
		c = 0
		for i in range(h):
			for j in pl[i]:
				c += j.count('.') * p1
		print(c)
	else:
		c = 0
		for i in range(h):
			for s in pl[i]:
				if len(s) % 2 == 0:
					c += len(s) // 2 * p2
				else:
					c += len(s) // 2 * p2 + p1
		print(c)


n = int(input())
for i in range(n):
	(a, b, x, y) = map(int, input().split())
	c = []
	for j in range(a):
		c.append(str(input()))
	cost = 0
	for j in range(a):
		h = 0
		f = 0
		for k in range(b):
			if c[j][k] == '*':
				h = h + f
				cost1 = int(h / 2) * y + h % 2 * x
				cost2 = h * x
				cost = cost + min(cost1, cost2)
				f = 0
				h = 0
			else:
				f = f + 1
		if f != 0:
			cost1 = int(f / 2) * y + f % 2 * x
			cost2 = f * x
			cost = cost + min(cost1, cost2)
	print(cost)


def solve():
	shit = list(map(int, input().split()))
	n = shit[0]
	m = shit[1]
	x = shit[2]
	y = shit[3]
	sqr = []
	for k in range(n):
		sl = [char for char in input()]
		sqr.append(sl)
	cost = 0
	for row in sqr:
		for i in range(len(row)):
			if row[i] == '.' and i != len(row) - 1 and (2 * x > y):
				if row[i + 1] == '.':
					cost += y
					row[i] = '*'
					row[i + 1] = '*'
				else:
					cost += x
					row[i] = '*'
			elif row[i] == '.':
				cost += x
				row[i] = '*'
	print(cost)
t = int(input())
for _ in range(t):
	solve()


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	count = 0
	for i in range(n):
		s1 = input()
		count += s1.count('.')
		l.append(s1)
	ans = 0
	if 2 * x <= y:
		print(count * x)
		continue
	i = 0
	while i < n:
		j = 0
		while j < m:
			if l[i][j] == '*':
				j += 1
				continue
			if j != m - 1 and l[i][j] == '.' and (l[i][j + 1] == '.'):
				ans += y
				j += 2
			else:
				ans += x
				j += 1
		i += 1
	print(ans)


import queue
import math
t = int(input())
while t:
	line = input().split()
	(n, m, x, y) = (int(line[0]), int(line[1]), int(line[2]), int(line[3]))
	ans = 0
	for i in range(n):
		row = input() + '*'
		cnt = 0
		for c in row:
			if c == '.':
				cnt += 1
			else:
				if cnt % 2 == 0:
					ans += min(x * cnt, y * cnt // 2)
				else:
					ans += min(x * cnt, y * (cnt // 2) + x)
				cnt = 0
	print(ans)
	t -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	tiles = []
	for _ in range(n):
		tiles.append(list(input().rstrip()) + ['*'])
	cost = 0
	two = min(2 * x, y)
	for i in range(n):
		for j in range(m):
			if tiles[i][j] == '.':
				if tiles[i][j + 1] == '.':
					cost += two
					tiles[i][j] = '*'
					tiles[i][j + 1] = '*'
				else:
					cost += x
					tiles[i][j] = '*'
	print(cost)


from math import ceil

def main():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		t1 = 0
		t2 = 0
		for j in range(n):
			s = input()
			t1 += s.count('.')
			t2 += s.count('..')
		t1 = t1 - t2 * 2
		print(min(x * t1 + y * t2, x * (t1 + 2 * t2)))
main()


terms = int(input())
for term in range(terms):
	(x, y, price1, price2) = [int(item) for item in input().split()]
	houses = ''
	for line in range(x):
		houses += input()
		houses += '-'
	prices1 = houses.count('.') * price1
	prices2 = houses.count('..') * price2 + houses.replace('..', '-').count('.') * price1
	print(min([prices1, prices2]))


import sys
input = sys.stdin.readline
from collections import Counter

def solve():
	(n, m, x, y) = map(int, input().split())
	A = [input().strip() for _ in range(n)]
	if x * 2 <= y:
		return Counter(''.join(A))['.'] * x
	ans = 0
	for i in range(n):
		now = 0
		for j in range(m):
			if A[i][j] == '*':
				if now == 1:
					ans += x
				now = 0
				continue
			if now == 0:
				now += 1
			else:
				now = 0
				ans += y
		if now == 1:
			ans += x
	return ans

def main():
	T = int(input())
	for _ in range(T):
		print(solve())
main()


from collections import deque
from sys import stdin, stdout
import math
input = stdin.readline
for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	arr = [list(input().strip()) for i in range(n)]
	first = 0
	second = 0
	for i in range(n):
		for g in range(m):
			if arr[i][g] != '.':
				continue
			first += a
		g = 0
		while g < m:
			if arr[i][g] != '.':
				g += 1
				continue
			if g < m - 1:
				if arr[i][g + 1] == '.':
					second += b
					g += 1
				else:
					second += a
			else:
				second += a
			g += 1
	print(min(first, second))


t = int(input())
while t:
	(a, b, c, d) = map(int, input().split())
	dd = 0
	sd = 0
	for i in range(a):
		s = input().strip()
		if '.' in s:
			sd += s.count('.')
		if '..' in s:
			dd += s.count('..')
	csd = c * sd
	cdd = d * dd + (sd - 2 * dd) * c
	print(min(csd, cdd))
	t -= 1


n = int(input())
for _ in range(n):
	(m, n, l, r) = map(int, input().split())
	r = min(l * 2, r)
	ans = 0
	for _ in range(m):
		for ln in map(len, input().split('*')):
			ans += ln % 2 * l + ln // 2 * r
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		arr.append(list(input()))
	for i in range(n):
		ct = 0
	dp = [[0] * (m + 1) for j in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if arr[i - 1][j - 1] == '.':
				dp[i][j] += dp[i][j - 1] + 1
	i = n
	cost = 0
	while i > 0:
		j = m
		while j > 0:
			k = dp[i][j]
			if k > 0:
				if k % 2 == 0:
					if 2 * x < y:
						cost += x * k
					else:
						cost += y * (k // 2)
					j = j - k
				else:
					cost += x
					if 2 * x < y:
						cost += x * (k - 1)
					else:
						cost += y * ((k - 1) // 2)
					j = j - k
			else:
				j -= 1
		i -= 1
	print(cost)


import math
import sys
input = sys.stdin.readline
t = int(input())
for cs in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input().strip()
		cnt = 0
		blocks = []
		for j in s:
			if j == '*':
				blocks.append(cnt)
				cnt = 0
			else:
				cnt = cnt + 1
		blocks.append(cnt)
		for j in blocks:
			ans += j % 2 * x
			ans += j // 2 * min(2 * x, y)
	print(ans)


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, M, X, Y, A):
	ans = 0
	for i in range(N):
		r = [c for c in A[i]]
		for j in range(M):
			if j < M - 1 and r[j] == r[j + 1] == '.' and (2 * X > Y):
				ans += Y
				r[j] = '*'
				r[j + 1] = '*'
			elif r[j] == '.':
				ans += X
				r[j] = '*'
	return ans

def main():
	T = read_int()
	for _ in range(T):
		(N, M, X, Y) = read_int_n()
		A = [read_str() for _ in range(N)]
		print(slv(N, M, X, Y, A))
main()


t = int(input())
for fd in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	a = []
	for i in range(n):
		a.append(input())
	y = min(y, 2 * x)
	count = 0
	for j in range(n):
		i = 0
		while i < m:
			if a[j][i] == '.':
				if m - i < 2:
					count += x
					i += 1
				elif a[j][i] == a[j][i + 1]:
					count += y
					i += 2
				else:
					count += x
					i += 1
			else:
				i += 1
	print(count)


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		r = input()
		mat.append(r)
	paved = defaultdict(int)
	for i in range(n):
		for j in range(m):
			if mat[i][j] == '*':
				paved[i, j] = 1
	price = 0
	for i in range(n):
		for j in range(m):
			if paved[i, j] == 0:
				if paved[i, j + 1] == 1 or j == m - 1:
					price += x
				elif paved[i, j + 1] == 0:
					if 2 * x <= y:
						new = x
					else:
						new = y
						paved[i, j + 1] = 1
					paved[i, j] = 1
					price += new
	print(price)
t = int(input())
for _ in range(t):
	main()


from itertools import groupby
from math import ceil

def fn():
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for i in range(n):
		m = input()
		for (i, j) in groupby(m):
			if i == '.':
				l = len(list(j))
				yr = l // 2 * y
				xr = l % 2 * x
				yax = l * x
				if yr + xr <= yax:
					cost += yr + xr
				else:
					cost += yax
	print(cost)
for _ in range(int(input())):
	fn()


for ii in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	(nx, ny) = (0, 0)
	for i in range(n):
		s = input()
		j = 0
		p = []
		for i in range(len(s)):
			if s[i] == '*':
				p.append(i)
		if len(p) == 0:
			nx += len(s) % 2
			ny += len(s) // 2
		elif len(p) == 1:
			nx += p[0] % 2 + (len(s) - p[-1] - 1) % 2
			ny += p[0] // 2 + (len(s) - p[-1] - 1) // 2
		else:
			for i in range(len(p) - 1):
				nx += (p[i + 1] - p[i] - 1) % 2
				ny += (p[i + 1] - p[i] - 1) // 2
			nx += p[0] % 2
			ny += p[0] // 2
			nx += (len(s) - p[-1] - 1) % 2
			ny += (len(s) - p[-1] - 1) // 2
	print(nx * x + ny * y)


t = input()
i = 0
nmxy = list()
col = list()
while i < int(t):
	g = input()
	g = g.split()
	nmxy.append(g)
	k = 0
	tem = list()
	while k != int(nmxy[i][0]):
		f = input()
		f = f.split()
		col.append(f)
		k = k + 1
	i = i + 1
tc = 0
c = 0
while tc < int(t):
	total = 0
	i = 0
	while i < int(nmxy[tc][0]):
		for tiles in col[c + i]:
			l = 0
			while True:
				count = 0
				while tiles[l] == '.':
					count = count + 1
					l = l + 1
					if l == len(tiles):
						break
				if 2 * int(nmxy[tc][2]) < int(nmxy[tc][3]):
					total = total + int(count) * int(nmxy[tc][2])
				else:
					total = total + int(count / 2) * int(nmxy[tc][3]) + int(count % 2) * int(nmxy[tc][2])
				l = l + 1
				if l >= len(tiles):
					break
		i = i + 1
	c = c + i
	print(total)
	tc = tc + 1


def solve():
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		arr.extend(list(map(str, input().split('*'))))
	cost = 0
	for ele in arr:
		l = len(ele)
		if l > 0:
			if y <= x * 2:
				if l % 2 == 0:
					cost += l // 2 * y
				elif l == 1:
					cost += x
				else:
					cost += x
					l -= 1
					cost += l // 2 * y
			else:
				cost += l * x
	return cost
for t in range(0, int(input())):
	print(solve())


def count2(s, a2):
	ans = 0
	for i in a2:
		ans += i.count(s)
	return ans
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for j in range(n):
		a.append(list(input()))
	if x * 2 < y:
		print(count2('.', a) * x)
		continue
	ans = 0
	for j in range(n):
		for j2 in range(m):
			if a[j][j2] == '.':
				if j2 + 1 < m and a[j][j2 + 1] == '.':
					ans += y
					a[j][j2] = '*'
					a[j][j2 + 1] = '*'
				else:
					ans += x
					a[j][j2] = '*'
	print(ans)


for u in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	if 2 * x <= y:
		ans = 0
		for i in range(len(l)):
			ans += l[i].count('.')
		print(ans * x)
	else:
		ans = 0
		for i in range(len(l)):
			t = l[i].count('..')
			t1 = l[i].count('.')
			r = t1 - 2 * t
			ans += t * y + r * x
		print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().strip().split()))
	type1 = 0
	type2 = 0
	for _ in range(n):
		temp = input()
		count = 0
		for i in range(m):
			if temp[i] == '.':
				count += 1
			else:
				type2 += count // 2
				type1 += count % 2
				count = 0
		type2 += count // 2
		type1 += count % 2
	if 2 * x <= y:
		print(x * (type1 + 2 * type2))
	else:
		print(x * type1 + y * type2)


T = int(input())
for t in range(T):
	(N, M, x, y) = [int(_) for _ in input().split()]
	answer = 0
	for i in range(N):
		row = input().split('*')
		for el in row:
			if el:
				l = len(el)
				b2 = l // 2
				answer += min(2 * x * b2, y * b2)
				if l % 2:
					answer += x
	print(answer)


import sys

def solve(arr, x, y):
	if y > 2 * x:
		y = 2 * x
	tot = 0
	for row in arr:
		run = 0
		for c in row:
			if c == 0:
				run += 1
			else:
				if run > 0:
					dubs = run // 2
					sings = run - 2 * dubs
					tot += y * dubs + x * sings
				run = 0
		if run > 0:
			dubs = run // 2
			sings = run - 2 * dubs
			tot += y * dubs + x * sings
	return tot
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
cur = 1
for ti in range(T):
	(n, m, x, y) = [int(x) for x in IN[cur].split(' ')]
	arr = [[] for i in range(n)]
	for i in range(n):
		arr[i] = [1 if c == '*' else 0 for c in IN[cur + i + 1]]
	result = solve(arr, x, y)
	print(result)
	cur += 1 + n


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	mat = []
	count = 0
	k = 0
	for i in range(a):
		n = input()
		n = list(n)
		count += n.count('.')
		mat.append(n)
	if 2 * c < d:
		print(count * c)
	else:
		for i in range(a):
			for j in range(b - 1):
				if mat[i][j] == mat[i][j + 1] == '.':
					k += d
					mat[i][j] = '*'
					mat[i][j + 1] = '*'
			k += mat[i].count('.') * c
		print(k)


t = int(input())
n_list = []
m_list = []
x_list = []
y_list = []
grid_list = []
for i in range(t):
	(n, m, x, y) = (int(x) for x in input().split())
	n_list.append(n)
	m_list.append(m)
	x_list.append(x)
	y_list.append(y)
	grid_list.append([input() for i in range(n_list[i])])
for i in range(t):
	two_tile = sum((x.count('..') for x in grid_list[i]))
	one_tile = sum((x.count('.') for x in grid_list[i]))
	if 2 * x_list[i] < y_list[i]:
		print(x_list[i] * one_tile)
	else:
		print(y_list[i] * two_tile + x_list[i] * (one_tile - 2 * two_tile))


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n, m, x, y, matrix):
	one_by_two = min(y, 2 * x)
	cost = 0
	for row in matrix:
		skip = False
		for (i, col) in enumerate(row):
			if skip:
				skip = False
				continue
			if col == '.':
				if i + 1 < len(row) and row[i + 1] == '.':
					cost += one_by_two
					skip = True
				else:
					cost += x
	return cost
for i in tests():
	(n, m, x, y) = arr_in()
	matrix = [list(str_in()) for z in range(n)]
	out(solve(n, m, x, y, matrix))


for _ in range(int(input())):
	(a, b, o, t) = map(int, input().split())
	s = []
	inp = ''
	for i in range(a):
		inp = input()
		s.append(inp)
	if 2 * o <= t:
		k = 0
		for i in range(a):
			k += s[i].count('.')
		print(k * o)
	else:
		ans = 0
		f = 0
		for i in range(a):
			for j in s[i]:
				if j == '.':
					if f == 1:
						ans -= o
						ans += t
						f = 0
					else:
						f = 1
						ans += o
				else:
					f = 0
			f = 0
		print(ans)


t = int(input())
for x in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		count = 0
		for tiles in range(n):
			List = list(input())
			for char in List:
				if char == '.':
					count += 1
		print(count * x)
	else:
		total_price = 0
		for tiles in range(n):
			List = list(input())
			count = 0
			for char in List:
				if char == '.':
					count += 1
				else:
					total_price += count // 2 * y + count % 2 * x
					count = 0
			total_price += count // 2 * y + count % 2 * x
		print(total_price)


t = int(input())
for i in range(t):
	nmxy = input()
	nmxy = nmxy.split()
	n = int(nmxy[0])
	m = int(nmxy[1])
	x = int(nmxy[2])
	y = int(nmxy[3])
	cost = 0
	for colour in range(n):
		bw = input()
		if 2 * x <= y:
			cost += bw.count('.') * x
		else:
			cost += bw.count('.') * x
			cost += bw.count('..') * y - 2 * bw.count('..') * x
	print(cost)


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	two_tile = y if y < 2 * x else 2 * x
	res = 0
	for _ in range(n):
		s = input() + '*'
		con = 0
		for i in s:
			if i == '.':
				con += 1
			else:
				(a, b) = divmod(con, 2)
				res += two_tile * a + x * b
				con = 0
	print(res)


for _ in range(int(input())):
	arr = [int(x) for x in input().split()]
	(n, m, x, y) = (arr[0], arr[1], arr[2], arr[3])
	li = []
	for i in range(n):
		li.append(input())
	if x <= y / 2:
		wt = 0
		for i in range(n):
			for j in range(m):
				if li[i][j] == '.':
					wt += 1
		ans = wt * x
		print(ans)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if j + 1 < m:
					if li[i][j] == '.' and li[i][j + 1] == '.':
						ans += y
						j += 2
					elif li[i][j] == '.':
						ans += x
						j += 1
					else:
						j += 1
				elif li[i][j] == '.':
					ans += x
					j += 1
				else:
					j += 1
		print(ans)


import math
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ss = 0
	for i in range(n):
		s = input()
		if x * 2 <= y:
			ss += s.count('.') * x
		else:
			p = s.count('..')
			ss += p * y
			z = len(s) - s.count('*') - p * 2
			ss += z * x
	print(ss)


x = int(input())
for i in range(x):
	inp = list(map(int, input().split()))
	n = inp[0]
	m = inp[1]
	x = inp[2]
	y = inp[3]
	if 2 * x < y:
		ans = 0
		for p in range(n):
			string = input()
			for char in string:
				if char == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		for p in range(n):
			string = input()
			indx = 0
			while indx < m - 1:
				if string[indx] == '.' and string[indx + 1] == '.':
					ans += y
					indx += 2
					continue
				elif string[indx] == '.' and string[indx + 1] == '*':
					ans += x
					indx += 2
					continue
				indx += 1
			if indx == m - 1:
				if string[indx] == '.':
					ans += x
		print(ans)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = [list(input()) for i in range(n)]
	c = 0
	if y / 2 >= x:
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					c += x
	else:
		a = (10 ** 4, 10 ** 4)
		for i in range(n):
			for j in range(m):
				if (i, j) in a:
					continue
				if l[i][j] == '.' and j != m - 1 and (l[i][j + 1] == '.'):
					a = ((i, j), (i, j + 1))
					c += y
				elif l[i][j] == '.':
					c += x
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if 2 * x <= y:
		for i in range(n):
			cnt = 0
			col = input()
			for j in range(m):
				if col[j] == '.':
					cnt += 1
			cost = cost + cnt * x
		print(cost)
	else:
		for i in range(n):
			a = 0
			b = 0
			col = input()
			j = 0
			while j < m:
				if col[j] == '.':
					if j + 1 < m and col[j + 1] == '.':
						b += 1
						j += 1
					else:
						a += 1
				j += 1
			cost = cost + a * x + b * y
		print(cost)


import math
t = int(input())
for g in range(0, t):
	nmxy = list(map(int, input().split()))
	n = nmxy[0]
	m = nmxy[1]
	x = nmxy[2]
	y = nmxy[3]
	price = 0
	for i in range(0, n):
		string = input()
		j = 0
		while j < m:
			if string[j] == '.':
				if j < m - 1:
					if string[j + 1] == '.':
						price += min(2 * x, y)
						j = j + 1
					else:
						price += x
				else:
					price += x
			j = j + 1
	print(price)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		(n, _, x, y) = map(int, input().split())
		y = min(y, 2 * x)
		ans = 0
		while n > 0:
			row = input().split('*')
			for tiles in row:
				length = len(tiles)
				if length == 0:
					continue
				if tiles[-1] == '\n':
					length -= 1
				ans += length // 2 * y
				if length % 2 == 1:
					ans += x
			n -= 1
		print(ans)
main()


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 200010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]
spf[0] = spf[1] = -1

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()
for _ in range(int(input())):
	(n, m, x, y) = arrIN()
	a = [input().strip() for i in range(n)]
	ans = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if a[i][j] == '.':
				cnt += 1
			else:
				ans += min(x * cnt, y * (cnt // 2) + x * (cnt % 2))
				cnt = 0
		ans += min(x * cnt, y * (cnt // 2) + x * (cnt % 2))
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	arr = [list(input())]
	for i in range(n - 1):
		arr += [list(input())]
	doubles = 0
	singles = 0
	for i in range(n):
		for j in range(m):
			if j != m - 1:
				if arr[i][j + 1] == '.' and arr[i][j] == '.':
					doubles += 1
					arr[i][j] = '*'
					arr[i][j + 1] = '*'
				elif arr[i][j] == '.':
					singles += 1
			elif arr[i][j] == '.':
				singles += 1
	ans = min(x * (doubles * 2 + singles), y * doubles + x * singles)
	print(ans)


from sys import stdin, stdout
import math
input = stdin.readline

def print(x):
	stdout.write(str(x) + '\n')

def solve():
	(n, m, x, y) = map(int, input().split())
	l = []
	c = 0
	for i in range(n):
		temp = list(input())
		c += temp.count('.')
		l.append(temp)
	if y / 2 > x or m == 1:
		print(x * c)
	else:
		cnt = 0
		for i in range(n):
			j = 0
			while j < m - 1:
				if l[i][j + 1] == '.' and l[i][j] == '.':
					cnt += 2
					j += 1
				j += 1
		print(cnt // 2 * y + (c - cnt) * x)
t = 1
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	theatre = []
	for j in range(n):
		row = input()
		theatre.append(row)
	total_cost = 0
	for j in range(n):
		if m == 1 and theatre[j][0] == '.':
			total_cost += x
		else:
			l = 0
			while l < m:
				if theatre[j][l] == '*':
					pass
					l += 1
				elif l < m - 1 and theatre[j][l] == '.' and (theatre[j][l + 1] == '.'):
					if 2 * x > y:
						total_cost += y
						l += 2
					else:
						total_cost += x
						l += 1
				else:
					total_cost += x
					l += 1
	print(total_cost)


t = int(input())
for i in range(t):
	l = list(map(int, input().strip().split()))
	n = l[0]
	m = l[1]
	x = l[2]
	y = l[3]
	cost = 0
	for j in range(n):
		s = input()
		if 2 * x <= y:
			a = s.count('.')
			cost += a * x
		else:
			a = s.count('.')
			b = s.count('..')
			cost = cost + b * y
			a = a - 2 * b
			cost = cost + a * x
	print(cost)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
answers = []
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	grid = []
	for _ in range(n):
		grid.append([1 if i == '*' else 0 for i in input()])
	a = grid
	freqs = [0 for i in range(m)]
	for i in range(n):
		current = 0
		last = None
		for j in range(m):
			if a[i][j] == 1:
				if current != 0:
					freqs[current - 1] += 1
				current = 0
			else:
				current += 1
			if j == m - 1:
				if current != 0:
					freqs[current - 1] += 1
	if 2 * x <= y:
		ans = 0
		for (i, f) in enumerate(freqs):
			ans += x * f * (i + 1)
	else:
		ans = 0
		for (i, f) in enumerate(freqs):
			ans += x * f * ((i + 1) % 2)
			ans += y * f * ((i + 1) // 2)
	answers.append(ans)
print(*answers, sep='\n')


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')

def main():
	for _ in range(iin()):
		(n, m, x, y) = ain()
		t = []
		for i in range(n):
			t.append(list(sin()))
		if 2 * x < y:
			y = 2 * x
		cost = 0
		for i in range(n):
			count = 0
			for j in range(m):
				if t[i][j] == '.':
					count += 1
				else:
					cost += count // 2 * y
					cost += count % 2 * x
					count = 0
			cost += count // 2 * y
			cost += count % 2 * x
		print(cost)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split(' ')))
	a = []
	for i in range(n):
		temp = input()
		a.append([j for j in temp])
	count = 0
	if 2 * x <= y:
		for i in range(n):
			count += a[i].count('.') * x
	else:
		for i in range(n):
			flag = 1
			for j in range(m):
				if a[i][j] == '.':
					count += x
				if flag == 0:
					if a[i][j] == '.' and a[i][j - 1] == '.':
						count += y - 2 * x
						flag = 1
						continue
				else:
					flag = 0
	print(count)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	c = 0
	for i in range(n):
		l1 = list(input())
		for j in l1:
			if j == '.':
				c = c + 1
		l.append(l1)
	ans = 0
	ans1 = c * x
	i = 0
	while i < n:
		j = 0
		while j < m:
			if l[i][j] == '.':
				ans = ans + x
				if j + 1 < m:
					if l[i][j + 1] == '.':
						ans = ans + (y - x)
						j = j + 1
			j = j + 1
		i = i + 1
	print(min(ans, ans1))


t = int(input())
l = []
for i in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	if 2 * x <= y:
		y = 2 * x
	cost = 0
	for k in range(n):
		str = input()
		dot = 0
		for s in str:
			if s == '.':
				dot = dot + 1
			else:
				d = int(dot / 2)
				s = dot % 2
				cost += d * y + s * x
				dot = 0
		d = int(dot / 2)
		s = dot % 2
		cost += d * y + s * x
	l.append(cost)
for x in l:
	print(x)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(n, m, x, y) = iia()
	arr = []
	for i in range(n):
		arr.append(list(is1()))
	cost = 0
	for row in arr:
		i = 0
		while i < len(row):
			if row[i] == '.':
				if i + 1 < len(row) and row[i + 1] == '.':
					cost += min(2 * x, y)
					i += 2
				else:
					cost += x
					i += 1
			else:
				i += 1
	print(cost)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = input()
		single = row.count('.')
		double = row.count('..')
		ans1 = single * x
		ans2 = double * (y - 2 * x) + ans1
		ans += min(ans1, ans2)
	print(ans)


t = int(input())
import math
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	res_all = 0
	for __ in range(n):
		A = []
		s = input()
		length = 0
		for c in s:
			if c == '.':
				length += 1
			else:
				A.append(length)
				length = 0
		A.append(length)
		res = 0
		if 2 * x > y:
			for a in A:
				res += a // 2 * y + a % 2 * x
		else:
			res = sum(A) * x
		res_all += res
	print(res_all)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(s_dot, do_dot) = (0, 0)
	for i in range(n):
		s = input()
		s_dot += s.count('.')
		do_dot += s.count('..')
	res1 = do_dot * y + x * (s_dot - 2 * do_dot)
	res2 = x * s_dot
	print(min(res1, res2))


for q in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if y > 2 * x:
		y = 2 * x
	s = 0
	for i in range(n):
		a = input()
		j = 0
		while j < m:
			if a[j] == '.':
				if j == m - 1:
					s += x
					j += 1
				else:
					if a[j + 1] == '.':
						s += y
					else:
						s += x
					j += 2
			else:
				j += 1
	print(s)


import math
t = int(input())
for u in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	p = 0
	deff = 0
	a = [[0] * m for i in range(n)]
	for i in range(n):
		u = list(input())
		for j in range(m):
			if u[j] == '.':
				a[i][j] = 1
	for i in range(n):
		for j in range(m):
			if deff == 0:
				if a[i][j] == 1:
					if j != m - 1:
						if a[i][j + 1] == 1:
							if 2 * x >= y:
								p += y
								deff = 1
							else:
								p += 2 * x
								deff = 1
						else:
							p += x
					else:
						p += x
			else:
				deff = 0
	print(p)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		l = list(input().split('*'))
		for j in l:
			a = len(j)
			ans += a // 2 * min(2 * x, y) + a % 2 * x
	print(ans)


t = int(input())
for _ in range(t):
	(n, _, x, y) = map(int, input().split())
	y = x * 2 if x * 2 < y else y
	s = 0
	for _ in range(n):
		ns = list(input())
		skip = False
		for (i, element) in enumerate(ns):
			if skip:
				skip = False
			elif element == '.':
				if i + 1 < len(ns) and ns[i + 1] == '.':
					s += y
					skip = True
				else:
					s += x
	print(s)


import sys
input = sys.stdin.readline

def solve(n, m, x, y, square):
	if x <= y / 2:
		cost = 0
		for r in range(n):
			for c in range(m):
				if square[r][c] == '.':
					cost += x
		return cost
	else:
		cost = 0
		for r in range(n):
			c = 0
			while c < m:
				if square[r][c] == '*':
					c += 1
				elif c < m - 1 and square[r][c + 1] == '.':
					cost += y
					c += 2
				else:
					cost += x
					c += 1
		return cost
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	square = []
	for _ in range(n):
		square.append(input().rstrip())
	print(solve(n, m, x, y, square))


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	li = [0] * n
	for i in range(n):
		li[i] = input()
	(p, q) = (0, 0)
	for i in range(n):
		p += li[i].count('.')
		q += li[i].count('..')
	if 2 * x <= y:
		print(p * x)
	else:
		print(y * q + (p - 2 * q) * x)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = cnt = 0
	for __ in range(n):
		s = input()
		i = 0
		while i < m:
			if s[i] == '.' and s[i + 1] == '.':
				ans += y
				i += 2
				cnt += 2
			elif s[i] == '.':
				ans += x
				i += 1
				cnt += 1
			else:
				i += 1
	if y >= 2 * x:
		print(cnt * x)
	else:
		print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split(' ')))
	mincost = 0
	for j in range(n):
		prev = '*'
		rowcost = 0
		L = input()
		preval = 0
		for k in L:
			if k == '.':
				if prev == '.':
					preval = min(preval + x, preval - x + y)
					prev = '*'
				else:
					preval += x
					prev = k
			else:
				prev = k
			rowcost = preval
		mincost += rowcost
	print(mincost)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		t = input().strip()
		a.append(t)
	ans = 0
	f = False
	if 2 * x > y:
		f = True
	if not f:
		for i in a:
			ans += i.count('.')
		print(ans * x)
	else:
		for i in range(n):
			j = 0
			while j < m:
				if a[i][j] == '.':
					if j < m - 1 and a[i][j + 1] == '.':
						ans += y
						j += 1
					else:
						ans += x
						j += 1
				j += 1
		print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	floor = []
	for i in range(n):
		floor.append(input())
	cost = 0
	if 2 * x <= y:
		for j in floor:
			cost += x * j.count('.')
		print(cost)
	else:
		for j in floor:
			n = j.count('..')
			cost += y * n
			cost += x * (j.count('.') - 2 * n)
		print(cost)


for lo in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ls = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		a = input()
		for j in range(m):
			ls[i][j] = a[j]
	z = y
	if 2 * x < y:
		z = 2 * x
	ans = 0
	for i in range(n):
		for j in range(m - 1):
			if ls[i][j] == '.':
				if ls[i][j + 1] == '.':
					ans += z
					ls[i][j + 1] = '*'
				else:
					ans += x
	for i in range(n):
		if ls[i][m - 1] == '.':
			ans += x
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	s = []
	for __ in range(n):
		s.append(input())
	groupLengths = []
	for i in range(n):
		cnt = 0
		for j in range(m):
			if s[i][j] == '.':
				cnt += 1
			else:
				if cnt > 0:
					groupLengths.append(cnt)
				cnt = 0
		if cnt > 0:
			groupLengths.append(cnt)
	twos = 0
	ones = 0
	for z in groupLengths:
		t = z // 2
		o = z % 2
		twos += t
		ones += o
	if x * 2 > y:
		print(y * twos + x * ones)
	else:
		print(x * (ones + twos * 2))


t = int(input())
for _ in range(t):
	(n, m, c1, c2) = map(int, input().split())
	string = []
	ans = 0
	for i in range(n):
		temp = input()
		j = 0
		while j < m:
			if temp[j] == '.':
				if j + 1 < m and temp[j + 1] == '.':
					ans += min(2 * c1, c2)
				else:
					ans += c1
				j += 1
			j += 1
	print(ans)


import math
t = int(input())
for tt in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		s = input().strip()
		arr.append(s)
	ans = 0
	for i in range(n):
		j = 0
		cc = 0
		while j < len(arr[i]):
			if arr[i][j] == '.':
				cc += 1
			else:
				if 2 * x <= y:
					ans += x * cc
				elif cc % 2 == 0:
					ans += y * (cc // 2)
				else:
					ans += y * (cc // 2)
					ans += x
				cc = 0
			j += 1
		if 2 * x <= y:
			ans += x * cc
		elif cc % 2 == 0:
			ans += y * (cc // 2)
		else:
			ans += y * (cc // 2)
			ans += x
		cc = 0
	print(ans)


import math
import sys
for _ in range(int(sys.stdin.readline())):
	(n, m, x, y) = map(int, sys.stdin.readline().split())
	l = []
	count = 0
	for i in range(n):
		xx = []
		yy = sys.stdin.readline()
		for i in range(m):
			xx.append(yy[i])
		count += xx.count('.')
		l.append(xx)
	if 2 * x <= y:
		print(count * x)
	else:
		ans = 0
		last = 1
		for i in range(n):
			for j in range(m - 1):
				if l[i][j] == '.' and l[i][j + 1] == '.':
					ans += 1
					l[i][j] = '*'
					l[i][j + 1] = '*'
		print((count - 2 * ans) * x + ans * y)


t = int(input())
cnt = 0
for cnt in range(t):
	(n, m, x, y) = map(int, input().split())
	a = list()
	sum_1 = 0
	for i in range(n):
		s = input()
		board = list(s)
		cnt = 0
		for i in board:
			if i == '.':
				cnt += 1
			elif cnt > 0:
				a.append(int(cnt))
				cnt = 0
		if cnt > 0:
			a.append(int(cnt))
	if 2 * x <= y:
		sum_1 += x * sum(a)
	else:
		for i in range(len(a)):
			if a[i] >= 2:
				z = a[i] // 2
				sum_1 += y * z
				if a[i] % 2 != 0:
					sum_1 += x
			else:
				sum_1 += x
	a.clear()
	print(sum_1)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	r = 0
	for _ in range(n):
		s = input()
		if 2 * x <= y:
			r += x * s.count('.')
		else:
			st = 0
			for v in s:
				if st:
					r += y if v == '.' else x
					st = 0
				elif v == '.':
					st = 1
			r += st * x
	print(r)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = 0
	l = []
	for i in range(n):
		s = 0
		for j in input():
			if j == '.':
				s += 1
			if s == 2:
				l.append(s)
				s = 0
			if j == '*' and s > 0:
				l.append(s)
				s = 0
		if s > 0:
			l.append(s)
	(s1, s2) = (0, 0)
	for i in l:
		if i == 2:
			s1 += y
			s2 += 2 * x
		else:
			s1 += x
			s2 += x
	print(min(s1, s2))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	total = 0
	for i in range(n):
		squares = input().split('*')
		for s in squares:
			l = len(s)
			if l == 1:
				total += x
			elif y >= 2 * x:
				total += x * l
			else:
				total += y * (l // 2) + x * (l % 2)
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	k = [input() for i in range(n)]
	(doubles, singles, i, j) = (0, 0, 0, 0)
	while i < n:
		while j < m:
			if j < m - 1 and k[i][j] == '.' and (k[i][j + 1] == '.'):
				doubles += 1
				singles += 2
				j += 2
			elif k[i][j] == '.':
				singles += 1
				j += 1
			else:
				j += 1
		i += 1
		j = 0
	net = singles - doubles * 2
	net_doubles = doubles * y + net * x
	net_singles = singles * x
	print(net_singles if net_singles < net_doubles else net_doubles)


def solve():
	from math import ceil
	t = int(input())
	while t > 0:
		(n, m, x, y) = map(int, input().strip().split())
		r = 2 * x
		l = [list(input()) for i in range(n)]
		c = 0
		for i in range(n):
			if m == 1:
				if l[i][0] == '*':
					pass
				else:
					c += x
			else:
				j = 0
				while j < m:
					if l[i][j] == '*':
						j += 1
					elif l[i][j] == '.':
						if j == m - 1:
							c += x
							j += 1
						elif l[i][j + 1] == '.':
							c += min(r, y)
							j += 2
						else:
							c += x
							j += 1
		print(c)
		t -= 1
solve()


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	matrix = []
	whites = 0
	for i in range(n):
		new = list(input())
		whites += new.count('.')
		matrix.append(new)
	if 2 * x <= y:
		print(whites * x)
	else:
		ans = 0
		for i in range(n):
			for j in range(m):
				if j + 1 < m and matrix[i][j] == matrix[i][j + 1] == '.':
					ans += y
					matrix[i][j + 1] = '*'
					matrix[i][j] = '*'
				elif matrix[i][j] == '.':
					ans += x
					matrix[i][j] = '*'
		print(ans)


t = int(input())
for dud in range(t):
	(n, m, x, y) = [int(a) for a in input().split()]
	total = 0
	for dud2 in range(n):
		prev = False
		l = input()
		for c in l:
			if 2 * x <= y:
				if c == '.':
					total += x
			elif c == '.' and prev is True:
				total -= x
				total += y
				prev = False
			elif c == '.':
				total += x
				prev = True
			else:
				prev = False
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	output = 0
	for i in range(n):
		string = input()
		length = m
		z = []
		v = 0
		for i in range(0, length):
			if string[i] == '*':
				if v > 0:
					z.append(v)
				v = 0
				continue
			if string[i] == '.':
				v += 1
		if v > 0:
			z.append(v)
		if y < 2 * x:
			for i in z:
				if i % 2 == 1:
					output += i // 2 * y + x
				else:
					output += i // 2 * y
		else:
			for i in z:
				output += x * i
	print(output)


from sys import stdin

def run(n, m, x, y, board):
	ans = 0
	inf = 1 << 30
	dp = [inf] * (m + 1)
	dp[0] = 0
	for i in range(m):
		if i + 1 <= m:
			dp[i + 1] = min(dp[i + 1], dp[i] + x)
		if i + 2 <= m:
			dp[i + 2] = min(dp[i + 2], dp[i] + y)
	for i in range(n):
		j = 0
		while j < m:
			if board[i][j] == '.':
				sz = 0
				while j < m and board[i][j] == '.':
					j += 1
					sz += 1
				ans += dp[sz]
			else:
				j += 1
	return ans

def main():
	t = int(stdin.readline())
	for _ in range(t):
		(n, m, x, y) = [int(x) for x in stdin.readline().split()]
		board = []
		for _ in range(n):
			board.append(stdin.readline().strip())
		ans = run(n, m, x, y, board)
		print(ans)
import os
if os.path.exists('tmp.in'):
	stdin = open('tmp.in')
main()


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(input()))
	ans = 0
	for i in range(n):
		for j in range(m - 1):
			if a[i][j] == '.' and a[i][j + 1] == '.':
				ans += min(x * 2, y)
				a[i][j] = '*'
				a[i][j + 1] = '*'
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				ans += x
	print(ans)


import re
import math
N_tests = int(input())
for test_case in range(N_tests):
	Data = list(map(int, input().split(' ')))
	cost = 0
	for n in range(Data[0]):
		m = str(input())
		x = re.findall('\\.+', m)
		for W_tiles in x:
			Num = W_tiles.count('.')
			if 2 * Data[2] > Data[3]:
				Tile_2 = math.floor(Num / 2)
				Tile_1 = Num % 2
				cost = cost + Tile_2 * Data[3] + Tile_1 * Data[2]
			else:
				cost = cost + Num * Data[2]
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	tot = 0
	flag = 0
	if 2 * x > y:
		flag = 1
	for i in range(n):
		count = 0
		a = input()
		for j in a:
			if j == '.':
				count += 1
			else:
				if flag == 0:
					tot += count * x
				else:
					tot += count // 2 * y + count % 2 * x
				count = 0
		if count != 0:
			if flag == 0:
				tot += count * x
			else:
				tot += count // 2 * y + count % 2 * x
			count = 0
	print(tot)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	l = []
	for _ in range(n):
		l.append(list(input()))
	p = []
	k = 0
	for e in l:
		for i in e:
			if i == '.':
				k += 1
			else:
				p.append(k)
				k = 0
		p.append(k)
		k = 0
	if 2 * x > y:
		ans = 0
		for e in p:
			if e // 2 > 0:
				ans += e // 2 * y
				ans += e % 2 * x
			else:
				ans += e * x
		print(ans)
	else:
		ans = 0
		for e in p:
			ans += e * x
		print(ans)


def f(s, x, y):
	ans = 0
	last = 0
	for i in range(len(s) + 1):
		if i < len(s) and s[i] == '.':
			last += 1
		elif last:
			if 2 * x <= y:
				ans += last * x
			else:
				ans += last // 2 * y + last % 2 * x
			last = 0
	return ans

def main():
	t = int(input())
	anss = [0] * t
	for i in range(t):
		(n, m, x, y) = map(int, input().split())
		ans = 0
		for _ in range(n):
			ans += f(input(), x, y)
		anss[i] = ans
	for ans in anss:
		print(ans)
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for i in range(n):
		arr = list(input())
		j = 0
		while j < m:
			if j == m - 1:
				if arr[j] == '.':
					cost += x
				break
			elif arr[j] == '.':
				if arr[j + 1] == '.':
					cost += min(2 * x, y)
					j += 2
					if j > m - 1:
						break
				else:
					cost += x
					j += 1
					if j > m - 1:
						break
			else:
				j += 1
	print(cost)


import sys, math, cmath, time
start_time = time.time()

def solve():
	(n, m, x, y) = invr()
	a = []
	for i in range(n):
		s = insr()
		a.append(s + '*')
	qw = [[] for i in range(n)]
	q = 0
	w = 0
	for i in range(n):
		c = 0
		for j in range(m + 1):
			if a[i][j] == '.':
				c = c + 1
			else:
				q = q + c // 2
				w = w + c % 2
				qw[i].append(c)
				c = 0
	print(min(q * y + w * x, (2 * q + w) * x))

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		(n, m, x, y) = In()
		ans = 0
		l = []
		for i in range(n):
			l.append(list(input()))
		for i in range(n):
			count = 0
			for j in range(m):
				if l[i][j] == '*':
					if count >= 2:
						ans += min(count // 2 * y + count % 2 * x, count * x)
					else:
						ans += count * x
					count = 0
				else:
					count += 1
			if count >= 2:
				ans += min(count // 2 * y + count % 2 * x, count * x)
			else:
				ans += count * x
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


T = int(input())
while T:
	(n, m, x, y) = map(int, input().split())
	A = 0
	B = 0
	for i in range(n):
		s = input()
		a = s.count('..')
		b = s.count('.') - 2 * a
		A = A + a
		B = B + b
	W1 = A * y + B * x
	W2 = (A * 2 + B) * x
	print(min(W1, W2))
	T = T - 1


import sys
from collections import defaultdict
import math
input = sys.stdin.readline

def print_result(l):
	return print(' '.join(map(str, l)))

def timer(function):
	import time

	def wrapper(*args, **kwargs):
		t1 = time.time()
		val = function()
		t2 = time.time()
		print('Executed \\{}\\ in {:.2f} seconds.'.format(function.__name__, t2 - t1))
		return val
	return wrapper

def string_to_arr(s):
	return [x for x in s]

def solve():
	(n, m, x, y) = map(int, input().split(' '))
	mat = [string_to_arr(input().split('\n')[0]) for i in range(n)]
	cost = 0
	for i in range(n):
		for j in range(m):
			if mat[i][j] == '.':
				if j != m - 1 and mat[i][j + 1] == '.':
					cost += min(2 * x, y)
					mat[i][j + 1] = '*'
				else:
					cost += x
				mat[i][j] = '*'
	print(cost)
t = int(input())
for _ in range(t):
	solve()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
t = int(input())
while t:
	t = t - 1
	(n, m, x, y) = map(int, input().split())
	pc = x * 2
	rate = 0
	for i in range(n):
		a = input()
		if pc > y:
			twos = a.count('..')
			ones = a.count('.') - twos * 2
			rate = rate + twos * y + ones * x
		else:
			rate = rate + a.count('.') * x
	print(rate)


import math
t = input()
t = int(t)
while t > 0:
	(n, m, x, y) = input().split()
	(n, m, x, y) = (int(n), int(m), int(x), int(y))
	total = 0
	for i in range(n):
		s = input()
		cnt = 0
		for (idx, j) in enumerate(s):
			if j == '.':
				cnt += 1
			if j == '*' or idx == len(s) - 1:
				if 2 * x <= y:
					total += cnt * x
					cnt = 0
				else:
					if cnt % 2 == 0:
						total += cnt // 2 * y
					else:
						total += cnt // 2 * y + x
					cnt = 0
	print(total)
	t -= 1


t = int(input())
while t > 0:
	(n, m, x, y) = map(int, input().split())
	big = 0
	small = 0
	for i in range(n):
		str = input()
		j = 0
		while j < m:
			if j + 1 < m and str[j] == str[j + 1] == '.':
				big += 1
				j += 1
			elif str[j] == '.':
				small += 1
			j += 1
	if y < x * 2:
		print(y * big + x * small)
	else:
		print(x * (2 * big + small))
	t -= 1


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	cost = 0
	for j in range(n):
		chars = list(input())
		count = 0
		for k in range(m):
			char = chars[k]
			if char == '*':
				if x > y / 2:
					cost += count // 2 * y + count % 2 * x
				else:
					cost += count * x
				count = 0
			else:
				count += 1
		if x > y / 2:
			cost += count // 2 * y + count % 2 * x
		else:
			cost += count * x
	print(cost)


t = int(input())
import math
output = []
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	small_output = 0
	for _ in range(n):
		chs = input().split('*')
		for item in chs:
			if 2 * x <= y:
				small_output += len(item) * x
			else:
				small_output += len(item) // 2 * y + len(item) % 2 * x
	output.append(small_output)
for item in output:
	print(item)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(r, s) = (0, 0)
	for _ in range(n):
		inp = input()
		a = inp.count('..')
		r += a
		s += inp.count('.') - 2 * a
	print([r * y + s * x, r * 2 * x + s * x][y > 2 * x])


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(a, b) = ([], 0)
	for _ in range(n):
		a.append(input())
	if 2 * x <= y:
		for i in range(n):
			b += a[i].count('.')
		print(b * x)
	elif m == 1:
		for i in range(n):
			b += a[i].count('.')
		print(b * x)
	else:
		for i in range(n):
			c = a[i].count('..')
			d = a[i].count('.')
			e = d - 2 * c
			b = b + e * x + c * y
		print(b)


t = int(input())
for qwerty in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		y = 2 * x
	s = 0
	for i in range(n):
		string = input()
		i = 0
		one_point = 0
		two_points = 0
		while i < len(string):
			if i < len(string) - 1 and string[i] == string[i + 1] == '.':
				two_points += 1
				i += 2
			elif string[i] == '.':
				one_point += 1
				i += 1
			else:
				i += 1
		s += one_point * x + two_points * y
	print(s)


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	cost = 0
	b = min(b, 2 * a)
	for __ in range(n):
		s = input()
		i = 0
		while i < m:
			if s[i] == '*':
				i += 1
				continue
			j = i
			while j + 1 < m and s[j + 1] == '.':
				j += 1
			l = j - i + 1
			cost += l % 2 * a + l // 2 * b
			i = j + 1
	print(cost)


t = int(input())
while t > 0:
	(a, b, c, d) = input().split()
	n = int(a)
	m = int(b)
	x = int(c)
	y = int(d)
	ans = 0
	if 2 * x <= y:
		for i in range(n):
			s = input()
			for j in s:
				if j == '.':
					ans += x
	else:
		for i in range(n):
			s = input()
			p = 0
			for j in s:
				if j == '.' and p == 1:
					ans -= x
					ans += y
					p = 0
				elif j == '.':
					ans += x
					p = 1
				else:
					p = 0
	t -= 1
	print(ans)


def inp():
	return list(map(int, input().split()))

def solve():
	(black, white) = ('*', '.')
	[n, m, priceOf1, priceOf2] = inp()
	theaterPaves = []
	for _ in range(n):
		theaterPaves.append(list(input()))
	cost = 0
	for i in range(n):
		for j in range(m):
			if theaterPaves[i][j] == black:
				continue
			if priceOf2 >= 2 * priceOf1:
				cost += priceOf1
			elif j == m - 1:
				cost += priceOf1
			elif theaterPaves[i][j + 1] == white:
				cost += priceOf2
				theaterPaves[i][j + 1] = black
			else:
				cost += priceOf1
	return cost
[tests] = inp()
for _ in range(tests):
	print(solve())


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	pavement = [input() for _ in range(n)]
	w = 0
	for i in pavement:
		w += i.count('.')
	if x <= y // 2:
		print(w * x)
	else:
		twos = 0
		for j in pavement:
			twos += j.count('..')
		print(twos * y + (w - twos * 2) * x)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(n, m, x, y) = sep()
	a = []
	for i in range(0, n):
		temp = list(inp())
		a.append(temp)
	if 2 * x > y:
		cost = 0
		for i in range(0, n):
			j = 0
			while j < m:
				if a[i][j] == '.':
					if j < m - 1 and a[i][j + 1] == '.':
						cost += y
						j += 2
					else:
						cost += x
						j += 1
				else:
					j += 1
		print(cost)
	else:
		cost = 0
		for i in range(0, n):
			for j in range(0, m):
				if a[i][j] == '.':
					cost += x
		print(cost)
testcase(int(inp()))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append([c for c in input()])
	cnt_1 = 0
	cnt_2 = 0
	for i in range(n):
		for j in range(1, m):
			if grid[i][j] == '.' and grid[i][j - 1] == '.':
				cnt_2 += 1
				grid[i][j] = '*'
			elif grid[i][j - 1] == '.':
				cnt_1 += 1
		if grid[i][m - 1] == '.':
			cnt_1 += 1
	ans = cnt_1 * x + cnt_2 * min(y, 2 * x)
	print(ans)


a = int(input())
for i in range(0, a):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	ans2 = 0
	for i in range(0, n):
		j = ''
		l = list(map(str, input()))
		for i in l:
			j += str(i)
		p = j.count('.')
		q = j.count('..')
		ans += q * y
		ans += (p - 2 * q) * x
		ans2 += p * x
	print(min(ans, ans2))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lis = []
	if y > 2 * x:
		flag = 1
	else:
		flag = 0
	cost = 0
	for i in range(n):
		lis = list(input())
		if flag:
			cost += lis.count('.') * x
		else:
			for i in range(m - 1):
				if lis[i] == '.':
					if lis[i + 1] == '.':
						cost += y
						lis[i + 1] = '*'
					else:
						cost += x
					lis[i] = '*'
			if lis[-1] == '.':
				cost += x
	print(cost)


from math import trunc
t = int(input())
for test in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	field = []
	for i in range(n):
		field.append(input())
	ans = 0
	for i in field:
		k = 0
		for j in range(m):
			if i[j] == '.':
				k += 1
			else:
				ans += int(k % 2 * x + min(trunc(k / 2) * 2 * x, trunc(k / 2) * y))
				k = 0
		if i[len(i) - 1] != '*':
			ans += int(k % 2 * x + min(trunc(k / 2) * 2 * x, trunc(k / 2) * y))
	print(ans)


t = int(input())
for k in range(t):
	(n, m, x, y) = map(int, input().split())
	S = 0
	y = min(y, x * 2)
	for i in range(n):
		tile = input().split('*')
		for j in range(len(tile)):
			l = len(tile[j])
			S += l // 2 * y + l % 2 * x
	print(S)


t = int(input())
p = []
for i in range(t):
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	summ = 0
	if 2 * x <= y:
		for j in range(n):
			s = input()
			summ += x * s.count('.')
	else:
		for j in range(n):
			s = input()
			k = 0
			while True:
				if k >= m:
					break
				if s[k] == '.':
					if k < m - 1:
						if s[k + 1] == '.':
							summ += y
							k += 2
						else:
							summ += x
							k += 1
					else:
						summ += x
						k += 1
				else:
					k += 1
	p.append(summ)
for i in range(t):
	print(p[i])


t = int(input())
for i in range(t):
	(n, m, a, b) = map(int, input().split())
	S = 0
	s = ['0'] * n
	for j in range(n):
		s[j] = input()
	if a * 2 > b:
		for j in range(n):
			S += s[j].count('..') * b
			s[j] = s[j].replace('..', '*')
			S += s[j].count('.') * a
	else:
		for j in range(n):
			S += s[j].count('.') * a
	print(S)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	p = []
	for j in range(n):
		a = list(input().split())
		p.extend(a)
	z = []
	s = 0
	for j in range(n):
		count = 0
		for k in range(m):
			if p[j][k] == '.':
				count += 1
			else:
				if count > 0:
					z.append(count)
				count = 0
		if p[j][m - 1] != '*':
			z.append(count)
	if y > 2 * x:
		print(sum(z) * x)
		continue
	for k in range(len(z)):
		s = s + int(z[k] / 2) * y + int(z[k] - int(z[k] / 2) * 2) * x
	print(s)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().strip().split())
	L = []
	for i in range(n):
		s = input()
		L.append(s)
	value = 0
	flag = 1
	for i in range(n):
		c = 0
		for j in L[i]:
			if j == '.':
				c = c + 1
				flag = 0
			elif c != 0:
				if c * x < c // 2 * y + c % 2 * x:
					value += c * x
				else:
					value += c // 2 * y + c % 2 * x
				c = 0
				flag = 1
		if flag == 0:
			if c != 0:
				if c * x < c // 2 * y + c % 2 * x:
					value += c * x
				else:
					value += c // 2 * y + c % 2 * x
				c = 0
	print(value)


for _ in range(int(input())):
	(n, m, x, y) = [int(x) for x in input().split()]
	p = 0
	q = 0
	for i in range(n):
		a = input()
		j = 0
		while j < m:
			if a[j] == '.':
				if j + 1 < m and a[j + 1] == '.':
					q += 1
					j += 2
				else:
					p += 1
					j += 1
			else:
				j += 1
	print(min(p * x + q * y, (p + 2 * q) * x))


def count(k, m, x, y):
	if x * 2 <= y:
		ans = 0
		for i in k:
			if i == '.':
				ans += x
	else:
		i = 0
		ans = 0
		while i < m:
			if k[i] == '.':
				ans += x
				if i + 1 < m:
					if k[i + 1] == '.':
						ans += y - x
						i = i + 2
					else:
						i = i + 1
				else:
					i = i + 1
			else:
				i = i + 1
	return int(ans)
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for j in range(n):
		k = input()
		ans += count(k, m, x, y)
	print(ans)


t = int(input())
while t != 0:
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for i in range(n):
		a.append(input())
	(i, j) = (0, 0)
	cost = 0
	flag = True if y < 2 * x else False
	while i < n:
		j = 0
		while j < m:
			if a[i][j] == '*':
				j += 1
			elif j + 1 < m and a[i][j] == '.' and (a[i][j + 1] == '.') and flag:
				cost += y
				j += 2
			else:
				cost += x
				j += 1
		i += 1
	print(cost)
	t -= 1


def cost(k, x, y):
	if 2 * x <= y:
		return k * x
	else:
		return k % 2 * x + k // 2 * y

def process_test_case(i_t):
	(n, m, x, y) = map(int, input().split())
	a_vec = []
	for _ in range(n):
		a_vec.append(input())
	total_cost = 0
	for i in range(n):
		j_len = 0
		for c in a_vec[i]:
			if c == '*' and j_len > 0:
				total_cost += cost(j_len, x, y)
				j_len = 0
			if c == '.':
				j_len += 1
		if j_len > 0:
			total_cost += cost(j_len, x, y)
	print(total_cost)
	return None
n_tests = int(input())
for i_test in range(1, n_tests + 1):
	process_test_case(i_test)


def mincost(s, x, y):
	if y >= 2 * x or len(s) == 1:
		l = list(s)
		return x * l.count('.')
	else:
		l = s.split('*')
		w = 0
		for i in l:
			w += i.count('.') // 2 * y + i.count('.') % 2 * x
		return w
q = int(input())
for j in range(0, q):
	(n, m, x, y) = map(int, input().split())
	t = 0
	for i in range(n):
		s = input()
		t += mincost(s, x, y)
	print(t)


def calculate_row_price(s, x, y, m):
	if 2 * x <= y or m == 1:
		return x * s.count('.')
	else:
		return sum([len(piece) // 2 * y + len(piece) % 2 * x for piece in s.split('*') if piece != ''])
from sys import stdin as f
t = int(f.readline().strip())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in f.readline().strip().split()]
	result = 0
	for i in range(n):
		s = f.readline().strip()
		result = result + calculate_row_price(s, x, y, m)
	print(result)


t = int(input())
for k in range(t):
	(n, m, x, y) = map(int, input().rstrip().split())
	arr = []
	ans1 = 0
	ans2 = 0
	for i in range(n):
		arr.append(input())
	for i in range(n):
		j = 0
		if m == 1 and arr[i][j] == '.':
			ans1 += 1
		else:
			while j < m:
				count = 0
				while arr[i][j] != '*':
					count += 1
					j += 1
					if j >= m:
						break
				else:
					j += 1
				ans2 += count // 2
				ans1 += count % 2
	if y > 2 * x:
		print((ans1 + ans2 * 2) * x)
	else:
		print(ans1 * x + ans2 * y)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split(' ')]
	tile1 = 0
	tile2 = 0
	for i in range(n):
		line = input()
		prev = '*'
		for cur in line:
			if cur == '.':
				if cur == prev:
					tile1 -= 1
					tile2 += 1
					prev = '*'
				else:
					tile1 += 1
					prev = '.'
			else:
				prev = '*'
	if x * 2 > y:
		print(int(tile2 * y + tile1 * x))
	else:
		print(int((tile2 * 2 + tile1) * x))


import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	grid = []
	for i in range(n):
		grid.append(input())
	if 2 * x <= y:
		white = 0
		for i in range(n):
			for j in range(m):
				if grid[i][j] == '.':
					white += 1
		ans = x * white
		print(ans)
	else:
		num_y = 0
		num_x = 0
		visited = []
		for i in range(n):
			visited.append([])
		for i in range(n):
			for j in range(m):
				visited[i].append(0)
		for i in range(n):
			for j in range(m):
				if visited[i][j] == 1:
					continue
				elif j <= m - 2 and grid[i][j] == '.' and (grid[i][j + 1] == '.'):
					visited[i][j] = 1
					visited[i][j + 1] = 1
					num_y += 1
				elif grid[i][j] == '.':
					visited[i][j] = 1
					num_x += 1
		ans = x * num_x + y * num_y
		print(int(ans))


import re
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	qq = 0
	for j in range(n):
		s = input()
		ss = '.'
		res = []
		res = [i for i in range(len(s)) if s.startswith(ss, i)]
		if len(res) == 1:
			qq += x
			continue
		k = 0
		while k < len(res):
			if k + 1 < len(res) and res[k] == res[k + 1] - 1:
				if 2 * x <= y:
					qq += 2 * x
				else:
					qq += y
				k += 2
			else:
				qq += x
				k += 1
	print(qq)


import sys
import math
from collections import defaultdict

def solve(test):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = input() + '*'
		j = 0
		while j < m:
			if row[j] == '.' and row[j + 1] == '.':
				ans += min(2 * x, y)
				j += 2
			elif row[j] == '.' and row[j + 1] == '*':
				ans += x
				j += 1
			else:
				j += 1
	print(ans)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


def solution(n, m, x, y, A):
	if 2 * x <= y:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if A[i][j] == '.':
					cnt += x
		return cnt
	cnt = 0
	for i in range(n):
		for j in range(m):
			if j <= m - 2 and A[i][j] == '.' and (A[i][j + 1] == '.'):
				cnt += y
				A[i][j + 1] = '*'
			elif A[i][j] == '.':
				cnt += x
	return cnt
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	A = []
	for _ in range(n):
		A.append(list(input()))
	print(solution(n, m, x, y, A))


for _ in ' ' * int(input()):
	(n, m, x, y) = map(int, input().split())
	s = ''
	cnt2 = 0
	for j in range(n):
		p = input()
		lst = list(p.split('*'))
		s += ''.join(lst)
		for i in lst:
			cnt2 += len(i) // 2 * y + len(i) % 2 * x
	print(min(len(s) * x, cnt2))


t = int(input())
while t:
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		inp = input()
		grid.append(inp)
	i = 0
	ans = 0
	while i < len(grid):
		j = 0
		while j < len(grid[0]):
			if grid[i][j] == '.':
				if j < len(grid[0]) - 1 and grid[i][j + 1] == '.':
					if 2 * x >= y:
						ans += y
						j += 2
					else:
						ans += x
						j += 1
				else:
					ans += x
					j += 1
			else:
				j += 1
		i += 1
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	ans = 0
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		a = input()
		arr.append([i for i in a])
	if 2 * x <= y:
		for i in arr:
			for j in i:
				if j == '.':
					ans += x
	else:
		for i in range(len(arr)):
			j = 0
			while j < len(arr[i]) - 1:
				if arr[i][j] == '.' and arr[i][j + 1] == '.':
					ans += y
					j += 2
				elif arr[i][j] == '.':
					ans += x
					j += 1
				else:
					j += 1
			if j == len(arr[i]) - 1:
				if arr[i][-1] == '.':
					ans += x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	count = 0
	for i in range(n):
		l.append(input())
		count += l[i].count('.')
	n_x = 0
	n_y = 0
	if y >= 2 * x:
		print(count * x)
	else:
		for i in range(n):
			n_y += l[i].count('..')
			n_x += l[i].count('.') - 2 * l[i].count('..')
		print(n_x * x + n_y * y)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	counter = 0
	if x * 2 <= y or m == 1:
		for i in range(n):
			c = int(input().count('.'))
			counter += c * x
	else:
		for i in range(n):
			line = input()
			yes = line.count('..')
			xes = line.count('.') - 2 * yes
			counter += y * yes + x * xes
	print(counter)


import math
t = int(input())
while t:
	t = t - 1
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		k = []
		s = input()
		for w in s:
			k.append(w)
		l.append(k)
	ans = 0
	for i in range(n):
		for j in range(m):
			if j + 1 < m and l[i][j] == l[i][j + 1] == '.' and (y <= 2 * x):
				l[i][j] = '*'
				l[i][j + 1] = '*'
				ans += y
			elif j + 1 < m and l[i][j] == l[i][j + 1] == '.' and (y > 2 * x):
				l[i][j] = '*'
				l[i][j + 1] = '*'
				ans += 2 * x
			elif l[i][j] == '.':
				l[i][j] = '*'
				ans += x
	print(ans)


num = int(input())
for case in range(num):
	(rows, columns, x, y) = map(int, input().split())
	matrix = []
	for index_y in range(rows):
		matrix.append(input())
	if x <= y / 2:
		count = 0
		for index_y in range(rows):
			count += matrix[index_y].count('.')
		print(count * x)
	else:
		count_x = 0
		count_y = 0
		for index_y in range(rows):
			index_x = 0
			prev = None
			while index_x < columns:
				current = matrix[index_y][index_x]
				if current == '.' and prev == '.':
					prev = None
					count_y += 1
				elif current == '*' and prev == '.':
					count_x += 1
					prev = '*'
				elif current == '.' and (prev == '*' or prev is None):
					prev = '.'
				index_x += 1
			if prev == '.':
				count_x += 1
		print(count_x * x + count_y * y)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	c1 = 0
	c2 = 0
	for i in range(n):
		j = 0
		while j < m - 1:
			if mat[i][j] == mat[i][j + 1]:
				if mat[i][j] == '.':
					c2 += 1
				j += 2
			elif mat[i][j] == '.':
				c1 += 1
				j += 1
			else:
				j += 1
		if j < m and mat[i][j] == '.':
			c1 += 1
	if x * 2 <= y:
		print((c2 * 2 + c1) * x)
	else:
		print(c1 * x + c2 * y)


def minBurles(lst, n, m, x, y):
	res = 0
	if 2 * x <= y:
		for i in range(n):
			res += x * lst[i].count('.')
	else:
		for i in range(n):
			(check, doub) = (1, 0)
			for j in range(m - 1):
				if lst[i][j] == '.' and lst[i][j + 1] == '.' and (check == 1):
					doub += 1
					check = 0
				else:
					check = 1
			res += y * doub + (lst[i].count('.') - doub * 2) * x
	return res
t = int(input())
for i in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	lst = []
	for i in range(n):
		lst.append(input())
	print(minBurles(lst, n, m, x, y))


cases = int(input())
for _ in range(cases):
	(n, m, x, y) = [int(x) for x in input().split()]
	total = 0
	if 2 * x < y:
		one = x
		two = 2 * x
	else:
		one = x
		two = y
	for i in range(n):
		row = list(input())
		count = 0
		for x in row:
			if x == '*':
				total += count * one
				count = 0
			if x == '.':
				count += 1
				if count == 2:
					count = 0
					total += two
		total += count * one
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		fg = 0
	elif x > y:
		fg = 2
	else:
		fg = 1
	count = 0
	for i in range(n):
		s = input()
		s += '#'
		i = 0
		while i < m:
			if s[i] == '.' and s[i + 1] == '.' and (fg == 0):
				count += 2 * x
				i += 2
			elif s[i] == '.' and s[i + 1] == '.' and (fg == 1):
				count += y
				i += 2
			elif s[i] == '.' and s[i + 1] == '.' and (fg == 2):
				count += y
				i += 2
			elif s[i] == '.':
				count += x
				i += 2
			else:
				i += 1
	print(count)


cases = int(input())
for t in range(cases):
	(n, m, x, y) = list(map(int, input().split()))
	c = 0
	for i in range(n):
		a = input()
		if '*' in a:
			l = a.split('*')
		else:
			l = [a]
		for j in l:
			sl = len(j)
			db = sl // 2
			ob = sl % 2
			v = min(x * sl, x * ob + y * db)
			c += v
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		s = input()
		ss = []
		for ii in s:
			ss.append(ii)
		a.append(ss)
	if 2 * x <= y:
		c = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					c = c + 1
		print(c * x)
	else:
		c = 0
		c1 = 0
		for i in range(n):
			j = 0
			while j < m - 1:
				if a[i][j] == '.' and a[i][j + 1] == '.':
					c = c + 1
					a[i][j] = '*'
					a[i][j + 1] = '*'
				elif a[i][j] == '.':
					c1 = c1 + 1
					a[i][j] = '*'
				j = j + 1
			if a[i][m - 1] == '.':
				c1 = c1 + 1
				a[i][j] = '*'
		print(c * y + c1 * x)


for _ in range(int(input())):
	(a, b, one, two) = map(int, input().split())
	ans = 0
	for _ in range(a):
		count = 0
		for (i, x) in enumerate(input()):
			if x == '.':
				count += 1
			if x == '*' or i == b - 1:
				if one * 2 <= two:
					ans += count * one
				else:
					ans += count // 2 * two + count % 2 * one
				count = 0
	print(ans)


import math
num_cases = int(input())
for _ in range(num_cases):
	li = input().strip().split()
	(n, m, x, y) = [int(i) for i in li]
	singles = 0
	doubles = 0
	for _ in range(n):
		row = input().strip()
		i = 0
		while i < m:
			if row[i] == '.':
				if i == m - 1:
					singles += 1
				elif row[i + 1] == '*':
					singles += 1
				else:
					doubles += 1
					i += 1
			i += 1
	if 2 * x < y:
		print(x * (2 * doubles + singles))
	else:
		print(x * singles + y * doubles)


test = int(input())
test_arr = []
for _ in range(test):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for _ in range(n):
		s = str(input())
		arr.append(list(s))
	price = 0
	for i_i in arr:
		single = 0
		linear = 0
		i = 0
		s_flag = 0
		for j in i_i:
			if j == '*':
				i = 0
				s_flag = 0
				if linear % 2 != 0:
					linear -= 1
					single += 1
			elif i == 1:
				linear += 1
				if s_flag == 0:
					single -= 1
					linear += 1
					s_flag = 1
			else:
				single += 1
				i = 1
		if single > 0:
			price += single * x
		if y < 2 * x:
			price += linear // 2 * y
			if linear % 2 != 0:
				price += x
		else:
			price += linear * x
	test_arr.append(price)
for t in test_arr:
	print(t)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	one = 0
	two = 0
	for i in range(a):
		s = input()
		stwo = s.count('..')
		two += stwo
		one = one + s.count('.') - 2 * stwo
	print(c * (one + 2 * two) if 2 * c <= d else one * c + two * d)


def whites(row):
	count = 0
	for char in row:
		if char == '.':
			count += 1
	return count

def twoone(row):
	result = [0, 0]
	consec_whites = 0
	for char in row:
		if char == '.':
			consec_whites += 1
		else:
			result[0] += consec_whites // 2
			result[1] += consec_whites % 2
			consec_whites = 0
	result[0] += consec_whites // 2
	result[1] += consec_whites % 2
	return result
t = int(input())
for iteration in range(t):
	[n, m, x, y] = list(map(int, input().split()))
	cost = 0
	if 2 * x <= y:
		for i in range(n):
			cost += whites(input()) * x
	else:
		for i in range(n):
			[twos, ones] = twoone(input())
			cost += twos * y + ones * x
	print(cost)


from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
	(r, c, x1, x2) = map(int, input().split())
	mat = [input() for i in range(r)]
	if x1 * 2 <= x2:
		wyn = 0
		for i in mat:
			wyn += i.count('.')
		print(wyn * x1)
	else:
		wyn = 0
		for i in range(r):
			row = mat[i]
			ind = 0
			while True:
				if ind >= c:
					break
				if ind < c - 1:
					if row[ind] == '.':
						if row[ind + 1] == '.':
							wyn += x2
							ind += 2
						else:
							wyn += x1
							ind += 1
					else:
						ind += 1
				else:
					if row[ind] == '.':
						wyn += x1
					ind += 1
		print(wyn)


t = int(input())
for _ in range(t):
	(x, y, a, b) = map(int, input().split())
	floor = []
	for i in range(x):
		floor.append(list(input()))
	cost = 0
	for i in floor:
		for j in range(len(i)):
			if j != len(i) - 1:
				if i[j] == '.' and i[j + 1] == '.':
					cost += min(2 * a, b)
					i[j] = '*'
					i[j + 1] = '*'
				elif i[j] == '.':
					cost += a
			elif i[j] == '.':
				cost += a
	print(cost)


t = int(input())
ans = []
for g in range(t):
	(n, m, x, y) = map(int, input().split())
	a = [input() for i in range(n)]
	q = y // 2
	s = 0
	for i in range(n):
		for j in range(m):
			if j != m - 1 and a[i][j] == a[i][j + 1] == '.' and (q < x):
				s += y
				a[i] = a[i][:j + 1] + '*' + a[i][j + 2:]
			elif a[i][j] == '.':
				s += x
	ans += [s]
for i in ans:
	print(i)


import math
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	li = []
	final = []
	f = 0
	su = 0
	for i in range(n):
		s = input()
		li.append(s)
	for i in li:
		c = 0
		for j in i:
			if j == '.':
				c += 1
				f = 1
			else:
				final.append(c)
				f = 0
				c = 0
		if f == 1:
			final.append(c)
	for i in final:
		if i == 1:
			su += x
		elif x * 2 < y:
			su += i * x
		else:
			su += i // 2 * y
			su += i % 2 * x
	print(su)


import math
import os
import random
import re
import sys
test = int(input())
while test > 0:
	test -= 1
	(n, m, one, two) = map(int, input().split())
	cost = 0
	if one * 2 <= two:
		while n > 0:
			n -= 1
			tiles = input()
			for t in tiles:
				if t == '.':
					cost += one
	else:
		while n > 0:
			n -= 1
			tiles = input()
			t = 0
			while t < m:
				if t + 1 < m and tiles[t] == '.' and (tiles[t + 1] == '.'):
					cost += two
					t += 2
				elif tiles[t] == '.':
					cost += one
					t += 1
				else:
					t += 1
	print(cost)


t = int(input())
while t:
	(n, m, x, y) = [int(i) for i in input().split()]
	a = []
	for i in range(n):
		a.append(input())
	adj2 = 0
	tot = 0
	for s in a:
		i = 0
		while i < m:
			if s[i] == '.':
				tot += 1
				if i + 1 < m and s[i + 1] == '.':
					adj2 += 1
					tot += 1
					i += 1
			i += 1
	print(min(tot * x, adj2 * y + (tot - 2 * adj2) * x))
	t -= 1


t = int(input())
for test in range(t):
	(m, n, x, y) = map(int, input().split())
	sum = 0
	for j in range(m):
		s = input()
		i = 0
		while i < n:
			if s[i] == '.':
				if i < n - 1 and s[i + 1] == '.':
					sum += min(2 * x, y)
					i += 1
				else:
					sum += x
					i += 1
			i += 1
	print(sum)


t = int(input())
while t > 0:
	(n, m, x, y) = map(int, input().split())
	nn = n
	t -= 1
	ans = 0
	stack = []
	onlyx = 0
	if x * 2 <= y:
		onlyx = 1
	while nn > 0:
		nn -= 1
		s = input()
		stack.append(s)
	if onlyx == 1:
		for i in stack:
			for j in range(0, len(i)):
				if i[j] == '.':
					ans += x
		print(ans)
	else:
		stack1 = []
		for i in stack:
			for j in range(0, len(i)):
				if i[j] == '.':
					if not stack1:
						stack1.append(j)
					elif stack1:
						if j - stack1[-1] == 1:
							ans += y
							stack1 = []
						else:
							ans += x
							stack1 = [j]
			if stack1:
				stack1 = []
				ans += x
		print(ans)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	sum = 0
	for nn in range(n):
		line = input()
		ii = 0
		while ii < m:
			if line[ii] == '.':
				if ii + 1 < m and line[ii + 1] == '.':
					sum += min(2 * x, y)
					ii += 2
				else:
					sum += x
					ii += 1
			else:
				ii += 1
	print(sum)


def main():
	(n, m, x, y) = map(int, input().split())
	sum = 0
	for i in range(n):
		stroka = input()
		podryad = 0
		for j in range(m):
			if stroka[j] == '.':
				podryad += 1
			elif podryad > 0:
				if 2 * x <= y or podryad == 1:
					sum += x * podryad
				else:
					sum += y * (podryad // 2) + x * (podryad % 2)
				podryad = 0
		if podryad > 0:
			if 2 * x <= y or podryad == 1:
				sum += x * podryad
			else:
				sum += y * (podryad // 2) + x * (podryad % 2)
	return sum
for _ in range(int(input())):
	print(main())


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	ar = []
	for i in range(n):
		ar.append(list(input()))
	if x * 2 <= y:
		ans = 0
		for i in range(n):
			ans += ar[i].count('.') * x
		print(ans)
	else:
		ans = 0
		for i in range(n):
			for j in range(m):
				if ar[i][j] == '.':
					if 0 <= i <= n - 1 and 0 <= j - 1 <= m - 1 and (ar[i][j - 1] == '.'):
						ans += y
						ar[i][j - 1] = '*'
					elif 0 <= i <= n - 1 and 0 <= j + 1 <= m - 1 and (ar[i][j + 1] == '.'):
						ans += y
						ar[i][j + 1] = '*'
					else:
						ans += x
					ar[i][j] = '*'
		print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lis = []
	if x * 2 < y:
		y = x * 2
	for i in range(n):
		a = list(input()) + ['*']
		lis.append(a)
	a = ['*'] * (m + 1)
	lis.append(a)
	ans = 0
	for i in range(n):
		for j in range(m):
			if lis[i][j] == '.' and lis[i][j + 1] == '.':
				ans += y
				lis[i][j] = '*'
				lis[i][j + 1] = '*'
			elif lis[i][j] == '.':
				ans += x
				lis[i][j] = '*'
	print(ans)


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
for _ in range(iinput()):
	(n, m, x, y) = rinput()
	one = x
	two = min(2 * x, y)
	mat = []
	for i in range(n):
		mat.append(input())
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			tiles = 0
			count = 0
			while j < m and mat[i][j] == '.':
				j += 1
				count += 1
			if count % 2 == 0:
				ans += count // 2 * two
			else:
				ans += count // 2 * two + one
			if count == 0:
				j += 1
	print(ans)


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		(n, m, x, y) = map(int, input().split())
		cost = 0
		for _ in range(n):
			aa = input()
			wc = aa.count('.')
			if y >= 2 * x:
				cost += wc * x
			else:
				i = 0
				while i < m - 1:
					if aa[i] == '.' and aa[i + 1] == '.':
						cost += y
						i += 2
					else:
						if aa[i] == '.':
							cost += x
						i += 1
				if i == m - 1 and aa[i] == '.':
					cost += x
		print(cost)
	q = int(input())
	for _ in range(q):
		solve()
main()


import math
t = int(input())
for i in range(t):
	inp = input().split(' ')
	inp = [int(x) for x in inp]
	(n, m, x, y) = (inp[0], inp[1], inp[2], inp[3])
	numOfDot = 0
	numOfTwo = 0
	for j in range(n):
		r = input()
		numOfDot += len(r.split('.')) - 1
		numOfTwo += len(r.split('..')) - 1
	if y >= 2 * x:
		print(numOfDot * x)
	else:
		print(numOfTwo * y + (numOfDot - 2 * numOfTwo) * x)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	A = []
	for i in range(n):
		A.append(input())
	(count, sum) = (0, 0)
	if x > y // 2:
		for i in range(n):
			temp = 0
			for j in range(m):
				if j == m - 1:
					if A[i][j] == '.':
						count += 1
						temp += 1
						sum += temp // 2
						continue
					else:
						sum += temp // 2
						continue
				elif A[i][j] == '.':
					count += 1
					temp += 1
					continue
				else:
					sum += temp // 2
					temp = 0
					continue
		print(sum * y + (count - 2 * sum) * x)
	else:
		for i in range(n):
			for j in range(m):
				if A[i][j] == '.':
					count += 1
		print(count * x)


import sys
input = sys.stdin.readline
import heapq
import copy
import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def preorder_postorder(root, paths, parent, left, level):
	l = 0
	queue = [root]
	while queue != []:
		node = queue[-1]
		flag = 0
		for i in paths[node]:
			if i != parent[node] and level[i] == sys.maxsize:
				parent[i] = node
				level[i] = level[node] + 1
				flag = 1
				queue.append(i)
		if flag == 0:
			left[parent[node]] += 1 + left[node]
			queue.pop()
	return [parent, left, level]

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		[n, m, one, two] = inlt()
		two = min(two, 2 * one)
		floor = []
		for i in range(n):
			floor.append(insr())
		total = 0
		for i in range(n):
			for j in range(m):
				if floor[i][j] == '.':
					if j + 1 < m and floor[i][j + 1] == '.':
						total = total + two
						floor[i][j + 1] = 'D'
					else:
						total = total + one
					floor[i][j] = 'D'
		print(total)
main()


for _ in range(int(input().strip())):
	(n, m, x, y) = map(int, input().strip().split())
	MATRIX = []
	for i in range(n):
		MATRIX.append(input().strip())
	if 2 * x <= y:
		co = 0
		for i in MATRIX:
			co += i.count('.')
		print(str(x * co))
	else:
		xCount = 0
		yCount = 0
		t = [False for j in range(m)]
		visited = [t[:] for i in range(n)]
		for i in range(n):
			for j in range(m):
				if visited[i][j] == False and j < m - 1 and (MATRIX[i][j] == '.') and (MATRIX[i][j + 1] == '.'):
					yCount += 1
					visited[i][j] = True
					visited[i][j + 1] = True
				elif visited[i][j] == False and MATRIX[i][j] == '.':
					xCount += 1
					visited[i][j] = True
		print(str(xCount * x + yCount * y))


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def solve():
	(n, m, a, b) = num_input()
	cost = 0
	for _ in range(n):
		temp = list(input())
		i = 0
		while i < m:
			j = i
			count = 0
			while j < m and temp[j] == '.':
				count += 1
				j += 1
			if count == 1:
				cost += a
			elif count > 1:
				if count % 2 == 0:
					cost += min(count * a, count // 2 * b)
				else:
					cost += min(count * a, a + count // 2 * b)
			i = j + 1
	print(cost)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(one, two) = (0, 0)
	r = ''
	for i in range(n):
		r += input()
	i = 0
	while i < n * m:
		if r[i] == '.':
			if (i + 1) % m == 0 or r[i + 1] != '.':
				one += 1
			else:
				two += 1
				i += 1
		i += 1
	print(one * x + two * min(2 * x, y))


import sys
import math
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	mat = [[] for _ in range(n)]
	whites = 0
	for i in range(n):
		inp = input()
		for j in inp:
			if j == '.':
				whites += 1
				mat[i].append(1)
			else:
				mat[i].append(0)
	if 2 * x <= y:
		print(whites * x)
	else:
		price = 0
		for i in range(n):
			j = 0
			while j < m:
				if mat[i][j] == 0:
					j += 1
					continue
				if j + 1 < m:
					if mat[i][j + 1] == 1:
						price += y
						j += 1
					else:
						price += x
				else:
					price += x
				j += 1
		print(price)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lis = []
	peeY = []
	peeX = []
	for i in range(n):
		lis.append(input())
	for i in lis:
		peey = i.count('..')
		peeY.append(peey)
		peeX.append(m - (peey * 2 + i.count('*')))
	price = 0
	price += sum(peeX) * x
	price += min(sum(peeY * 2) * x, sum(peeY) * y)
	print(price)


t = int(input())
for _ in range(t):
	(h, w, x, y) = map(int, input().split())
	to_pave = [[c == '.' for c in input()] for _ in range(h)]
	if 2 * x <= y:
		ans = sum((row.count(True) for row in to_pave)) * x
	else:
		ans = 0
		for row in to_pave:
			i = 0
			while i < w:
				if row[i] == True:
					if i + 1 < w and row[i + 1] == True:
						i += 2
						ans += y
					else:
						i += 1
						ans += x
				else:
					i += 1
	print(ans)


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N, M, a, b) = map(int, input().split())
	if a * 2 < b:
		b = a * 2
	r = 0
	for _ in range(N):
		s = input().strip()
		s += '#'
		c = 0
		for i in range(M + 1):
			if s[i] == '.':
				c += 1
			else:
				(u, v) = divmod(c, 2)
				r += u * b + v * a
				c = 0
	print(r)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(input())
	dp = [0] * n
	for i in range(n):
		(s, d) = (0, 0)
		f = 0
		for j in range(m):
			if arr[i][j] == '.':
				s += 1
				f += 1
			else:
				d += f // 2
				f = 0
		if f != 0:
			d += f // 2
			f = 0
		if s and d:
			dp[i] = min(s * x, d * y + (s - 2 * d) * x)
		elif s:
			dp[i] = s * x
	print(sum(dp))


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	l = []
	ans = 0
	for i in range(n):
		arr = []
		p = input()
		for i in p:
			arr.append(i)
		l.append(arr)
	for i in range(n):
		j = 0
		while j < m:
			if l[i][j] == '.':
				if j + 1 < m and l[i][j + 1] == '.' and (2 * x >= y):
					ans += y
					j += 2
				else:
					ans += x
					j += 1
			else:
				j += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	tc = 0
	for _ in range(n):
		line = input()
		if x < y / 2:
			tc += x * line.count('.')
		else:
			line = line.split('*')
			for i in line:
				tc += len(i) // 2 * y + x * (len(i) % 2)
	print(tc)


from sys import stdin, stdout
for case in range(int(stdin.readline())):
	(n, m, x, y) = [int(x) for x in stdin.readline().split()]
	tiles = []
	for r in range(n):
		tiles.append([x for x in stdin.readline().strip()])
	if y >= 2 * x:
		total = 0
		for row in tiles:
			for square in row:
				if square == '.':
					total += 1
		print(total * x)
	else:
		total = 0
		for row in tiles:
			index = 0
			while index < len(row):
				if row[index] == '*':
					index += 1
				elif index + 1 < len(row):
					if row[index + 1] == '.':
						total += y
					else:
						total += x
					index += 2
				else:
					total += x
					index += 1
		print(total)


t = int(input())
for q in range(t):
	(n, m, one, two) = map(int, input().split())
	square = []
	b = 0
	w = 0
	tc = 0
	for y in range(n):
		r = input()
		x = 0
		while x < m:
			if r[x] == '.':
				w += 1
				if x != m - 1 and r[x + 1] == '.':
					tc += 1
					w += 1
					x += 2
				else:
					x += 1
			else:
				b += 1
				x += 1
			square.append(r)
	print(min(tc * two + (w - tc * 2) * one, one * w))


from sys import exit
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		be = s.count('.')
		ch = m - be
		if x * 2 <= y:
			ans += be * x
			continue
		j = 0
		while j < m:
			if s[j] == '*':
				j += 1
				continue
			if j + 1 == m or s[j + 1] == '*':
				ans += x
				j += 1
				continue
			ans += y
			j += 2
	print(ans)


def main():
	t = int(input())
	for _ in range(t):
		(r, c, x, y) = [int(x) for x in input().split()]
		mapp = []
		for _ in range(r):
			mapp.append(input().strip())
		singles = 0
		doubles = 0
		for r in mapp:
			singles += r.count('.')
			doubles += r.count('..')
		var1 = singles * x
		var2 = doubles * y + (singles - 2 * doubles) * x
		print(min(var1, var2))
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if 2 * x <= y:
		for s in range(n):
			tiles = [u for u in input()]
			for i in range(m):
				if tiles[i] == '.':
					cost += x
	else:
		for s in range(n):
			tiles = [u for u in input()]
			tilecount = 0
			for i in range(m):
				if tiles[i] == '.':
					tilecount += 1
				elif tiles[i] == '*':
					if tilecount > 0:
						if tilecount % 2 == 0:
							cost += tilecount // 2 * y
						else:
							cost += (tilecount - 1) // 2 * y + x
					tilecount = 0
			if tilecount > 0:
				if tilecount % 2 == 0:
					cost += tilecount // 2 * y
				else:
					cost += (tilecount - 1) // 2 * y + x
	print(cost)


import sys

def I():
	return sys.stdin.readline().rstrip()
for tc in range(1, 1 + int(I())):
	(R, C, x, y) = map(int, I().split())
	y = min(y, 2 * x)
	ans = 0
	for row in range(R):
		for s in I().split('*'):
			ans += len(s) // 2 * y
			ans += len(s) % 2 * x
	print(ans)


for i in range(int(input())):
	(x, y, n, m) = map(int, input().split())
	c = 0
	v = 0
	for j in range(x):
		lst = list(input())
		v = v + lst.count('.')
		q = 0
		while q < y - 1:
			if lst[q + 1] == lst[q] == '.':
				c = c + m
				q = q + 2
			elif lst[q] == '.':
				c = c + n
				q = q + 1
			else:
				q = q + 1
		if q < y:
			if lst[q] == '.':
				c = c + n
	print(min(c, v * n))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	count2 = 0
	count1 = 0
	for i in range(n):
		s = input()
		count2 += s.count('..')
		count1 += s.count('.')
	count1 = count1 - count2 * 2
	price1 = count1 * x + count2 * y
	price2 = (count1 + 2 * count2) * x
	print(min(price1, price2))


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	sn = 0
	c = 0
	for i in range(n):
		s = input()
		f = 0
		for j in range(m):
			if s[j] == '.':
				sn += 1
			if j == m - 1:
				break
			if s[j] == '*':
				f = 0
			if s[j] == '.' and s[j + 1] == '.':
				if f == 0:
					c += 1
					f = 1
				else:
					f = 0
	q = min(sn * x, c * y + (sn - 2 * c) * x)
	print(q)


for t in range(int(input())):
	square = []
	cost = 0
	(n, m, x, y) = map(int, input().split())
	for i in range(n):
		square.append(input())
	is_worth = x * 2 > y
	for s in square:
		if is_worth:
			cost += s.count('..') * y
			s = ''.join(s.split('..'))
			cost += s.count('.') * x
		else:
			cost += s.count('.') * x
	print(cost)


import math
t = int(input())
for i in range(0, t):
	(n, m, x, y) = map(int, input().split(' '))
	c1 = 0
	c2 = 0
	for j in range(0, n):
		l = list(input())
		for k in range(len(l)):
			if l[k] == '.':
				if k + 1 < len(l) and l[k + 1] == '.':
					c2 += 1
					l[k] = '*'
					l[k + 1] = '*'
				else:
					c1 += 1
					l[k] = '*'
	if 2 * x <= y:
		print(x * (c1 + 2 * c2))
	else:
		print(x * c1 + y * c2)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	ans = 0
	for i in range(n):
		temp = []
		count = 0
		for j in range(m):
			if mat[i][j] == '.':
				count += 1
			else:
				temp.append(count)
				count = 0
		temp.append(count)
		temp = [i for i in temp if i]
		for l in temp:
			if l % 2 == 0:
				ans += min(l // 2 * y, l * x)
			else:
				ans += min(l // 2 * y + x, l * x)
	print(ans)


from sys import stdin
input = lambda : stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	(n, m, x, y) = mp()
	l = []
	ans = 0
	if 2 * x <= y:
		p = 2 * x
	else:
		p = y
	for i in range(n):
		sm = input().split('*')
		for j in sm:
			if sm != ' ':
				lth = len(j) // 2
				ans += p * lth
				if len(j) % 2 == 1:
					ans += x
	print(ans)


a0 = int(input())
for _ in range(a0):
	(n, m, x, y) = input().strip().split(' ')
	(n, m, x, y) = [int(n), int(m), int(x), int(y)]
	rows = []
	for _ in range(n):
		rows.append(list(input().strip()))
	res = 0
	if 2 * x < y:
		for row in rows:
			for elem in row:
				if elem == '.':
					res += x
	else:
		for row in rows:
			prev = row[0]
			for elem in row[1:]:
				if elem == '.' and prev == '.':
					res += y
					prev = '*'
				elif elem == '*' and prev == '.':
					res += x
					prev = '*'
				elif elem == '*' and prev == '*':
					continue
				else:
					prev = '.'
			if prev == '.':
				res += x
	print(res)


for t in range(int(input())):
	(n, m, p1, p2) = map(int, input().split())
	(x, y) = (0, 0)
	for i in range(n):
		row = list(input())
		(s, p) = (0, 0)
		for r in range(m):
			if row[r] == '.':
				s += 1
				t += 1
			else:
				p = s // 2
				y += p
				x += s - p * 2
				s = 0
		p = s // 2
		y += p
		x += s - p * 2
	if p2 < p1 * 2:
		print(x * p1 + y * p2)
	else:
		print(p1 * x + p1 * y * 2)


from math import ceil
t = int(input())
for testCase in range(t):
	(n, m, x, y) = [int(_) for _ in input().split()]
	if 2 * x < y:
		y = 2 * x
	diff = y - x
	cost = 0
	for row in range(n):
		square = input()
		prev = 0
		for col in square:
			if col == '.':
				prev += 1
				cost += x * (prev & 1) + diff * (prev + 1 & 1)
			else:
				prev = 0
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	res = 0
	curr = 0
	val1 = x
	if 2 * x > y:
		val2 = y
	else:
		val2 = 2 * x
	for i in range(n):
		for j in range(m):
			if l[i][j] == '*':
				curr = 0
			elif curr == 1:
				res += val2 - val1
				curr = 0
			else:
				curr = 1
				res += val1
		curr = 0
	print(res)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	price = 0
	for i in range(n):
		s = input()
		dot = s.count('.')
		dotdot = s.count('..')
		if 2 * x <= y:
			price += dot * x
		else:
			price += dotdot * y
			price += (dot - dotdot * 2) * x
	print(price)


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	(n, m, x, y) = get_tuple()
	arr = []
	for i in range(n):
		arr.append(list(input()))
	if 2 * x <= y:
		c = 0
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '.':
					c += 1
		print(c * x)
	else:
		i = 0
		(c1, c2) = (0, 0)
		while i < n:
			j = 0
			while j < m:
				if arr[i][j] == '.':
					if j < m - 1:
						if arr[i][j + 1] == '.':
							c2 += 1
							j += 2
						else:
							c1 += 1
							j += 1
					else:
						c1 += 1
						j += 1
				else:
					j += 1
			i += 1
		print(c1 * x + c2 * y)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


for _ in range(int(input())):
	info = list(map(int, input().split(' ')))
	if info[2] * 2 > info[3]:
		choose = 2
	else:
		choose = 1
	cost = 0
	for x in range(info[0]):
		i = 0
		row = list(input())
		while i < info[1]:
			if row[i] == '.':
				if i == info[1] - 1:
					cost += info[2]
					i += 1
				elif i != info[1] - 1 and row[i + 1] == '.':
					if choose == 1:
						cost += 2 * info[2]
					else:
						cost += info[3]
					i += 2
				elif i != info[1] - 1 and row[i + 1] == '*':
					cost += info[2]
					i += 2
			else:
				i += 1
	print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		ar = input()
		j = 0
		fl = 0
		while j < m:
			if x * 2 > y and j > 0 and (ar[j] == '.') and (fl == 1) and (ar[j] == ar[j - 1]):
				fl = 0
				ans -= x
				ans += y
			elif ar[j] == '.':
				ans += x
				fl = 1
			j += 1
	print(ans)


def f(a, n, m, x, y):
	if y >= x * 2:
		return x * sum([i.count('.') for i in a])
	ans = 0
	for i in range(n):
		j = 0
		(cx, cy) = (0, 0)
		while j < m:
			if j + 1 < m and a[i][j] == '.' and (a[i][j + 1] == '.'):
				cy += 1
				j += 1
			elif a[i][j] == '.':
				cx += 1
			j += 1
		ans = ans + cy * y + cx * x
	return ans
for i in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	a = []
	for i in range(n):
		ls = [str(i) for i in input().split()]
		a.extend(ls)
	print(f(a, n, m, x, y))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	one_by_one = 0
	two_by_one = 0
	whites = 0
	for i in range(n):
		s = input()
		cnt = 0
		for j in s:
			if j == '.':
				cnt += 1
				whites += 1
			elif cnt > 0:
				two_by_one += cnt // 2
				one_by_one += cnt % 2
				cnt = 0
		if cnt > 0:
			two_by_one += cnt // 2
			one_by_one += cnt % 2
	if y >= 2 * x:
		ans = whites * x
	else:
		ans = one_by_one * x
		ans += two_by_one * y
	print(ans)


import sys, math
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	tpc = 0
	tsc = 0
	for i in range(n):
		a = input()
		flag = 0
		for j in range(m):
			if j != m - 1 and a[j] == '.' and (a[j + 1] == '.') and (flag == 0):
				tpc = tpc + 1
				flag = 1
			else:
				flag = 0
			if a[j] == '.':
				tsc = tsc + 1
	if 2 * x <= y:
		print(tsc * x)
	else:
		print(tpc * y + (tsc - tpc * 2) * x)


t = int(input())
while t:
	t -= 1
	(n, m, xval, yval) = map(int, input().split(' '))
	lis = []
	for x in range(n):
		s = input()
		lis.append([x for x in s])
	mi = min(2 * xval, yval)
	ans = 0
	for x in range(n):
		for y in range(m):
			if y + 1 < m:
				if lis[x][y] == lis[x][y + 1] and lis[x][y] == '.':
					ans += mi
					lis[x][y + 1] = '#'
				elif lis[x][y] == '.':
					ans += xval
			elif lis[x][y] == '.':
				ans += xval
	print(ans)


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10 ** 9 + 7
inf = float('inf')

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))
t = I()
for _ in range(t):
	(n, m, x, y) = LI()
	maze = [list(input()) for _ in range(n)]
	ans = 0
	for i in range(n):
		for j in range(m):
			if maze[i][j] == '.':
				if j + 1 < m:
					if maze[i][j + 1] == '.':
						if 2 * x <= y:
							ans += 2 * x
							maze[i][j + 1] = '*'
						else:
							ans += y
							maze[i][j + 1] = '*'
					else:
						ans += x
				else:
					ans += x
	print(ans)


for i in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	sum = 0
	if b >= 2 * a:
		for j in range(n):
			s = input()
			sum += s.count('.') * a
	else:
		for j in range(n):
			s = input()
			k = 0
			if '*' in s:
				for i in range(len(s)):
					if s[i] != '*':
						k += 1
					else:
						if k % 2 == 0:
							sum += b * int(k / 2)
						else:
							sum += int((k - 1) / 2) * b + a
						k = 0
				if k != 0:
					if k % 2 == 0:
						sum += int(k / 2) * b
					else:
						sum += int((k - 1) / 2) * b + a
			elif s.count('.') % 2 == 0:
				sum = sum + b * int(s.count('.') / 2)
			else:
				sum += a + int(s.count('.') / 2) * b
	print(sum)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if x * 2 < y:
		y = x * 2
	ans = 0
	for aewa in range(n):
		save = '*'
		for i in list(input()):
			if i == '.' and save == '.':
				ans += y
				save = '*'
			elif i == '.':
				save = '.'
			elif save == '.':
				ans += x
				save = '*'
		if save == '.':
			ans += x
	print(ans)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrix(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d
t = int(input())
for q in range(t):
	(n, m, x, y) = inputnums()
	a = inputmatrix(n)
	ones = 0
	twos = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j == m - 1:
					ones += 1
					break
				elif a[i][j + 1] == '.':
					twos += 1
					j += 2
				elif a[i][j + 1] == '*':
					ones += 1
					j += 2
			else:
				j += 1
	if 2 * x <= y:
		print((ones + 2 * twos) * x)
	else:
		print(ones * x + twos * y)


for gfhua in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if y > x * 2:
		y = x * 2
	ans = 0
	for i in range(n):
		s = input()
		l = 0
		for j in s:
			if j == '.':
				l += 1
			else:
				ans += l // 2 * y + l % 2 * x
				l = 0
		ans += l // 2 * y + l % 2 * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if x <= y / 2:
		c = 0
		for i in range(n):
			s = input()
			c = c + x * s.count('.')
		print(c)
	else:
		c = 0
		for i in range(n):
			s = input()
			w2 = s.count('..')
			w1 = s.count('.') - 2 * w2
			c = c + y * w2
			c = c + x * w1
		print(c)


import sys
input = sys.stdin.readline
from collections import defaultdict
import math
for _ in range(int(input())):
	(N, M, F, S) = map(int, input().split())
	L = []
	for i in range(N):
		L.append(list(input()))
	count = 0
	if S < 2 * F:
		for i in range(N):
			for j in range(M):
				if L[i][j] == '.':
					if j + 1 < M:
						if L[i][j + 1] == '.':
							L[i][j + 1] = '#'
							count += S
						else:
							count += F
					else:
						count += F
		print(count)
	else:
		for i in range(N):
			for j in range(M):
				if L[i][j] == '.':
					count += F
		print(count)


t = int(input())
for tt in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	y = min(y, 2 * x)
	total = 0
	for nn in range(n):
		row = input()
		i = 0
		while i < m:
			if row[i] == '*':
				i += 1
			elif i + 1 < m and row[i + 1] == '.':
				total += y
				i += 2
			else:
				total += x
				i += 2
	print(total)


from itertools import groupby
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	p1 = p2 = 0
	for i in range(n):
		l = list(input())
		l = [(i[0], len(list(i[1]))) for i in groupby(l)]
		for (p, q) in l:
			if p == '.':
				p1 += q // 2 * y + q % 2 * x
				p2 += q
	print(min(p1, p2 * x))


rw = int(input())
for ewqr in range(rw):
	(n, m, x, y) = map(int, input().split())
	a = [[] for i in range(n + 1)]
	for i in range(n):
		a[i] = list(input() + '*')
	a[n] = ['*' for i in range(m)]
	p2 = 0
	p1 = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if a[i][j + 1] == '.':
					p2 += 1
					a[i][j + 1] = '*'
				else:
					p1 += 1
	print(min(p2 * y + p1 * x, p2 * 2 * x + p1 * x))


from math import ceil
t = int(input())
while t:
	t -= 1
	(n, m, x, y) = [int(i) for i in input().split()]
	if y > 2 * x:
		y = 2 * x
	ans = 0
	for i in range(n):
		a = input()
		temp = 0
		for j in range(len(a)):
			if a[j] == '.' and temp == 0:
				if j != len(a) - 1 and a[j + 1] == '.':
					ans += y
					temp = 1
				else:
					ans += x
			elif temp == 1:
				temp = 0
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	rec = [input() for x in range(n)]
	cost = [x, min(x * 2, y)]
	ans = 0
	for i in rec:
		j = 0
		while j < m:
			if i[j] == '.':
				if j < m - 1 and i[j + 1] == '.':
					ans += cost[1]
					j += 1
				else:
					ans += cost[0]
			j += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	l = 0
	if 2 * x < y:
		for _ in [0] * n:
			a = list(input())
			p = p + a.count('.')
		print(x * p)
	else:
		for _ in [0] * n:
			a = list(input())
			j = 0
			while j < m - 1:
				if a[j] == '.' and a[j + 1] == '.':
					l = l + 1
					j = j + 2
				elif a[j] == '.':
					p = p + 1
					j = j + 2
				else:
					j = j + 1
			if j == m - 1 and a[-1] == '.':
				p = p + 1
		print(l * y + p * x)


def tiles(grid):
	single = 0
	double = 0
	for i in range(len(grid)):
		j = 0
		while j < len(grid[0]):
			if j < len(grid[0]) - 1 and grid[i][j] == grid[i][j + 1] == '.':
				double += 1
				j += 2
				continue
			elif grid[i][j] == '.':
				single += 1
			j += 1
	return (single, double)

def minprice(grid, x, y):
	(single, double) = tiles(grid)
	if 2 * x < y:
		return single * x + double * 2 * x
	return single * x + double * y
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for j in range(n):
		grid.append(input())
	print(minprice(grid, x, y))


def count(ai):
	return ai.count('.')
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	aij = [input() for i in range(n)]
	if x * 2 <= y:
		ai = [count(aij[i]) for i in range(n)]
		print(sum(ai) * x)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if aij[i][j] == '*':
					j += 1
					continue
				if j + 1 < m and aij[i][j] == aij[i][j + 1]:
					ans += y
					j += 2
				else:
					ans += x
					j += 1
		print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		sd = s.count('.')
		dd = s.count('..')
		if x * 2 <= y:
			ans += x * sd
		else:
			ans += dd * y + (sd - 2 * dd) * x
	print(ans)


t = int(input())
for iza in range(t):
	X = input()
	X1 = X.split()
	n = int(X1[0])
	m = int(X1[1])
	x = int(X1[2])
	y = int(X1[3])
	A = [None] * n
	for i in range(n):
		A[i] = [None] * m
	for i in range(n):
		l = input()
		for j in range(m):
			if l[j] == '*':
				A[i][j] = 1
			else:
				A[i][j] = 0
	B = []
	for i in range(n):
		v = [A[i][0], 0]
		B.append(v)
		for j in range(m):
			if B[-1][0] == A[i][j]:
				B[-1][1] = B[-1][1] + 1
			else:
				B.append([A[i][j], 1])
	if 2 * x <= y:
		blanc = 0
		for i in range(len(B)):
			if B[i][0] == 0:
				blanc = blanc + B[i][1]
		print(int(blanc * x))
	else:
		unos = 0
		dos = 0
		for i in range(len(B)):
			if B[i][0] == 0:
				dos = dos + int(B[i][1] / 2)
				unos = unos + B[i][1] % 2
		print(int(unos * x + dos * y))


import os
import sys
from io import BytesIO, IOBase

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def main():
	T = iin()
	for _ in range(T):
		(n, m, x, y) = lin()
		ans = []
		for i in range(n):
			a = input()
			ch = 0
			for i in a:
				if i == '*':
					if ch:
						ans.append(ch)
					ch = 0
				else:
					ch += 1
			else:
				if ch:
					ans.append(ch)
		sol = sum([min(i * x, i // 2 * y + i % 2 * x) for i in ans])
		print(sol)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	minimum = min(x * 2, y)
	for __ in range(n):
		a = input()
		i = 0
		while i < m:
			if a[i] == '*':
				i += 1
				continue
			j = i
			while j + 1 < m and a[j + 1] == '.':
				j += 1
			len = j + 1 - i
			ans += len % 2 * x + len // 2 * minimum
			i = j + 1
	print(ans)


cases = input()
for i in range(0, int(cases)):
	inss = input()
	list = inss.split(' ')
	n = int(list[0])
	m = int(list[1])
	x = int(list[2])
	y = int(list[3])
	price = 0
	if 2 * x > y:
		lame = True
	else:
		lame = False
	for count in range(0, n):
		data = input()
		if lame:
			sing = 0
			doub = 0
			lolz = False
			for item in data:
				if lolz:
					if item == '.':
						doub = doub + 1
						sing = sing - 1
						lolz = False
					else:
						lolz = False
				elif item == '.':
					sing = sing + 1
					lolz = True
			price = price + (doub * y + sing * x)
		else:
			for item in data:
				if item == '.':
					price = price + x
	print(price)


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	total = 0
	for z in range(n):
		line = input()
		j = 0
		while j < len(line):
			if line[j] == '.':
				if j + 1 < len(line) and line[j + 1] == '.' and (x * 2 > y):
					total += y
					j += 2
				else:
					total += x
					j += 1
			else:
				j += 1
	print(total)


def main():
	t = int(input())
	while t > 0:
		calc_cost()
		t -= 1

def calc_cost():
	data = list(map(int, input().split()))
	n = data[0]
	m = data[1]
	x = data[2]
	y = data[3]
	cost = 0
	amount_of_dots = 0
	for i in range(n):
		line = list(input())
		j = 0
		while j < m:
			actual = line[j]
			if j + 1 >= m:
				if actual == '.':
					amount_of_dots += 1
					cost += x
					j += 1
					break
				else:
					break
			else:
				next = line[j + 1]
				if actual == '.':
					amount_of_dots += 1
					if next == '.':
						amount_of_dots += 1
						cost += y
						j += 2
					else:
						cost += x
						j += 1
				else:
					j += 1
	if cost <= amount_of_dots * x:
		print(cost)
	else:
		print(amount_of_dots * x)
main()


from sys import stdin

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in inp().split()]
	ar = []
	white = 0
	cons = 0
	now = False
	for i in range(n):
		ar.append(inp())
	for i in range(n):
		now = False
		for j in range(m):
			if ar[i][j] == '.':
				white += 1
				if now:
					cons += 1
					now = False
				else:
					now = True
			if now and str(ar[i])[j] == '*':
				now = False
	if 2 * x <= y:
		print(x * white)
	else:
		print(x * (white - 2 * cons) + y * cons)


from collections import Counter
import math
t = int(input())
while t > 0:
	t = t - 1
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(str(input())))
	ans = 0
	if y >= 2 * x:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					ans += x
		print(ans)
	else:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					if j < m - 1:
						if a[i][j + 1] == '.':
							ans += y
							a[i][j] = '*'
							a[i][j + 1] = '*'
						else:
							ans += x
							a[i][j] = '*'
					else:
						ans += x
						a[i][j] = '*'
		print(ans)


import sys
from os import path
if path.exists('C:/Users/prana/Desktop/sublime/input.txt'):
	sys.stdin = open('C:/Users/prana/Desktop/sublime/input.txt', 'r')
	sys.stdout = open('C:/Users/prana/Desktop/sublime/output.txt', 'w')
import math

def solve():
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	a = []
	for i in range(n):
		b = input()
		b = list(b)
		a.append(b)
	if 2 * x <= y:
		print(x * sum([i.count('.') for i in a]))
	else:
		c1 = 0
		c = 0
		for i in range(n):
			for j in range(m - 1):
				if a[i][j] == '.' and a[i][j + 1] == '.':
					c1 += 1
					ch = '*'
					a[i][j] = ch
					a[i][j + 1] = ch
		ans = c1 * y
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					c += 1
		ans = ans + c * x
		print(ans)
T = 1
T = int(input())
for _ in range(1, T + 1):
	print('Case #', _, file=sys.stderr)
	solve()


from math import ceil
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = [[x for x in input()] for i in range(n)]
	if y > x * 2:
		y = x * 2
	cost = 0
	for i in range(n):
		for j in range(m):
			if mat[i][j] == '.':
				cost += x
			if mat[i][j] == '.' and j - 1 > -1 and (mat[i][j - 1] == '.'):
				cost -= x * 2
				cost += y
				mat[i][j] = '*'
				mat[i][j - 1] = '*'
	print(cost)


import math

def dict_sort(ans):
	ans = sorted(ans.items(), reverse=True, key=lambda kv: kv[1])

def is_prime(n, k):
	if n == 1:
		return 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			res = i
			break
	return res

def inp():
	ls = list(map(int, input().split()))
	return ls
t = int(input())
for ii in range(t):
	(n, m, x, y) = map(int, input().split())
	adj = 0
	single = 0
	for i in range(n):
		s = input()
		j = 0
		while j < m - 1:
			if s[j] == '.' and s[j + 1] == '.':
				adj += 1
				j += 2
			else:
				j += 1
		for k in range(m):
			if s[k] == '.':
				single += 1
	ans1 = single * x
	if m < 2:
		print(ans1)
	else:
		print(min(ans1, adj * y + (single - adj * 2) * x))


import math
t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	(n, m, x, y) = (l[0], l[1], l[2], l[3])
	if y > 2 * x:
		val = 0
		for j in range(n):
			p = str(input())
			val += p.count('.')
		print(x * val)
	else:
		(vala, valb) = (0, 0)
		for j in range(n):
			p = str(input())
			vala += p.count('..')
			valb += p.count('.')
		print(x * (valb - 2 * vala) + y * vala)


t = int(input())
for _ in range(t):
	(n, m, xx, yy) = map(int, input().split())
	a = [''] * n
	for y in range(n):
		a[y] = list(input())
	ans = 0
	if xx * 2 < yy:
		for y in range(n):
			for x in range(m):
				if a[y][x] == '.':
					ans += xx
	else:
		for y in range(n):
			x = 0
			while x < m:
				if a[y][x] == '*':
					x += 1
					continue
				if x + 1 < m:
					if a[y][x] == '.' and a[y][x + 1] == '.':
						ans += yy
						x += 2
						continue
				ans += xx
				x += 1
	print(ans)


for xyz in range(0, int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(0, n):
		s = input()
		s += '*'
		l.append(s)
	l.append('*' * (m + 1))
	c = 0
	for i in l:
		for j in i:
			if j == '.':
				c += 1
	if True == False:
		print(0)
	else:
		cost = 0
		l1 = []
		for i in l:
			l2 = []
			for j in i:
				if j == '*':
					l2.append(1)
				else:
					l2.append(0)
			l1.append(l2)
		l = l1
		for i in range(0, n):
			for j in range(0, m):
				if l[i][j] == 0:
					if l[i][j + 1] == 0:
						cost += y
						(l[i][j], l[i][j + 1]) = (1, 1)
					else:
						cost += x
						l[i][j] = 1
		print(min(cost, x * c))


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	r = []
	si = 0
	d = 0
	for j in range(n):
		s = input()
		r.append(s)
	for j in range(len(r)):
		q = r[j].count('.')
		si = si + q
	c1 = si * x
	for j in range(len(r)):
		k = 0
		while k < len(r[j]) and len(r[j]) > 1:
			if k == len(r[j]) - 1:
				break
			if r[j][k] == '.' and r[j][k + 1] == '.':
				d = d + 1
				k = k + 2
			else:
				k = k + 1
	si = si - 2 * d
	c2 = si * x + d * y
	print(min(c1, c2))


t = int(input())
for test in range(t):
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	total = 0
	if y >= 2 * x:
		y = 2 * x
	for i in range(n):
		j = 0
		line = input()
		matriz = [str(char) for char in line]
		while j < m:
			if j < m - 1:
				if matriz[j] == '.' and matriz[j + 1] == '.':
					total += y
					j += 2
				elif matriz[j] == '.' and matriz[j + 1] == '*':
					total += x
					j += 2
				elif matriz[j] == '*' and matriz[j + 1] == '.':
					j += 1
				else:
					j += 2
			else:
				if matriz[j] == '.':
					total += x
				j += 1
	print(total)


t = int(input())
for i in range(t):
	price = 0
	(height, length, x1price, x2price) = [int(i) for i in input().split()]
	for j in range(height):
		matrixline = input()
		if 2 * x1price <= x2price:
			price += matrixline.count('.') * x1price
		elif 2 * x1price > x2price:
			price += matrixline.count('..') * x2price
			matrixline = matrixline.replace('..', '')
			price += matrixline.count('.') * x1price
	print(price)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		b = input()
		b = [qq for qq in b]
		a.append(b)
	ans = 0
	if x * 2 <= y:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					ans += 1
		print(ans * x)
	else:
		cnt = 0
		for i in range(n):
			for j in range(m - 1):
				if a[i][j] == '.' and a[i][j + 1] == '.':
					a[i][j] = a[i][j + 1] = '#'
					ans += 1
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					cnt += 1
		print(cnt * x + ans * y)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().strip().split())
	y = min(2 * x, y)
	cost = 0
	for i in range(n):
		cw = 0
		for c in input().strip():
			if c == '.':
				cw += 1
			else:
				cost += cw // 2 * y + cw % 2 * x
				cw = 0
		if cw:
			cost += cw // 2 * y + cw % 2 * x
	print(cost)


T = int(input())
for i in range(T):
	(n, m, c1, c2) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		s = s + '*'
		c = 0
		j = 0
		while j < m:
			e = s[j]
			e1 = s[j + 1]
			if e == e1 == '.':
				if 2 * c1 > c2:
					c = c + c2
				else:
					c = c + 2 * c1
				j = j + 2
			elif e == '.':
				c = c + c1
				j = j + 1
			else:
				j = j + 1
		ans = ans + c
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for j in range(n):
		l.append(input())
	if 2 * x <= y:
		z = True
	else:
		z = False
	ans = 0
	if z == True:
		for j in l:
			for k in range(m):
				if j[k] == '.':
					ans += x
		print(ans)
	else:
		for j in l:
			l1 = j.split('*')
			for k in l1:
				a = len(k) // 2
				b = len(k) % 2
				ans += a * y + b * x
		print(ans)


inp = lambda : map(int, input().split())

def sol():
	(x, y, on, tw) = inp()
	res = 0
	for n in range(x):
		s = input()
		sm = 0
		for n in s:
			if n == '.':
				sm += 1
			else:
				a = sm * on
				b = sm // 2 * tw + sm % 2 * on
				res += min(a, b)
				sm = 0
		a = sm * on
		b = sm // 2 * tw + sm % 2 * on
		res += min(a, b)
	print(res)
for n in range(int(input())):
	sol()


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	v = []
	for j in range(n):
		s = input()
		v.append(s)
	two = 2 * x
	if 2 * x > y:
		two = y
	ans = 0
	for j in range(n):
		k = 0
		while k < m:
			if k < m - 1:
				if v[j][k:k + 2] == '..':
					ans += two
					k += 2
					continue
			if v[j][k] == '.':
				ans += x
			k = k + 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	(ans1, ans2) = (0, 0)
	for i in range(n):
		s = input()
		ans1 += s.count('.') * x
		ans2 += s.count('..') * y + (s.count('.') - 2 * s.count('..')) * x
	print(min(ans1, ans2))


t = int(input())
ans = []
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	field = []
	summ = 0
	flag = False
	min1 = min(2 * x, y)
	for j in range(n):
		field.append(input())
	for j in range(n):
		for k in range(m):
			if flag:
				flag = False
				continue
			elif field[j][k] == '.':
				if k < m - 1 and field[j][k + 1] == '.':
					summ += min1
					flag = True
				else:
					summ += x
	ans.append(summ)
for i in ans:
	print(i)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		(n, m, x, y) = MI()
		ss = [SI() for _ in range(n)]
		ans = 0
		for s in ss:
			cnt = 0
			pc = '.'
			for c in s:
				if c == '.':
					cnt += 1
				elif cnt:
					ans += min(cnt * x, cnt // 2 * y + cnt % 2 * x)
					cnt = 0
			if cnt:
				ans += min(cnt * x, cnt // 2 * y + cnt % 2 * x)
		print(ans)
main()


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split(' '))
	cost = 0
	if x < y / 2:
		for i in range(n):
			Cs = list(input())
			cost += Cs.count('.') * x
	else:
		for i in range(n):
			Cs = list(input())
			j = 0
			while j < m:
				if j == m - 1:
					if Cs[j] == '.':
						cost += x
					j += 1
				elif Cs[j] == '.' and Cs[j + 1] == '.':
					cost += min(2 * x, y)
					j += 2
				elif Cs[j] == '.' and Cs[j + 1] == '*':
					cost += x
					j += 2
				elif Cs[j] == '*':
					j += 1
	print(cost)


import sys
input = lambda : sys.stdin.readline().rstrip()
import math
from collections import deque
from queue import LifoQueue

def binary_search(a, n):
	L = 0
	R = len(a) - 1
	while L <= R:
		mid = L + (R - L) // 2
		if a[mid] == n:
			return mid
		elif a[mid] < n:
			L = mid + 1
		else:
			R = mid - 1
	return -1
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		cnt = 0
		for gogo in range(n):
			s = input()
			for pp in s:
				if pp == '.':
					cnt += 1
		print(cnt * x)
	else:
		sin = 0
		dou = 0
		for i in range(n):
			s = input()
			s += '*'
			cnt = 0
			j = 0
			while j < m:
				if s[j] == '.':
					the = 0
					for k in range(j, m + 1):
						if s[k] != '.':
							the = k - j
							j = k
							break
					if the % 2 == 0:
						dou += the // 2
					else:
						dou += the // 2
						sin += 1
				j += 1
		print(sin * x + dou * y)


for _ in range(int(input())):
	(a, b, x, y) = map(int, input().split())
	total = 0
	for __ in range(a):
		i = input()
		one = i.count('.')
		two = i.count('..')
		if x * 2 >= y:
			total += two * y
			one -= two * 2
			total += one * x
		else:
			total += one * x
	print(total)


import math
t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	n = l[0]
	m = l[1]
	x = l[2]
	y = l[3]
	mat = []
	c = 0
	for _ in range(n):
		a = input()
		a = list(a)
		c += a.count('.')
		mat.append(a)
	if c * x <= math.floor(c / 2) * y + c % 2 * x:
		print(c * x)
	else:
		c1 = 0
		c2 = 0
		for i in range(0, n, 1):
			j = 0
			while j < m:
				if mat[i][j] == '.':
					if j + 1 < m and mat[i][j + 1] == '.':
						c2 += 1
						j += 1
					else:
						c1 += 1
				j += 1
		print(c1 * x + c2 * y)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [list(input()) for _ in range(n)]
	res = 0
	for j in range(m):
		for i in range(n):
			if x + x > y and j < m - 1 and (a[i][j] == '.') and (a[i][j + 1] == '.'):
				a[i][j] = a[i][j + 1] = '*'
				res += y
			elif a[i][j] == '.':
				res += x
	print(res)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	count = 0
	if 2 * x <= y:
		for j in range(n):
			st = input()
			for k in st:
				if k == '.':
					count += x
	else:
		for j in range(n):
			st = input()
			s = len(st)
			k = 0
			while k < s:
				if st[k] == '.':
					if k < s - 1 and st[k + 1] == '.':
						count += y
						k += 2
					else:
						count += x
						k += 1
				else:
					k += 1
	print(count)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt

def solve():
	(n, m, x, y) = read()
	a = [string() for i in range(n)]
	c = 0
	totdot = 0
	for i in range(n):
		contdot = 0
		for j in range(m):
			if a[i][j] == '.':
				contdot += 1
				totdot += 1
			else:
				c += contdot // 2 * y
				c += contdot % 2 * x
				contdot = 0
		c += contdot // 2 * y
		c += contdot % 2 * x
	print(min(c, totdot * x))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


import math
t = int(input())
while t:
	t = t - 1
	(n, m, x, y) = [int(s) for s in input().split()]
	mat = []
	for i in range(n):
		mat.append(input())
	if y >= 2 * x:
		prefer = 1
	else:
		prefer = 2
	cost = 0
	for i in range(n):
		if mat[i][0] == '.':
			arr = [1]
		else:
			arr = []
		for j in range(1, m):
			if mat[i][j] == '.' and mat[i][j - 1] == '.':
				arr[-1] = arr[-1] + 1
			if mat[i][j] == '.' and mat[i][j - 1] == '*':
				arr.append(1)
		if prefer == 1:
			cost = cost + sum(arr) * x
		if prefer == 2:
			for k in arr:
				cost = cost + math.floor(k / 2) * y + (k - 2 * math.floor(k / 2)) * x
	print(cost)


tc = int(input())
for ii in range(tc):
	(n, m, x, y) = map(int, input().split())
	ar = []
	for i in range(n):
		a = input()
		ar.append(a)
	hm = 0
	if y < x * 2:
		hm = 1
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if hm == 1:
				if ar[i][j] == '.':
					if j < m - 1 and ar[i][j + 1] == '.':
						ans += y
						j += 2
					else:
						ans += x
						j += 1
				else:
					j += 1
			else:
				if ar[i][j] == '.':
					ans += x
				j += 1
	print(ans)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		s = input()
		for j in range(m):
			if s[j] == '*':
				l[i][j] = 1
	res = 0
	for i in range(n):
		j = 0
		while j < m:
			if l[i][j] == 0:
				if j + 1 < m and l[i][j + 1] == 0 and (y <= 2 * x):
					res += y
					j += 2
				else:
					res += x
					j += 1
			else:
				j += 1
	print(res)


from sys import stdin, stdout
for query in range(int(stdin.readline())):
	nmxy = stdin.readline().split()
	n = int(nmxy[0])
	m = int(nmxy[1])
	x = int(nmxy[2])
	y = int(nmxy[3])
	d = [[x for x in list(stdin.readline().strip())] for x in range(n)]
	if x * 2 <= y:
		count = 0
		for a in range(n):
			for b in range(m):
				if d[a][b] == '.':
					count += 1
		stdout.write(str(count * x) + '\n')
	else:
		count1 = 0
		count2 = 0
		for a in range(n):
			e = False
			for b in range(m):
				if d[a][b] == '.':
					if not e:
						e = True
					else:
						count2 += 1
						e = False
				else:
					if e:
						count1 += 1
					e = False
			if e:
				count1 += 1
		stdout.write(str(count1 * x + count2 * y) + '\n')


import math
t = int(input())
for i in range(t):
	(r, c, x, y) = map(int, input().split())
	factor = 2 * x
	if factor > y:
		factor = y
	ans = 0
	for j in range(r):
		s = input()
		flag = 0
		for k in range(len(s)):
			if flag == 1:
				flag = 0
				continue
			if s[k] == '.':
				if k != len(s) - 1:
					if s[k + 1] == '.':
						ans += factor
						flag = 1
					else:
						ans += x
				else:
					ans += x
	print(ans)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for testcase in range(ntest):
		(n, m, x, y) = [int(k) for k in stdin.readline().split()]
		y = min(y, 2 * x)
		res = 0
		for i in range(n):
			row = stdin.readline().strip() + '*'
			s = 0
			for c in row:
				if c == '.':
					s += 1
				else:
					res += s // 2 * y + s % 2 * x
					s = 0
		stdout.write('%d\n' % res)
	return 0
main()


for _ in range(int(input())):
	a = input().split()
	b = int(a[0])
	c = int(a[2])
	d = int(a[3])
	e = 0
	if c * 2 > d:
		for __ in range(b):
			f = input().split('*')
			for i in range(len(f)):
				h = len(f[i])
				e = e + (h // 2 * d + h % 2 * c)
	else:
		for __ in range(b):
			f = input().split('*')
			for i in range(len(f)):
				h = len(f[i])
				e = e + h * c
	print(e)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	(H, W, x, y) = MAP()
	grid = [[]] * H
	for i in range(H):
		grid[i] = input() + '*'
	if x * 2 < y:
		y = x * 2
	ans = 0
	for i in range(H):
		j = 0
		while j < W:
			if grid[i][j] == grid[i][j + 1] == '.':
				ans += y
				j += 2
			elif grid[i][j] == '.' and grid[i][j + 1] == '*':
				ans += x
				j += 1
			else:
				j += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	l = []
	for __ in range(n):
		l.append(input())
	if x * 2 <= y:
		cst = 0
		for i in l:
			if '.' in i:
				cst = cst + x * i.count('.')
		print(cst)
	else:
		cst = 0
		for i in range(n):
			j = 0
			while j < m:
				if l[i][j] == '.':
					if j < m - 1:
						if l[i][j + 1] == '.':
							cst = cst + y
							j += 2
						else:
							cst = cst + x
							j += 1
					else:
						cst = cst + x
						j += 1
				else:
					j += 1
		print(cst)


anss = []
for h in range(int(input())):
	(n, m, x, y) = map(int, input().strip().split())
	graph = [list(input()) for i in range(n)]
	ans = 0
	if 2 * x <= y:
		for i in graph:
			ans += i.count('.') * x
	else:
		for i in range(n):
			for j in range(m - 1):
				if graph[i][j] == '.' and graph[i][j + 1] == '.':
					ans += y
					graph[i][j + 1] = graph[i][j] = '*'
			ans += graph[i].count('.') * x
	anss.append(str(ans))
print('\n'.join(anss))


t = int(input())
for i in range(t):
	(n1, m1, x1, y1) = input().split(' ')
	n = int(n1)
	m = int(m1)
	x = int(x1)
	y = int(y1)
	w = c = 0
	for i in range(n):
		k = 0
		s = input()
		for i in range(m):
			if s[i] == '.':
				w = w + 1
		while k < m - 1:
			if s[k] == s[k + 1] and s[k] == '.':
				c = c + 1
				k = k + 2
			else:
				k = k + 1
	if y >= x * 2:
		print(w * x)
	else:
		print(c * y + (w - 2 * c) * x)


import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return list(input())

def solve(n, m, x, y, a):
	c = 0
	if x * 2 <= y or m == 1:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					c += 1
		print(c * x)
	else:
		sum1 = 0
		sum2 = 0
		for i in range(n):
			c = 0
			for j in range(m):
				if a[i][j] == '.':
					c += 1
				elif a[i][j] == '*':
					sum1 += c // 2
					sum2 += c % 2
					c = 0
				if j == m - 1 and a[i][j] == '.':
					sum1 += c // 2
					sum2 += c % 2
		print(sum1 * y + sum2 * x)
n = inp()
for i in range(n):
	l = inlt()
	a = []
	for i in range(l[0]):
		a.append(insr())
	solve(l[0], l[1], l[2], l[3], a)


import math
t = int(input())
for test in range(t):
	(n, m, x, y) = map(int, input().split())
	arr = []
	ans = 0
	for i in range(n):
		s = input()
		arr.append(s)
	if 2 * x <= y:
		for i in range(n):
			ans += arr[i].count('.') * x
	else:
		for i in range(n):
			tot = arr[i].count('.')
			ans += arr[i].count('..') * y + (tot - 2 * arr[i].count('..')) * x
	print(ans)


for _ in range(int(input())):
	(a, b, n, m) = map(int, input().split())
	ans = 0
	for i in range(a):
		Ans = 0
		s = input()
		z = 0
		if 2 * n > m:
			for j in range(len(s) - 1):
				if s[j] == '.' and s[j + 1] == '.':
					if z % 2 == 0:
						Ans += m
					z += 1
				else:
					z = 0
			kek = 0
			for j in range(len(s)):
				if s[j] == '.':
					kek += 1
			kek -= Ans // m * 2
			ans += Ans + kek * n
		else:
			kek = 0
			for j in range(len(s)):
				if s[j] == '.':
					kek += 1
			ans += kek * n
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	cost = 0
	theatre = []
	if y >= 2 * x:
		for j in range(n):
			lst = list(input())
			theatre.append(lst)
			for k in range(m):
				if lst[k] == '.':
					cost += x
	else:
		for j in range(n):
			lst = list(input())
			theatre.append(lst)
			k = 0
			while k <= m - 1:
				if k != m - 1 and lst[k] == '.' and (lst[k + 1] == '.'):
					cost += y
					k = k + 2
				elif lst[k] == '.':
					cost += x
					k = k + 1
				else:
					k = k + 1
	print(cost)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	list = []
	for i in range(n):
		a = [x for x in input()]
		list.append(a)
	if 2 * x < y:
		count = 0
		for q in list:
			for w in q:
				if w == '.':
					count += 1
		print(count * x)
	else:
		count = 0
		pi_2 = 0
		g = 0
		while g < n:
			h = 0
			while h < m - 1:
				if list[g][h] == '.' and list[g][h + 1] == '.':
					pi_2 = pi_2 + 1
					h = h + 2
				else:
					h = h + 1
			g = g + 1
		for q in list:
			for w in q:
				if w == '.':
					count += 1
		print((count - 2 * pi_2) * x + pi_2 * y)


for _ in range(int(input())):
	(n, m, one, two) = map(int, input().split())
	array = []
	for i in range(n):
		array.append(input())
	count1 = 0
	count2 = 0
	previous = False
	if one * 2 > two:
		for x in array:
			previous = False
			for y in x:
				if y == '.':
					if previous:
						count2 += 1
						previous = False
					else:
						count1 += 1
						previous = True
				else:
					previous = False
		count1 = count1 - count2
		print(count1 * one + count2 * two)
	else:
		for x in array:
			for y in x:
				if y == '.':
					count1 += 1
		print(count1 * one)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		s = input()
		if 2 * x <= y:
			ans += s.count('.') * x
		else:
			j = s.find('.')
			if j == -1:
				continue
			for i in range(j + 1, m):
				if s[i] != '.':
					if j != -1:
						ans += (i - j) // 2 * y + (i - j) % 2 * x
					j = -1
				elif j == -1:
					j = i
			if j != -1:
				ans += (m - j) // 2 * y + (m - j) % 2 * x
	print(ans)


t = int(input())

def cost(stars, m, x, y):
	cost2 = min(2 * x, y)
	cost1 = x
	cst = 0
	for ind in range(1, len(stars)):
		blanks = stars[ind] - stars[ind - 1] - 1
		cst += blanks // 2 * cost2 + (cost1 if blanks & 1 else 0)
	return cst
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = input()
		stars = [-1] + [j for j in range(m) if row[j] == '*'] + [m]
		ans += cost(stars, m, x, y)
	print(ans)


from sys import stdout, stdin
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if x * 2 > y:
		one = 0
		two = 0
		for i in range(n):
			s = stdin.readline()
			j = 0
			while j < m:
				if s[j] == '.':
					if j < m - 1 and s[j + 1] == '.':
						two += 1
						j += 2
					else:
						one += 1
						j += 1
				else:
					j += 1
		ans += one * x + y * two
	else:
		cnt = 0
		for i in range(n):
			s = stdin.readline()
			for j in range(m):
				if s[j] == '.':
					cnt += 1
		ans += cnt * x
	stdout.write(str(ans) + '\n')


import sys
input = sys.stdin.readline
flush = sys.stdout.flush

def count(r):
	res = 0
	i = 0
	for x in r:
		if x == '*':
			res += i // 2
			i = 0
			continue
		i += 1
	res += i // 2
	return res
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	A = [list(input().rstrip()) for _ in range(n)]
	res = sum((x.count('.') for x in A)) * x
	if 2 * x <= y:
		print(res)
		continue
	for r in A:
		res -= (2 * x - y) * count(r)
	print(res)


import sys
import math
t = int(sys.stdin.readline().strip())
for i in range(t):
	(n, m, x, y) = list(map(int, sys.stdin.readline().strip().split(' ')))
	grid = []
	for j in range(n):
		grid.append(list(sys.stdin.readline().strip()))
	if y >= 2 * x:
		cnt = 0
		for i in range(n):
			for j in range(m):
				if grid[i][j] == '.':
					cnt += 1
		print(cnt * x)
	else:
		cost = 0
		for i in range(n):
			for j in range(m):
				if j + 1 < m and grid[i][j] == '.' and (grid[i][j + 1] == '.'):
					cost += y
					grid[i][j] = '*'
					grid[i][j + 1] = '*'
				elif grid[i][j] == '.':
					cost += x
					grid[i][j] = '*'
		print(cost)


import sys
sys.stdin.readline()
lines = sys.stdin.read().splitlines()
i = 0
while i < len(lines):
	(n, m, x, y) = map(int, lines[i].split(' '))
	p1 = x
	p2 = min(x * 2, y)
	price = 0
	while n:
		i += 1
		n -= 1
		row = lines[i]
		seq = 0
		for cell in row:
			if cell == '.':
				seq += 1
				if seq == 2:
					seq = 0
					price += p2
			elif seq:
				assert seq == 1
				seq = 0
				price += p1
		if seq:
			assert seq == 1
			seq = 0
			price += p1
	i += 1
	print(price)


import sys
import math
import heapq
YES = 'YES'
NO = 'NO'
modulo = 10 ** 9 + 7

def solve():
	(n, m, x, y) = map(int, input().split())
	rows = [input() for _ in range(n)]
	dp = []
	for _ in range(n + 1):
		dp.append([0 for _ in range(m + 1)])
	for row in range(n - 1, -1, -1):
		for col in range(m - 1, -1, -1):
			if col == m - 1 and rows[row][col] == '.':
				dp[row][col] = x
				continue
			if rows[row][col] == '*':
				dp[row][col] = dp[row][col + 1]
			elif rows[row][col + 1] == '*':
				dp[row][col] = dp[row][col + 1] + x
			else:
				dp[row][col] = min(dp[row][col + 1] + x, dp[row][col + 2] + y if col + 2 < m else y)
	return sum([dp[i][0] for i in range(n)])
for _ in range(int(input())):
	print(solve())


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	for i in range(n):
		s = input()
		a = s.count('.')
		b = s.count('..')
		p += min(a * x, b * y + (a - 2 * b) * x)
	print(p)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	res = 0
	for i in range(n):
		flag = False
		st = input()
		for item in st:
			if item == '.':
				if 2 * x < y:
					res += x
				elif flag:
					res += y
					flag = False
				else:
					flag = True
			elif flag:
				res += x
				flag = False
		if flag:
			res += x
			flag = False
	print(res)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	ans = 0
	for i in range(n):
		num = l[i].count('.')
		boo = False
		n2 = 0
		for j in range(m):
			if l[i][j] == '.':
				if boo:
					n2 += 1
					boo = False
				else:
					boo = True
			else:
				boo = False
		ans += min(num * x, (num - 2 * n2) * x + n2 * y)
	print(ans)


def f(n, m, x, y, A):
	if x * 2 <= y:
		y = 2 * x
	price = 0
	for i in range(n):
		A[i] = A[i] + '*'
		j = 0
		while j < m:
			if A[i][j] == '.':
				if A[i][j + 1] == '.':
					price += y
					j += 2
				else:
					price += x
					j += 1
			else:
				j += 1
	return price
t = int(input())
for i in range(t):
	[n, m, x, y] = input().split(' ')
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	A = []
	for j in range(n):
		A.append(input())
	print(f(n, m, x, y, A))


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	sqr = []
	for i in range(n):
		sqr.append(input())
	flag = False
	if x <= y / 2:
		flag = True
	price = 0
	for row in sqr:
		i = 0
		while i < m:
			if row[i] == '*':
				i += 1
			elif flag:
				price += x
				i += 1
			elif i + 1 < m and row[i + 1] == '.':
				price += y
				i += 2
			else:
				price += x
				i += 1
	print(price)


def solve(n, m, x, y, a):
	res = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				if j < m - 1:
					if a[i][j + 1] == '*':
						a[i][j] = '*'
						res += x
						continue
					(a[i][j], a[i][j + 1]) = ('*', '*')
					if 2 * x <= y:
						res += 2 * x
					else:
						res += y
				else:
					a[i][j] = '*'
					res += x
	return res

def main():
	t = int(input())
	ans = [0] * t
	for i in range(t):
		(n, m, x, y) = [int(i) for i in input().split()]
		a = [list(input()) for i in range(n)]
		ans[i] = solve(n, m, x, y, a)
	print('\n'.join(map(str, ans)))
main()


a = int(input())
for _ in range(a):
	(b, c, x, y) = map(int, input().split())
	total = 0
	for _ in range(b):
		z = list(input())
		i = 0
		while i < c:
			if z[i] == '*':
				i += 1
				pass
			elif i == c - 1:
				if z[i] == '.':
					total += x
				break
			elif z[i + 1] == '.' and y <= 2 * x:
				total += y
				i += 2
			elif z[i + 1] == '.':
				total += 2 * x
				i += 2
			else:
				total += x
				i += 2
	print(total)


import os
import heapq
import sys
import math
from collections import defaultdict
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		matrix = []
		fill = []
		for i in range(n):
			lis = input()
			matrix.append(lis)
		res1 = 0
		res2 = 0
		for i in range(n):
			j = 0
			while j < m:
				if j + 1 < m and matrix[i][j] == '.':
					if matrix[i][j + 1] == '.':
						res1 += y
						j += 2
						res2 += x * 2
					else:
						res1 += x
						res2 += x
						j += 1
				elif matrix[i][j] == '.':
					res1 += x
					res2 += x
					j += 1
				else:
					j += 1
		print(min(res1, res2))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	A = []
	for i in range(n):
		tempa = input()
		A.append(tempa[:])
	count2 = 0
	count1 = 0
	for i in range(n):
		two_dot = 0
		for j in range(m):
			if A[i][j] == '.':
				count1 += 1
				two_dot += 1
				if two_dot == 2:
					count2 += 1
					two_dot = 0
			else:
				two_dot = 0
	if 2 * x <= y:
		print(x * count1)
	else:
		print(count2 * y + (count1 - count2 * 2) * x)


import math
import collections
for _ in range(0, int(input())):
	(m, n, f, s) = map(int, input().split())
	l = []
	for i in range(0, m):
		u = input()
		l.append(list(u))
	ans = 0
	for i in range(0, m):
		for j in range(0, n):
			if l[i][j] == '.':
				if j + 1 < n and l[i][j + 1] == '.':
					ans += min(2 * f, s)
					l[i][j + 1] = '*'
				else:
					ans += f
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	fi = []
	for _ in range(n):
		te = input()
		a += [te]
		l = []
		ct = 0
		fl = 0
		for i in range(m):
			if te[i] == '.':
				ct += 1
				fl = 0
			else:
				if fl == 0 and ct != 0:
					l += [ct]
				fl = 1
				ct = 0
		if fl == 0 and ct != 0:
			l += [ct]
		fi += [l]
	price = 0
	if 2 * x <= y:
		y = 2 * x
	for i in range(len(fi)):
		for j in range(len(fi[i])):
			if fi[i][j] == 1:
				price += x
			else:
				t1 = fi[i][j] // 2
				t2 = fi[i][j] % 2
				price += t1 * y + t2 * x
	print(price)


import math
t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	m = int(l[1])
	x = int(l[2])
	y = int(l[3])
	l = []
	for i in range(n):
		s = input()
		to = [i for i in s]
		l.append(to)
	y = min(2 * x, y)
	cost = 0
	for i in range(n):
		for j in range(m):
			if l[i][j] == '.' and j != m - 1 and (l[i][j + 1] == '.'):
				cost += y
				l[i][j + 1] = '*'
			elif l[i][j] == '.':
				cost += x
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	flag = 1
	ans = 0
	if 2 * x > y:
		flag = 0
	for i in range(n):
		arr = list(input())
		if flag:
			for j in arr:
				if j == '.':
					ans += x
		else:
			(s, e, flag_) = (0, 0, 0)
			for j in range(m):
				if arr[j] == '.':
					e = j + 1
					if j == m - 1:
						ans += (e - s) // 2 * y + (e - s) % 2 * x
				else:
					ans += (e - s) // 2 * y + (e - s) % 2 * x
					s = j + 1
					e = s
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	d = {}
	d[2] = 0
	d[1] = 0
	for j in range(n):
		count = 0
		s = str(input())
		for i in s:
			if i == '.':
				count += 1
			else:
				d[2] += count // 2
				d[1] += count % 2
				count = 0
		d[2] += count // 2
		d[1] += count % 2
	if 2 * x < y:
		ans = d[2] * x * 2 + d[1] * x
	else:
		ans = d[2] * y + d[1] * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	t = 0
	for i in range(n):
		a.append(input())
	for row in a:
		cnt = 0
		for char in row:
			if char == '.':
				cnt += 1
			else:
				if cnt >= 2:
					if y < x * 2:
						t += cnt // 2 * y + cnt % 2 * x
					else:
						t += cnt * x
				elif cnt > 0:
					t += x
				cnt = 0
		if cnt >= 2:
			if y < x * 2:
				t += cnt // 2 * y + cnt % 2 * x
			else:
				t += cnt * x
		elif cnt > 0:
			t += x
	print(t)


t = int(input())
for i in range(t):
	til = []
	cost = 0
	most = 0
	url = False
	(n, m, x, y) = map(int, input().split())
	for a in range(n):
		til.extend((str(i) for i in input()))
	for f in range(n):
		for l in range(m):
			if l == 0:
				url = False
			if til[f * m + l] == '.':
				most += x
				if url == False:
					url = True
					cost += x
				else:
					url = False
					cost += y - x
			else:
				url = False
	if cost >= most:
		print(most)
	else:
		print(cost)


nb_cases = int(input())
reponses = []
for case in range(nb_cases):
	(n, m, prix11, prix12) = [int(x) for x in input().split()]
	total_price = 0
	simple = 0
	double = 0
	for x in range(n):
		line = input()
		precedent = '*'
		for y in range(m):
			if line[y] == '*':
				precedent = '*'
				continue
			elif line[y] == '.' and precedent == '*':
				simple += 1
				precedent = '.'
			elif line[y] == '.' and precedent == '.':
				simple -= 1
				double += 1
				precedent = '*'
	reponses.append(min(simple * prix11 + double * prix12, (simple + 2 * double) * prix11))
for x in reponses:
	print(x)


def solve():
	(n, m, cx, cy) = map(int, input().split())
	priority = 1 if cx * 2 <= cy else 2
	result = 0
	for y in range(n):
		string = list(input())
		for x in range(m):
			if string[x] == '.':
				if priority == 1 or x >= m - 1:
					result += cx
				elif string[x + 1] == '.':
					string[x + 1] = '*'
					result += cy
				else:
					result += cx
	print(result)

def main():
	for _ in range(int(input())):
		solve()
main()


def func(s, m, x, y):
	single = s.count('.')
	pair = s.count('..')
	if m == 1:
		cost = single * x
	else:
		cost1 = pair * y + (single - pair * 2) * x
		cost2 = single * x
		if cost1 > cost2:
			cost = cost2
		else:
			cost = cost1
	return cost
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	s = ''
	for i in range(n):
		s += input()
		s += '*'
	print(func(s, m, x, y))


def main():
	t = int(input())
	for i in range(t):
		(n, m, x, y) = map(int, input().split())
		square = [input() for i in range(n)]
		price = 0
		for i in range(n):
			last = '*'
			for j in range(m):
				if last == '*':
					last = square[i][j]
				elif square[i][j] == '.':
					price += min(x * 2, y)
					last = '*'
				else:
					price += x
					last = '*'
			if last == '.':
				price += x
		print(price)
main()


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
import bisect

def process(square, N, M, X, Y):
	derivative = Y - 2 * X
	total = 0
	for i in range(N):
		count = 0
		for j in range(M):
			if j == M - 1:
				if square[i][j] == '.':
					count += 1
					if derivative <= 0:
						a = count // 2
						remaining = count - 2 * a
						total += Y * a + remaining * X
					elif derivative > 0:
						a = 0
						remaining = count - 2 * a
						total += Y * a + remaining * X
					count = 0
				elif square[i][j] == '*':
					if derivative <= 0:
						a = count // 2
						remaining = count - 2 * a
						total += Y * a + remaining * X
					elif derivative > 0:
						a = 0
						remaining = count - 2 * a
						total += Y * a + remaining * X
					count = 0
			elif square[i][j] == '.':
				count += 1
			elif square[i][j] == '*':
				if derivative <= 0:
					a = count // 2
					remaining = count - 2 * a
					total += Y * a + remaining * X
				elif derivative > 0:
					a = 0
					remaining = count - 2 * a
					total += Y * a + remaining * X
				count = 0
	return total
test_cases = inp()
for i in range(test_cases):
	(N, M, X, Y) = inlt()
	square = []
	for i in range(N):
		square.append(input())
	print(process(square, N, M, X, Y))


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().strip().split(' ')))
	theatre = [[''] * m] * n
	ret = 0
	for j in range(n):
		theatre[j] = input()
	if y >= 2 * x:
		ret = x * sum([r.count('.') for r in theatre])
	else:
		for j in range(n):
			end = 0
			for k in range(m):
				if theatre[j][k] == '.':
					end += 1
				if end == 2:
					ret += y
					end = 0
				if theatre[j][k] == '*' or k == m - 1:
					ret += x * end
					end = 0
	print(ret)


t = int(input())
ans = [0] * t
for i in range(t):
	(n, m, p1, p2) = map(int, input().split())
	a = [0] * n
	for j in range(n):
		a[j] = list(input())
	if p1 <= p2 // 2:
		for j in range(n):
			ans[i] += p1 * a[j].count('.')
	else:
		for j in range(n):
			for k in range(m):
				if a[j][k] == '.':
					if k < m - 1:
						if a[j][k + 1] == '.':
							ans[i] += p2
							a[j][k] = '*'
							a[j][k + 1] = '*'
						else:
							a[j][k] = '*'
							ans[i] += p1
					else:
						a[j][k] = '*'
						ans[i] += p1
for i in range(t):
	print(int(ans[i]))


t = int(input())
while t:
	t -= 1
	(rows, cols, cost1, cost2) = input().split()
	(r, c, c1, c2) = (int(rows), int(cols), int(cost1), int(cost2))
	matrix = []
	for i in range(r):
		matrix.append(list(input()))
	no_of_two = 0
	no_of_one = 0
	for i in range(r):
		j = 0
		while j < c:
			if j + 1 < c and matrix[i][j] == '.' and (matrix[i][j + 1] == '.'):
				no_of_two += 1
				j += 1
			elif matrix[i][j] == '.':
				no_of_one += 1
			j += 1
	if no_of_two == 0:
		print(no_of_one * c1)
	else:
		cost = 0
		if 2 * c1 >= c2:
			cost += no_of_two * c2
		else:
			cost += 2 * no_of_two * c1
		cost += no_of_one * c1
		print(cost)


n = int(input())
for _ in range(n):
	(n, m, x, y) = [int(s) for s in input().split()]
	ans = 0
	if 2 * x <= y:
		for i in range(n):
			ans += input().count('.') * x
	else:
		a = [input() for i in range(n)]
		for i in range(n):
			num1 = a[i].count('.')
			num2 = a[i].count('..')
			ans += num2 * y + (num1 - 2 * num2) * x
	print(ans)


t = int(input())
for test in range(t):
	(n, m, x, y) = map(int, input().split())
	grid = []
	cost = 0
	for _ in range(n):
		grid.append(input())
	if y < 2 * x and m > 1:
		for r in range(n):
			c = 0
			while c < m:
				while c < m and grid[r][c] == '*':
					c += 1
				if c < m - 1 and grid[r][c] == grid[r][c + 1] == '.':
					cost += y
					c += 2
				elif c < m and grid[r][c] == '.':
					cost += x
					c += 1
				else:
					c += 1
	else:
		for r in range(n):
			for c in range(m):
				if grid[r][c] == '.':
					cost += x
	print(cost)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	(n, m, a, b) = zz()
	(one, two) = (0, 0)
	for _ in range(n):
		l = z().split('*')
		for i in l:
			i = i.strip()
			p = len(i)
			two += p // 2
			one += p % 2
	if 2 * a < b:
		print((one + two * 2) * a)
	else:
		print(one * a + two * b)


t = int(input())
for _ in range(t):
	(n, m, x, y) = input().split()
	arr = []
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	for i in range(n):
		r = input()
		a = []
		for i in range(m):
			a.append(r[i])
		arr.append(a)
		a = []
	maxi = 0
	cur = 0
	for i in range(n):
		j = 0
		while j < m:
			if arr[i][j] == '.':
				if j < m - 1 and arr[i][j + 1] == '.':
					maxi += min(x * 2, y)
					j = j + 1
				else:
					maxi = maxi + x
			j = j + 1
	print(maxi)


from sys import stdin, stdout
import math
T = int(stdin.readline())
for i in range(T):
	(n, m, x, y) = map(int, stdin.readline().split())
	double_white = 0
	whites = 0
	for i in range(n):
		row = stdin.readline()
		prev = False
		for tile in row:
			if tile == '.':
				whites += 1
				if prev == True:
					double_white += 1
					prev = False
				else:
					prev = True
			else:
				prev = False
	if 2 * x <= y:
		out = whites * x
	else:
		out = double_white * y + (whites - 2 * double_white) * x
	stdout.write(str(out) + '\n')


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve_row(x, y):
	row = input()
	if x * 2 <= y:
		return x * row.count('.')
	cost = 0
	i = 0
	while i < len(row) - 1:
		if row[i] == '*':
			i += 1
			continue
		elif row[i + 1] == '.':
			cost += y
			i += 2
		else:
			cost += x
			i += 2
	if i == len(row) - 1 and row[-1] == '.':
		cost += x
	return cost

def solve():
	(n, m, x, y) = rl()
	answer = 0
	for i in range(n):
		answer += solve_row(x, y)
	print(answer)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	(n, m, x, y) = iis()
	sq = []
	for i in range(n):
		a = input().strip()
		sq.append([])
		for c in a:
			sq[i].append(c)
	count = 0
	if y > 2 * x:
		y = 2 * x
	for i in range(len(sq)):
		for j in range(len(sq[i])):
			if sq[i][j] == '.':
				if j < len(sq[i]) - 1:
					if sq[i][j + 1] == '.':
						count += y
						sq[i][j + 1] = '*'
					else:
						count += x
				else:
					count += x
	print(count)


import sys
import heapq
import random
import collections
import math

def solve(x, y, grid):
	console('----- solving ------')
	console(x, y, grid)
	if 2 * x <= y:
		return sum([row.count('.') for row in grid]) * x
	res_1 = 0
	res_2 = 0
	for row in grid:
		for segment in row.split('*'):
			res_1 += len(segment) % 2
			res_2 += len(segment) // 2
	return res_1 * x + res_2 * y

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	(nrows, _, x, y) = list(map(int, input().split()))
	grid = []
	for _ in range(nrows):
		grid.append(input())
	res = solve(x, y, grid)
	print(res)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(N, M, X, Y) = [int(x) for x in input().split()]
		S = [input().strip() for _ in range(N)]
		if X * 2 <= Y:
			ans = 0
			for j in range(N):
				ans += S[j].count('.')
			print(ans * X)
		else:
			s = 0
			for j in range(N):
				ansy = S[j].count('..')
				ansx = S[j].count('.')
				ansx -= ansy * 2
				s += ansx * X + ansy * Y
			print(s)
main()


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = []
	for i in range(n):
		t = list(input())
		p.append(t)
	if 2 * x <= y:
		r = 2 * x
	else:
		r = y
	price = 0
	cnt = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if p[i][j] == '.':
				cnt += 1
			elif p[i][j] == '*':
				if cnt > 0:
					price += r * (cnt // 2)
					if cnt % 2 != 0:
						price += x
					cnt = 0
		if cnt > 0:
			price += r * (cnt // 2)
			if cnt % 2 != 0:
				price += x
	print(price)


def min_cost(n, m, x, y, arr):
	cost = 0
	for i in range(n):
		j = 0
		while j < m:
			if arr[i][j] == '.':
				next = j + 1
				if next < m and arr[i][next] == '.':
					j = j + 2
					cost1 = 2 * x
					cost2 = y
					cost += min(cost1, cost2)
					continue
				cost += x
				j = j + 1
			else:
				j += 1
	return cost
T = int(input())
for t in range(T):
	numbers = list(map(int, input().split()))
	(n, m, x, y) = (numbers[0], numbers[1], numbers[2], numbers[3])
	mat = []
	for i in range(n):
		mat.append(input())
	ans = min_cost(n, m, x, y, mat)
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	t = 0
	t_temp = 0
	for i in range(n):
		l = list(input())
		if len(l) == 1 and l[0] == '.':
			t += x
			t_temp += x
		else:
			(c, j) = (0, 0)
			while j < len(l) - 1:
				if l[j] == '.':
					if l[j + 1] == '.':
						c += y
						j += 1
					else:
						c += x
				j += 1
			t += c
			if j == len(l) - 1 and l[j] == '.':
				t += x
			t_temp += x * l.count('.')
	print(min(t, t_temp))


import sys
from collections import defaultdict as dd
import heapq
import math
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	q = []
	for j in range(n):
		q.append(list(input()))
	a = 0
	if y < 2 * x:
		for j in range(n):
			for k in range(m):
				if q[j][k] == '.':
					if k < m - 1 and q[j][k + 1] == '.':
						a += y
						q[j][k + 1] = '*'
					else:
						a += x
					q[j][k] = '*'
	else:
		for j in range(n):
			for k in range(m):
				if q[j][k] == '.':
					a += x
	print(a)


from math import sqrt
t = int(input())
for q in range(0, t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(0, n):
		a.append(input())
	ans1 = 0
	for i in range(0, n):
		for j in range(0, m):
			if a[i][j] == '.':
				ans1 += x
	ans2 = 0
	used = []
	for i in range(0, n):
		used.append([])
		for j in range(0, m):
			used[i].append(False)
	for i in range(0, n):
		for j in range(0, m - 1):
			if a[i][j + 1] == a[i][j] == '.' and (not used[i][j + 1]) and (not used[i][j]):
				ans2 += y
				used[i][j] = True
				used[i][j + 1] = True
	for i in range(0, n):
		for j in range(0, m):
			if a[i][j] == '.' and (not used[i][j]):
				ans2 += x
	print(min(ans1, ans2))


from collections import deque
t = int(input())
while t:
	(n, m, x, y) = map(int, input().split())
	a = x
	b = y
	ll = []
	if 2 * a < b:
		b = 2 * x
	ans = 0
	for i in range(n):
		l = list(input().strip())
		ll.append(l)
		j = 0
		while j < m:
			if ll[i][j] == '.':
				if j + 1 < m and ll[i][j + 1] == '.':
					ans += b
					j += 1
				else:
					ans += a
			j += 1
	print(ans)
	t -= 1


def main():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = list(map(int, input().split()))
		square = [input()[:-1] for i in range(n)]
		counter = 0
		for i in range(n):
			for j in range(m):
				counter += int(square[i][j] == '.')
		if 2 * x <= y:
			print('{0}\n'.format(counter * x))
		else:
			counter_two = 0
			for i in range(n):
				counter_two += square[i].count('..')
			print('{0}\n'.format(counter_two * y + (counter - counter_two * 2) * x))
main()


T = int(input().strip())

def cover(row, x, y):
	if 2 * x <= y:
		return x * sum(row)
	res = 0
	for j in row:
		res += y * (j // 2) + x * (j % 2)
	return res
for t in range(T):
	(n, m, x, y) = list(map(int, input().split()))
	cost = 0
	for _ in range(n):
		row = list(map(len, input().strip().split('*')))
		cost += cover(row, x, y)
	print(cost)


import sys
import string
input = sys.stdin.readline
import math
l = int(input())
for i in range(l):
	n = list(map(int, input().split()))
	total = 0
	for x in range(n[0]):
		row = input()
		li = []
		counter = 0
		for char in row:
			if char == '.':
				counter += 1
			else:
				if n[2] * 2 >= n[3] and counter == 1:
					total += n[2]
				counter = 0
			if n[2] * 2 < n[3]:
				if counter == 1:
					total += n[2]
					counter = 0
			elif counter == 2:
				total += n[3]
				counter = 0
	print(total)


t = int(input())
for _ in range(t):
	(n, m, x, y) = (int(i) for i in input().split())
	a = [input() for i in range(n)]
	cost = 0
	if 2 * x < y:
		for i in a:
			cost += i.count('.') * x
		print(cost)
		continue
	for i in a:
		single = i.count('.')
		double = i.count('..')
		cost += double * y + (single - 2 * double) * x
	print(cost)


tc = int(input())
for ca in range(tc):
	(n, m, x, y) = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		s = input()
		cnt = 0
		for j in range(m):
			if s[j] == '.':
				cnt += 1
			else:
				ans += min(x * cnt, cnt // 2 * y + (cnt & 1) * x)
				cnt = 0
		ans += min(x * cnt, cnt // 2 * y + (cnt & 1) * x)
	print(ans)


def solve(n, m, x, y, l):
	if y >= 2 * x:
		c = 0
		for i in l:
			c += i.count('.')
		return c * x
	else:
		c = 0
		if m == 1:
			return l.count('.') * x
		for i in l:
			g = i.count('..')
			c += g * y
			c += (i.count('.') - 2 * g) * x
		return c
t = int(input())
for we in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	print(solve(n, m, x, y, l))


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	go = 0
	price = 0
	grid = []
	if x * 2 < y:
		go = 1
	for j in range(n):
		l = input()
		grid.append(l)
	if go == 1:
		for j in range(n):
			for l in range(m):
				if grid[j][l] == '.':
					price += x
	else:
		for j in range(n):
			consec = 0
			ind = 0
			wCount = grid[j].count('.')
			while ind < m - 1:
				if grid[j][ind] == '.' and grid[j][ind + 1] == '.':
					consec += 1
					ind += 1
				ind += 1
			price += consec * y
			wCount -= consec * 2
			price += wCount * x
	print(price)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	k = []
	t = []
	for i in range(n):
		k.append(str(input()))
		t.append(k[i].split('*'))
	yy = 0
	xx = 0
	l = 0
	for i in range(len(t)):
		for j in range(len(t[i])):
			f = len(t[i][j])
			l = l + f
			if f == 0:
				pass
			elif f % 2 == 0:
				z = f // 2
				yy = yy + z
			else:
				z = f // 2
				yy = yy + z
				xx = xx + 1
	else:
		print(min(xx * x + yy * y, l * x))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c = 0
	if y <= 2 * x:
		for _ in range(n):
			curr = input()
			d = curr.count('..')
			s = curr.count('.')
			c += y * d
			curr = curr.replace('..', '')
			c += x * curr.count('.')
	else:
		for _ in range(n):
			curr = input()
			c += x * curr.count('.')
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	for i in range(n):
		twos = 0
		ones = 0
		a = input()
		j = 0
		while j < m:
			if a[j] == '.':
				if j + 1 != m:
					if a[j + 1] == '.':
						twos += 1
						j += 1
					else:
						ones += 1
				else:
					ones += 1
			j += 1
		if y <= 2 * x:
			cost += ones * x + twos * y
		else:
			cost += x * (ones + twos * 2)
	print(cost)


T = int(input())
for t_itr in range(T):
	(N, M, X, Y) = list(map(int, input().rstrip().split()))
	F = []
	for _ in range(N):
		F.append(input())
	result = 0
	if Y > 2 * X:
		Y = 2 * X
	for f in F:
		big = False
		for l in range(len(f)):
			if f[l] == '.':
				if big:
					result += Y
					big = False
				else:
					big = True
			elif big:
				result += X
				big = False
		if big:
			result += X
	print(result)


t = int(input())
while t:
	(m, n, p, q) = [int(x) for x in input().split()]
	r = []
	for i in range(m):
		s = input()
		l = s.split('*')
		for j in l:
			if j:
				r.append(len(j))
	cost = 0
	for l in r:
		two = l // 2
		one = l % 2
		cost += one * p + two * 2 * min(p, q / 2)
	print(int(cost))
	t -= 1


for x in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	p = 0
	for i in range(n):
		s = input()
		s += '*'
		j = 0
		while j < m:
			if s[j] == '.' and s[j + 1] == '.':
				if 2 * x < y:
					p += 2 * x
				else:
					p += y
				j += 2
			elif s[j] == '.' and s[j + 1] != '.':
				p += x
				j += 2
			else:
				j += 1
	print(p)


num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c = 0
	for q in range(n):
		s = input()
		a = s.count('..')
		b = s.count('.')
		if y < 2 * x:
			c += a * y + (b - 2 * a) * x
		else:
			c += b * x
	print(c)
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


import math
TT = int(input())
for _ in range(TT):
	(n, m, x, y) = map(int, input().split())
	mp = []
	for i in range(n):
		l = input()
		mp.append(l)
	ans = 0
	for i in range(n):
		con = 0
		for j in range(m):
			if mp[i][j] == '.':
				con += 1
			else:
				ans += min(int(con // 2) * y, int(con // 2) * x * 2) + int(con % 2) * x
				con = 0
		ans += min(int(con // 2) * y, int(con // 2) * x * 2) + int(con % 2) * x
	print(ans)


import math

def solve():
	(n, m, x, y) = list(map(int, input().split()))
	if m == 1:
		grid = [input().split() for _ in range(n)]
	else:
		grid = [[ch for ch in input()] for _ in range(n)]
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if grid[i][j] == '.':
				if j < m - 1 and grid[i][j + 1] == '.':
					if x * 2 > y:
						ans += y
					else:
						ans += x * 2
					j += 1
				else:
					ans += x
			j += 1
	print(ans)

def main():
	T = int(input())
	while T:
		T -= 1
		solve()
main()


for z in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		s = input()
		array = []
		for k in range(len(s)):
			array.append(s[k])
		if 2 * x <= y:
			for j in range(m):
				if array[j] == '.':
					ans += x
		else:
			for j in range(m - 1):
				if array[j] == '.' and array[j + 1] == '.':
					ans += y
					array[j] = '*'
					array[j + 1] = '*'
				elif array[j] == '.':
					ans += x
			if array[m - 1] == '.':
				ans += x
	print(ans)


from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		a = input()
		c = 0
		for j in range(m + 1):
			if j == m or a[j] == '*':
				if c % 2 == 0:
					ans += min(c * x, c // 2 * y)
				else:
					ans += min(c * x, c // 2 * y + x)
				c = 0
			else:
				c += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().rstrip().split())
	priceForDouble = min(2 * x, y)
	arr = []
	cost = 0
	for row in range(n):
		rowArr = list(input())
		if len(rowArr) == 1 and rowArr[0] == '.':
			cost += x
		for col in range(1, m):
			if col + 1 < m and rowArr[col] == '.' and (rowArr[col - 1] == '*') and (rowArr[col + 1] == '.'):
				continue
			if rowArr[col] == '.' and rowArr[col - 1] == '.':
				cost += priceForDouble
			elif rowArr[col] == '.' or rowArr[col - 1] == '.':
				cost += x
			(rowArr[col], rowArr[col - 1]) = ('*', '*')
	print(cost)


T = int(input())
import math
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for _ in range(n):
		a = input()
		a = a + '*'
		s = []
		p = 0
		for j in range(m + 1):
			if a[j] == '.':
				p += 1
			else:
				s.append(p)
				p = 0
		a = 0
		for i in s:
			if i % 2 == 1:
				ans += min(i // 2 * y + x, i * x)
			else:
				ans += min(i // 2 * y, i * x)
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append([x for x in input()])
	if x <= y // 2:
		y = 2 * x
	c = 0
	for i in range(n):
		j = 0
		while j < m:
			if j < m - 1:
				if l[i][j] == '.' and l[i][j + 1] == '.':
					c += y
					j += 1
				elif l[i][j] == '.':
					c += x
			elif l[i][j] == '.':
				c += x
			j += 1
	print(c)


rr = lambda : input().strip()
rrm = lambda : map(int, rr().split())
T = int(rr())
for _ in range(T):
	(n, m, x, y) = rrm()
	cost = 0
	if x * 2 <= y:
		for i in range(n):
			temp = rr()
			cost += temp.count('.') * x
	else:
		for i in range(n):
			temp = rr()
			a = temp.count('..')
			b = temp.count('.')
			sin = 0
			X = b - a * 2
			if X > 0:
				sin = x * X
			cost += a * y + sin
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	data = []
	white_count = 0
	for i in range(n):
		temp = input()
		data.append(temp)
		white_count += temp.count('.')
	if x <= y // 2:
		print(white_count * x)
		continue
	adj_white_count = 0
	for i in range(n):
		start = 0
		if data[i][0] == '.':
			start = 1
		else:
			start = 0
		for j in range(1, m):
			if data[i][j] == '.':
				if start == 1:
					adj_white_count += 1
					start = 0
				else:
					start = 1
			else:
				start = 0
	print(adj_white_count * y + (white_count - adj_white_count * 2) * x)


import math
for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	l = []
	for i in range(n):
		l1 = list(input())
		l.append(l1)
	if 2 * a <= b:
		t = 0
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					t += 1
		print(a * t)
	else:
		c = 0
		for i in range(n):
			j = 0
			for j in range(m - 1):
				if l[i][j] == '.' and l[i][j + 1] == '.':
					c += b
					l[i][j + 1] = '*'
				elif l[i][j] == '.' and l[i][j + 1] != '.':
					c += a
			if l[i][m - 1] == '.':
				c += a
		print(c)


def solve():
	(n, m, x, y) = map(int, input().split())
	y = min(2 * x, y)
	cost = 0
	for _ in range(n):
		r = input()
		f = False
		for p in r:
			if f:
				if p == '.':
					cost -= x
					cost += y
				f = False
			elif p == '.':
				cost += x
				f = True
	print(cost)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for x in range(t):
	(n, m, a, b) = map(int, input().split())
	arr = [[j for j in input().strip()] for i in range(n)]
	f = 0
	if a * 2 <= b:
		f = 1
	if n == m == 1 and arr[0][0] == '.':
		print(a)
	else:
		ans = f1 = 0
		for i in range(n):
			for j in range(m):
				if f1 == 1:
					f1 = 0
					continue
				if f == 1:
					if arr[i][j] == '.':
						ans += a
				elif j + 1 < m and arr[i][j] == arr[i][j + 1] == '.':
					ans += b
					f1 = 1
				elif arr[i][j] == '.':
					ans += a
		print(ans)


tq = int(input())
for x in range(tq):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for ts in range(n):
		arr.append(list(input()))
	sor = 0
	for ps in range(n):
		tp = 0
		while tp < m:
			if tp < m - 1 and arr[ps][tp] == '.' and (arr[ps][tp + 1] == '.') and (y < 2 * x):
				sor += y
				tp += 2
			elif arr[ps][tp] == '.':
				sor += x
				tp += 1
			else:
				tp += 1
	print(sor)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(list(input().strip()))
	cost = 0
	for arrj in arr:
		i = 0
		m = len(arrj)
		while i < m:
			if i == m - 1:
				break
			if arrj[i] == '.' and arrj[i + 1] == '.':
				cost += min(2 * x, y)
				i += 2
			elif arrj[i] == '.':
				cost += x
				i += 1
			else:
				i += 1
		if i == m - 1:
			cost += x if arrj[i] == '.' else 0
	print(cost)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return list(sys.stdin.readline().strip())
t = II()
for q in range(t):
	(n, m, x, y) = MI()
	l = [[0] * (m + 1)]
	for z in range(n):
		l.append([0] + SI())
	isFirst = 2 * x <= y
	cost = 0
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if l[i][j] == '.':
				if isFirst:
					cost += x
					l[i][j] = '*'
				elif j < m and l[i][j + 1] == '.':
					cost += y
					l[i][j] = '*'
					l[i][j + 1] = '*'
				else:
					cost += x
					l[i][j] = '*'
	print(cost)


def solve():
	(n, m, x, y) = list(map(lambda x: int(x), input().split()))
	ans = 0
	for i in range(n):
		row = input()
		if y >= 2 * x:
			ans += x * row.count('.')
		else:
			walk = 0
			while walk < len(row):
				if row[walk] != '.':
					walk += 1
				elif walk + 1 < len(row) and row[walk + 1] == '.':
					ans += y
					walk += 2
				else:
					ans += x
					walk += 1
	return ans
cases = int(input())
ans = []
for case in range(cases):
	ans.append(solve())
for el in ans:
	print(el)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	visited = set()
	if y < 2 * x:
		good = True
	else:
		good = False
	ans = 0
	for i in range(n):
		L = list(input())
		(ans1, ans2) = (0, 0)
		k = 0
		while k < m:
			if k + 1 < m and L[k] == '.' and (L[k + 1] == '.'):
				if good:
					ans1 += y
				else:
					ans1 += 2 * x
				k += 2
			elif L[k] == '.':
				ans1 += x
				k += 1
			else:
				k += 1
		for k in range(m):
			if L[k] == '.':
				ans2 += x
		ans += min(ans1, ans2)
	print(ans)


for t in range(int(input())):
	(n, m, one, two) = map(int, input().split())
	board = []
	for i in range(n):
		board.append(list(input()))
	two = min(2 * one, two)
	cost = 0
	for i in range(n):
		for j in range(m):
			if board[i][j] == '.':
				if j + 1 < m and board[i][j + 1] == '.':
					cost += two
					board[i][j] = '*'
					board[i][j + 1] = '*'
				else:
					cost += one
					board[i][j] = '*'
	print(cost)


ll = lambda : list(map(int, input().split()))
s = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
import math
for _ in range(int(input())):
	(n, m, x, y) = v()
	s1 = 0
	s2 = 0
	for i in range(n):
		s = input()
		c = 0
		for i1 in range(len(s)):
			if s[i1] == '.':
				c = c + 1
			if s[i1] != '.' or i1 == len(s) - 1:
				s2 = s2 + c // 2
				s1 = s1 + c % 2
				c = 0
	if 2 * x < y:
		print(s2 * 2 * x + s1 * x)
	else:
		print(s2 * y + s1 * x)


from sys import stdin, stdout
import collections
import heapq
import itertools
import functools
import math
import operator
import string
ALPHABET = string.ascii_lowercase
DIGITS = string.digits

def read_line():
	return stdin.readline().strip()

def read_int():
	return int(read_line())

def read_arr():
	return read_line().split(' ')

def read_int_arr():
	return [int(x) for x in read_arr()]

def query_interactive(s):
	print(s)
	stdout.flush()

def solve():
	(n, m, x, y) = read_int_arr()
	y = min(y, x * 2)
	mat = [read_line() for _ in range(n)]
	res = 0
	for row in mat:
		cur_l = 0
		for c in row:
			if c == '.':
				cur_l += 1
			else:
				res += cur_l % 2 * x + cur_l // 2 * y
				cur_l = 0
		else:
			res += cur_l % 2 * x + cur_l // 2 * y
			cur_l = 0
	print(res)

def main():
	T = 1
	T = read_int()
	for _ in range(T):
		solve()
main()


for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	if x + x <= y:
		priority = 1
	else:
		priority = 2
	k = 0
	sum_ = 0
	for i in range(n):
		string = list(input())
		for r in range(m):
			if string[r] == '.':
				if k == 0:
					if priority == 1 or r == m - 1:
						sum_ += x
					elif string[r + 1] == '*':
						sum_ += x
					else:
						k += 1
				else:
					sum_ += y
					k = 0
	print(sum_)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	v = [[0 for i in range(m)] for j in range(n)]
	mat = []
	for i in range(n):
		mat.append(input())
	if x < y / 2:
		count = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					count += 1
		ans = x * count
	else:
		ones = 0
		twos = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					ones += 1
					if j > 0 and mat[i][j - 1] == '.' and (v[i][j - 1] == 0):
						ones -= 2
						twos += 1
						v[i][j] = 1
		ans = x * ones + y * twos
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ar = []
	for i in range(n):
		ar.append(list(input()))
	if 2 * x <= y:
		ans = 0
		for i in range(n):
			ans += ar[i].count('.')
		print(x * ans)
	else:
		ans = 0
		for i in range(n):
			j = 0
			ct = 0
			while j < m:
				flg = 1
				while j < m and ar[i][j] == '.':
					ct += 1
					j += 1
					flg = 0
				ans += ct // 2 * y
				ans += ct % 2 * x
				ct = 0
				if flg:
					j += 1
		print(ans)


t = int(input())
for test in range(1, t + 1):
	(n, m, x, y) = map(int, input().rstrip().split())
	a = []
	ones = 0
	twos = 0
	for rows in range(n):
		l = input()
		i = 0
		while i < m:
			if l[i] == '.':
				if i == m - 1:
					ones += 1
					i += 1
				elif l[i + 1] == '.':
					twos += 1
					i += 2
				else:
					ones += 1
					i += 1
			else:
				i += 1
	cost = 0
	if 2 * x * twos > y * twos:
		cost = x * ones + y * twos
	else:
		cost = 2 * x * twos + x * ones
	print(cost)


from sys import stdout, stdin
for testcases in range(int(stdin.readline())):
	(rows, col, ones, twos) = map(int, stdin.readline().split())
	twos = min(ones * 2, twos)
	total = 0
	for k in range(rows):
		string = stdin.readline()
		tiles = 0
		for i in string:
			if i == '.':
				tiles += 1
			else:
				total += twos * (tiles // 2) + ones * (tiles % 2)
				tiles = 0
	stdout.write(str(total) + '\n')


t = int(input())
for _ in range(t):
	(n, m, x, y) = tuple(map(int, input().split()))
	response = 0
	if x <= y / 2:
		for row in range(n):
			response += x * input().count('.')
	else:
		for r in range(n):
			row = input()
			w = row.count('.')
			dup = row.count('..')
			sin = w - 2 * dup
			response += sin * x + dup * y
	print(response)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		total = 0
		for j in range(n):
			l = list(input())
			total += x * l.count('.')
		print(total)
	else:
		total = 0
		for j in range(n):
			(l, i) = (list(input()), 0)
			while i < len(l):
				if l[i] == '*':
					i += 1
					continue
				elif i != len(l) - 1 and l[i] == l[i + 1] == '.':
					total += y
					i += 2
				else:
					total += x
					i += 1
		print(total)


t = input()
t = int(t)
(iteration, row) = (0, 0)
(n, m, x, y, cost) = (0, 0, 0, 0, 0)
while iteration <= t:
	i = input()
	if 48 <= ord(i[0]) <= 57:
		ind = 0
		s = ''
		for l in i:
			s += l
			if l == ' ':
				if ind == 2:
					x = int(s)
					s = ''
				elif ind == 1:
					m = int(s)
					ind += 1
					s = ''
				elif ind == 0:
					n = int(s)
					ind += 1
					s = ''
		y = int(s)
		s = ''
		iteration += 1
	else:
		useY = False
		for l2 in i:
			if 2 * x > y:
				if l2 == '.':
					if useY:
						cost -= x
						cost += y
						useY = False
					else:
						cost += x
						useY = True
				else:
					useY = False
			elif l2 == '.':
				cost += x
		row += 1
		if row == n:
			print(int(cost))
			cost = 0
			row = 0
			if iteration == t:
				break


def sol():
	t = int(input())
	for tt in range(t):
		(n, m, x, y) = map(int, input().split())
		A = []
		for i in range(n):
			A.append(input())
		cnt = 0
		for i in range(n):
			if m == 1:
				if A[i][0] == '.':
					cnt += x
				else:
					cnt += 0
				continue
			if x * 2 <= y:
				for j in range(m):
					if A[i][j] == '.':
						cnt += x
			else:
				A[i] = A[i].replace('..', 'p')
				for j in range(len(A[i])):
					if A[i][j] == 'p':
						cnt += y
					elif A[i][j] == '.':
						cnt += x
		print(cnt)
sol()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	c = 0
	for j in range(n):
		s = input()
		l = s.split('*')
		for i in range(len(l)):
			if l[i] == '.':
				cost += x
			elif l[i] == '..':
				cost += min(2 * x, y)
			else:
				ch = l[i]
				fl = ch.count('.')
				fc = min(2 * x, y)
				if fl % 2 == 0:
					cost = cost + fc * (fl // 2)
				else:
					cost = cost + fc * (fl // 2) + x
	print(cost)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def solve():
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	ans = 0
	for i in range(n):
		ai = input().split('*')
		for aj in ai:
			(q, w) = divmod(len(aj), 2)
			ans += q * y + w * x
	print(ans)
t = int(input())
for i in range(t):
	solve()


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	l = []
	x = []
	cnt = 0
	cnt2 = 0
	for i in range(a):
		x = list(input())
		cnt += x.count('.')
		l.append(x)
	if d > c * 2:
		print(cnt * c)
		continue
	else:
		for j in l:
			k = 0
			while k < len(j) - 1:
				if j[k] + j[k + 1] == '..':
					cnt2 += 1
					k += 1
				k += 1
		cnt = cnt - cnt2 * 2
		print(cnt2 * d + cnt * c)


import sys
input = sys.stdin.readline

def main():
	for _ in range(int(input())):
		matrix = []
		single = 0
		double = 0
		(n, m, x, y) = list(map(int, input().split()))
		ans = 0
		y = min(2 * x, y)
		for i in range(n):
			s = list(input())
			matrix.append(s)
		for j in range(n):
			count = 0
			for i in range(0, m):
				if matrix[j][i] == '.':
					count += 1
				else:
					ans += count // 2 * y + count % 2 * x
					count = 0
			if count != 0:
				ans += count // 2 * y + count % 2 * x
		print(ans)
main()


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	cont = 0
	linha = ''
	for a in range(n):
		linha = str(input())
		b = 0
		while b < len(linha):
			if b < len(linha) - 1 and linha[b] == '.' and (linha[b + 1] == '.'):
				if y <= 2 * x:
					cont += y
				else:
					cont += 2 * x
				b += 2
			elif linha[b] == '.':
				cont += x
				b += 1
			else:
				b += 1
	print(cont)


from sys import stdin
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
for _ in range(I()):
	(n, m, x, y) = R()
	s = []
	c = 0
	for i in range(n):
		s += (list(S()) + ['*'],)
	if 2 * x < y:
		p = 2 * x
	else:
		p = y
	for i in range(n):
		for j in range(m):
			if s[i][j] == s[i][j + 1] == '.':
				c += p
				s[i][j] = '*'
				s[i][j + 1] = '*'
			elif s[i][j] == '.':
				c += x
				s[i][j] = '*'
	print(c)


tests = int(input())
for i in range(tests):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr1 = []
		string = input()
		for z in string:
			if z == '*':
				arr1.append(False)
			else:
				arr1.append(True)
		arr.append(arr1)
	if 2 * x <= y:
		cost = 0
		for k in arr:
			for l in k:
				if l == True:
					cost += x
		print(cost)
	else:
		cost = 0
		for k in arr:
			i = 0
			while i < m:
				if i < m - 1:
					if k[i] == False:
						i += 1
					elif k[i + 1] == True:
						cost += y
						i += 2
					else:
						cost += x
						i += 2
				else:
					if k[i] == True:
						cost += x
					i += 1
		print(cost)


for _ in range(int(input())):
	(n, m, p, o) = list(map(int, input().split()))
	l = []
	a = 0
	b = 0
	for i in range(n):
		s = input()
		b = b + s.count('.')
		l.append(s)
	if 2 * p <= o:
		print(p * b)
	else:
		d = 0
		for i in range(n):
			t = ''
			for j in range(m):
				if l[i][j] == '.':
					if l[i][j] == t:
						t = ''
						d = d + 1
						continue
				t = l[i][j]
		print(d * o + (b - 2 * d) * p)


import sys
import math
import heapq

def I():
	return int(sys.stdin.readline())

def IL():
	return list(map(int, sys.stdin.readline().strip().split()))

def ILS():
	return list(map(str, sys.stdin.readline().strip().split()))
t = I()
for tt in range(t):
	(n, m, x, y) = IL()
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	ans = 0
	if x <= y / 2:
		ans = 0
		for ll in l:
			for ss in ll:
				if ss == '.':
					ans += x
	else:
		for ll in l:
			cnt = 0
			i = 0
			while i < len(ll):
				while i < len(ll) and ll[i] == '.':
					i += 1
					cnt += 1
				ans += cnt // 2 * y + x * (cnt % 2)
				cnt = 0
				i += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = [list(input()) for i in range(n)]
	flag = False
	if y < x * 2:
		flag = True
	ans = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.':
				if j == m - 1:
					ans += x
					arr[i][j] = '*'
				elif arr[i][j + 1] == '.':
					if flag:
						ans += y
						arr[i][j] = '*'
						arr[i][j + 1] = '*'
					else:
						ans += 2 * x
						arr[i][j] = '*'
						arr[i][j + 1] = '*'
				else:
					arr[i][j] = '*'
					ans += x
	print(ans)


for ii in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	q = []
	z = 0
	z1 = 0
	for i in range(n):
		q.append(input())
	for i in q:
		s = i
		ss = 0
		while ss < len(s):
			c = 0
			if s[ss] == '.':
				for jj in range(ss, len(s), +1):
					if s[jj] == '.':
						if jj == len(s) - 1:
							ss = jj + 1
						c += 1
					elif s[jj] == '*':
						ss = jj + 1
						break
				z += c // 2 * y + c % 2 * x
				z1 += c * x
			else:
				ss += 1
	print(min(z, z1))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = [input() for i in range(n)]
	(one, two) = (0, 0)
	for i in range(n):
		one += l[i].count('.')
		two += l[i].count('..')
	print(min(one * x, two * y + (one - two * 2) * x))


testy = int(input())
results = []
for i in range(testy):
	(m, n, x, y) = map(int, input().split())
	plansza = []
	for i in range(m):
		plansza.append(input())
	mincost = 0
	kolejne = 0
	dwojki = 0
	kropki = 0
	if x * 2 <= y:
		for wiersz in plansza:
			for znak in wiersz:
				if znak == '.':
					mincost += x
		results.append(mincost)
	else:
		for wiersz in plansza:
			kolejne = 0
			for znak in wiersz:
				if znak == '.':
					kropki += 1
					kolejne += 1
					if kolejne == 2:
						dwojki += 1
						kolejne = 0
				else:
					kolejne = 0
		results.append(dwojki * y + (kropki - 2 * dwojki) * x)
for result in results:
	print(result)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
q = nn()
for _ in range(q):
	(n, m, x, y) = mi()
	board = []
	cost = 0
	for _ in range(n):
		board.append(li())
	for row in board:
		lens = []
		cur_len = 0
		for c in row:
			if c == '.':
				cur_len += 1
			else:
				lens.append(cur_len)
				cur_len = 0
		lens.append(cur_len)
		for l in lens:
			if l % 2 == 0:
				cost += min(l * x, l // 2 * y)
			else:
				cost += min((l - 1) * x, l // 2 * y) + x
	print(cost)


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	cost = 0
	for i in range(n):
		r = input()
		white = False
		for j in range(m):
			if r[j] == '.':
				if white:
					cost += y
					cost -= x
					white = False
				else:
					cost += x
					white = True
			else:
				white = False
	print(cost)


for i in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for j in range(n):
		a.append(input().split('*'))
	if y >= 2 * x:
		t = 0
		for row in a:
			for col in row:
				t += len(col)
		print(t * x)
	else:
		cost = 0
		for row in a:
			for col in row:
				cost += len(col) // 2 * y
				cost += len(col) % 2 * x
		print(cost)


def solve(arr, x, y):
	if 2 * x <= y:
		cnt = 0
		for row in arr:
			cnt += row.count('.')
		print(cnt * x)
	else:
		ans = 0
		for row in arr:
			cnt = 0
			for r in row:
				if r == '.':
					cnt += 1
				else:
					if cnt & 1:
						ans += x + (cnt - 1) // 2 * y
					else:
						ans += cnt // 2 * y
					cnt = 0
			if cnt:
				if cnt & 1:
					ans += x + (cnt - 1) // 2 * y
				else:
					ans += cnt // 2 * y
		print(ans)
T = int(input())
while T:
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for _ in range(n):
		tmp = list(input())
		arr.append(tmp)
	solve(arr, x, y)
	T -= 1


t = int(input())
for test in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	ans = 0
	for i in range(n):
		temp = list(input())
		j = 0
		while j < m:
			count = 0
			while j < m and temp[j] == '.':
				j += 1
				count += 1
			j += 1
			if count % 2 == 0:
				if y > 2 * x:
					ans += count * x
				else:
					ans += count // 2 * y
			else:
				ans += x
				count -= 1
				if y > 2 * x:
					ans += count * x
				else:
					ans += count // 2 * y
	print(ans)


import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	square = [input() for _ in range(n)]
	if y >= 2 * x:
		print(sum((row.count('.') for row in square)) * x)
		continue
	cost = 0
	for row in square:
		whites = row.split('*')
		for w in whites:
			cost += len(w) // 2 * y
			if len(w) % 2:
				cost += x
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	count = 0
	for i in range(n):
		a = input()
		arr.append(a)
		count += a.count('.')
	if 2 * x <= y:
		print(x * count)
		continue
	price = 0
	for a in arr:
		j = 0
		while j < m:
			if j == m - 1:
				if a[j] == '.':
					price += x
				j += 1
			else:
				if a[j] == '.':
					if a[j + 1] == '.':
						price += y
						j += 1
					else:
						price += x
						j += 1
				j += 1
	print(price)


def solve(tiling, x, y):
	white_tiles = []
	painted = set()
	(n, m) = (len(tiling), len(tiling[0]))
	for i in range(n):
		for j in range(m):
			if tiling[i][j] == '.':
				white_tiles.append((i, j))
	set_white = set(white_tiles)
	ans = 0
	for (i, j) in white_tiles:
		if j < m - 1:
			if (i, j) not in painted and (i, j + 1) in set_white and ((i, j + 1) not in painted):
				ans += min(2 * x, y)
				painted.add((i, j + 1))
				painted.add((i, j))
		if (i, j) not in painted:
			ans += x
			painted.add((i, j))
	return int(ans)

def main():
	t = int(input())
	i = 1
	while i <= t:
		(n, m, x, y) = map(int, input().split())
		tiling = []
		for q in range(n):
			tiling.append(list(input()))
		print(solve(tiling, x, y))
		i += 1
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	use_one = False
	if 2 * x <= y:
		use_one = True
	total = 0
	for _ in range(n):
		qtd = 0
		for l in input():
			if l == '*':
				if use_one:
					total += x * qtd
				else:
					duplas = qtd // 2
					total += y * duplas
					total += x * (qtd - duplas * 2)
				qtd = 0
			else:
				qtd += 1
		if use_one:
			total += x * qtd
		else:
			duplas = qtd // 2
			total += y * duplas
			total += x * (qtd - duplas * 2)
	print(total)


def optimise_row(x, y, ones, twos):
	if y >= 2 * x:
		return (twos * 2 + ones) * x
	if y < 2 * x:
		return twos * y + ones * x
t = int(input())
for i in range(0, t):
	ans = 0
	q = input()
	(n, m, x, y) = q.split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	maze = []
	for i in range(0, n):
		maze.append(input())
		maze[i] = list(maze[i])
	for i in range(0, n):
		twos = 0
		ones = 0
		j = 0
		while j < m:
			if j + 1 < m:
				if maze[i][j] == '.' and maze[i][j + 1] == '.':
					twos += 1
					j += 1
				elif maze[i][j] == '.' and maze[i][j + 1] != '.':
					ones += 1
				else:
					pass
			elif maze[i][j] == '.':
				ones += 1
			else:
				pass
			j += 1
		ans += optimise_row(x, y, ones, twos)
	print(ans)


import sys
import math as mt
t = int(input())
for __ in range(t):
	(n, m, a, b) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	if 2 * a <= b:
		ans = 0
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					ans += a
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m - 1:
				if l[i][j] == l[i][j + 1] == '.':
					ans += b
					j += 1
				elif l[i][j] == '.':
					ans += a
				j += 1
			if j == m - 1:
				if l[i][j] == '.':
					ans += a
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for t in range(n):
		kl = list(input())
		l.append(kl)
	if x <= y / 2:
		cnt = 0
		for t in l:
			for p in t:
				if p == '.':
					cnt += 1
		print(cnt * x)
	else:
		ans = 0
		for t in range(n):
			cnt = 0
			for i in range(m):
				if l[t][i] == '.':
					cnt += 1
				if l[t][i] == '*' and cnt != 0:
					ans += y * (cnt // 2)
					if cnt % 2:
						ans += x
					cnt = 0
			ans += y * (cnt // 2)
			if cnt % 2:
				ans += x
		print(ans)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	b = 0
	w = 0
	for i in range(n):
		s = input()
		w += s.count('.')
		l.append(s)
	if 2 * x <= y:
		print(x * w)
	else:
		a = 0
		for i in range(n):
			ss = ''
			for j in range(m):
				if l[i][j] == '.':
					if l[i][j] == ss:
						ss = ''
						a += 1
						continue
				ss = l[i][j]
		print(a * y + (w - 2 * a) * x)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		s = input()
		for j in range(m):
			a.append(s[j])
		a.append('*')
	la = len(a)
	if x <= y / 2:
		count = x * a.count('.')
		print(count)
	else:
		count = 0
		for i in range(la):
			if a[i] == '.':
				if a[i + 1] == '.':
					count += y
					a[i] = a[i + 1] = '*'
				else:
					count += x
					a[i] = '*'
		print(count)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = 0
	if 2 * x <= y:
		y = 2 * x
	for j in range(n):
		a = input()
		v = 0
		for i in range(m):
			if a[i] == '*':
				s += v // 2 * y + v % 2 * x
				v = 0
			else:
				v += 1
		s += v // 2 * y + v % 2 * x
	print(s)


for i in [0] * int(input()):
	total = 0
	(r, c, o, t) = map(int, input().split())
	for i in range(r):
		a = input()
		if t <= o * 2:
			total += a.count('..') * t
			a = a.replace('..', '')
			total += a.count('.') * o
		else:
			total += a.count('.') * o
	print(total)


from io import BytesIO, IOBase
import sys
import os

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))

def main():
	t = Iint()
	for _ in range(t):
		(n, m, x, y) = Imap()
		better = y <= 2 * x
		ans = 0
		for _ in range(n):
			s = input()
			if not better:
				ans += s.count('.') * x
				continue
			i = 0
			while i < m:
				if s[i] == '.':
					if i < m - 1 and s[i + 1] == '.':
						ans += y
						i += 1
					else:
						ans += x
				i += 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def main():
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		string = str(input())
		j = 0
		while j < m:
			if j < m - 1 and string[j] == string[j + 1] and (string[j] == '.'):
				ans += min(2 * x, y)
				j += 2
			elif string[j] == '.':
				ans += x
				j += 1
			elif string[j] != '.':
				j += 1
	print(ans)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		s = input().strip('\n')
		temp = []
		for k in s:
			temp.append(k)
		mat.append(temp)
	cost = 0
	for i in range(n):
		row = mat[i]
		dp = [0] * m
		available = [0] * m
		for j in range(m):
			if row[j] == '.':
				if j == 0:
					dp[j] += x
				elif row[j - 1] == '.':
					if 2 * x <= y:
						dp[j] = dp[j - 1] + x
					elif available[j - 1] == 0:
						if j - 2 >= 0:
							dp[j] = dp[j - 2] + y
							available[j] = 1
							available[j - 1] = 1
						else:
							dp[j] = y
							available[j] = 1
							available[j - 1] = 1
					else:
						dp[j] = dp[j - 1] + x
				else:
					dp[j] = dp[j - 1] + x
			elif j != 0:
				dp[j] = dp[j - 1]
		cost += dp[m - 1]
	print(cost)


for test in range(int(input())):
	(m, n, x, y) = map(int, input().split())
	board = []
	vis = [[0] * n for i in range(m)]
	coun = 0
	for i in range(m):
		board.append(input())
		coun += board[-1].count('.')
	if x * 2 <= y:
		print(coun * x)
	else:
		ans = 0
		for i in range(m):
			prev = 0
			for j in range(n):
				if board[i][j] == '.':
					prev += 1
				else:
					if prev % 2 == 0:
						ans += prev // 2 * y
					else:
						ans += prev // 2 * y + x
					prev = 0
			if prev % 2 == 0:
				ans += prev // 2 * y
			else:
				ans += prev // 2 * y + x
			prev = 0
		print(ans)


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)
t = inn()
for tt in range(t):
	(n, m, x, y) = inm()
	sm = 0
	if 2 * x <= y:
		for i in range(n):
			s = ins()
			sm += s.count('.')
		sm *= x
	else:
		for i in range(n):
			s = ins()
			t = s.split('*')
			for z in t:
				p = len(z)
				sm += p // 2 * y + p % 2 * x
	print(sm)


t = int(input())
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	s = []
	ans = 0
	for i in range(n):
		s.append(input())
	for i in range(n):
		val = list(s[i])
		for j in range(len(val)):
			if val[j] == '.':
				cost = x
				if j < len(val) - 1 and val[j + 1] == '.':
					if y < 2 * x:
						cost = y
						val[j + 1] = '*'
						1
				ans += cost
	print(ans)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(n, m, x, y) = map(int, stdin.readline().split())
	arr = []
	for __ in range(n):
		arr.append([i for i in stdin.readline()])
	if 2 * x <= y:
		ans = 0
		for i in range(n):
			for j in range(m):
				if arr[i][j] == '.':
					ans += x
		stdout.write(str(ans) + '\n')
	else:
		ans = 0
		for i in range(n):
			count = 0
			countinuos = 0
			j = 0
			while j < m:
				if arr[i][j] == '.':
					count += 1
					try:
						if arr[i][j + 1] == '.':
							count -= 1
							countinuos += 1
							j = j + 2
							continue
					except:
						j += 2
						continue
				j += 1
			if countinuos > 0:
				ans += countinuos * y
				ans += count * x
			else:
				ans += count * x
		stdout.write(str(ans) + '\n')


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	sum = 0
	for i in range(n):
		s = list(input())
		c = 0
		for i in range(0, len(s)):
			if s[i] == '.':
				c = c + 1
			if s[i] != '.' and c > 0:
				if c % 2 == 0:
					if x * 2 <= y:
						sum = sum + x * 2 * (c // 2)
						c = 0
					else:
						sum = sum + y * (c // 2)
						c = 0
				else:
					sum = sum + x
					c = c - 1
					if c > 0:
						if x * 2 <= y:
							sum = sum + x * 2 * (c // 2)
							c = 0
						else:
							sum = sum + y * (c // 2)
							c = 0
		if c > 0:
			if c % 2 == 0:
				if x * 2 <= y:
					sum = sum + x * 2 * (c // 2)
					c = 0
				else:
					sum = sum + y * (c // 2)
					c = 0
			else:
				sum = sum + x
				c = c - 1
				if c % 2 == 0 and c > 0:
					if x * 2 <= y:
						sum = sum + x * 2 * (c // 2)
						c = 0
					else:
						sum = sum + y * (c // 2)
						c = 0
	print(sum)


t = int(input())
while t > 0:
	t -= 1
	cost = 0
	(n, m, x, y) = map(int, input().split())
	for i in range(n):
		s = input()
		j = 0
		while j < m:
			if s[j] == '*':
				j += 1
				continue
			if j + 1 < len(s) and s[j] == '.' and (s[j + 1] == '.') and (y < 2 * x):
				j += 2
				cost += y
			else:
				j += 1
				cost += x
	print(cost)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	f = []
	for i in range(a):
		f.append(list(input()))
	e = 0
	j = 0
	for i in range(a):
		j = 0
		if c > d:
			while j <= b - 1:
				if f[i][j] == '.':
					if j > 0 and f[i][j - 1] != '*':
						e += d
						j += 1
					elif j < b - 1 and f[i][j + 1] != '*':
						e += d
						f[i][j + 1] = '*'
						j += 2
					else:
						e += c
						j += 1
				else:
					j += 1
		elif c * 2 > d:
			while j <= b - 1:
				if f[i][j] == '.':
					if j < b - 1 and f[i][j + 1] == '.':
						e += d
						f[i][j + 1] = '*'
						j += 2
					else:
						e += c
						j += 1
				else:
					j += 1
		else:
			while j <= b - 1:
				if f[i][j] == '.':
					e += c
				j += 1
	print(e)


import sys, math
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, sys.stdin.readline().split())
	a = []
	for i in range(n):
		a.append(input())
	ans = 0
	if y >= 2 * x:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					ans += x
	else:
		for i in range(n):
			j = 0
			flag = False
			while j < m:
				if j < m - 1 and a[i][j] == '.' and (a[i][j + 1] == a[i][j]):
					ans += y
					j += 2
					flag = True
				elif a[i][j] == '.':
					ans += x
					j += 2
					flag = True
				else:
					j += 1
	print(ans)


qtd = int(input())
for _ in range(qtd):
	(n, m, price11, price12) = map(int, input().split())
	total = 0
	for i in range(n):
		row = input() + '*'
		j = 0
		while j < len(row) - 1:
			if row[j] == '*':
				pass
			elif row[j] == '.' and row[j + 1] == '*':
				total += price11
			elif row[j] == '.' and row[j + 1] == '.':
				if price11 * 2 < price12:
					total += price11 * 2
				else:
					total += price12
				j += 1
			j += 1
	print(total)


import sys, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	(n, m, x, y) = map(int, sys.stdin.readline().split())
	grid = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
	double = 0
	single = 0
	total = 0
	for i in range(n):
		for j in range(m):
			if grid[i][j] == '.':
				total += 1
	for i in range(n):
		j = 0
		while j < m - 1:
			if grid[i][j] == '.' and grid[i][j + 1] == '.':
				double += 1
				j += 2
			else:
				j += 1
	if y <= 2 * x:
		print(y * double + x * (total - 2 * double))
	else:
		print(x * total)


import sys
input = sys.stdin.readline
from sys import stdin, stdout

def main():
	for _ in range(int(input())):
		(n, m, x, y) = [int(s) for s in input().split()]
		l = [input().strip() + '**' for i in range(n)]
		ans = 0
		for i in range(n):
			dp = [0] * (m + 2)
			for j in range(m):
				if l[i][j] == '*':
					dp[j] = dp[j - 1]
				elif l[i][j - 1] == '*':
					dp[j] = dp[j - 1] + x
				else:
					dp[j] = min(x + dp[j - 1], y + dp[j - 2])
			ans += dp[m - 1]
		print(ans)
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	theater = list()
	for i in range(n):
		arr = input()
		theater.append(arr)
	if x < y / 2:
		res = 0
		for line in theater:
			res += line.count('.')
		res *= x
	else:
		res = 0
		for line in theater:
			i = 0
			while i < m:
				if i + 1 < m:
					if line[i] == '.' and line[i + 1] == '.':
						res += y
						i += 2
						continue
				if line[i] == '.':
					res += x
				i += 1
	print(res)


a = int(input())
for i in range(a):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	total = 0
	for i in range(n):
		s = input()
		ans = []
		for i in range(len(s)):
			ans.append(s[i])
		for i in range(1, len(ans)):
			if ans[i] == ans[i - 1] == '.':
				total += y
				ans[i] = '*'
				ans[i - 1] = '*'
			elif ans[i] == '*' and ans[i - 1] == '.':
				total += x
		if len(ans) == 1 and ans[-1] == '.':
			total += x
		if len(ans) > 1 and ans[-1] == '.' and (ans[-2] == '*'):
			total += x
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	tea = []
	for i in range(n):
		tea.append(input())
	flag = True if 2 * x > y else False
	cont = 0
	for i in range(n):
		for j in range(m):
			if tea[i][j] == '.':
				cont += x
	if flag:
		for i in range(n):
			j = 0
			while j < m - 1:
				if tea[i][j] == '.' and tea[i][j + 1] == '.':
					cont -= 2 * x
					cont += y
					j += 2
				else:
					j += 1
	print(cont)


from sys import stdin, stdout
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	(c1, c2) = (0, 0)
	N = []
	for j in range(n):
		M = list(input())
		N.append(M)
	(r1, r2) = (0, 0)
	for i in range(n):
		j = 0
		while j < m:
			if j + 1 < m and N[i][j] == '.':
				if N[i][j + 1] == '.':
					r1 += y * 1
					j += 2
					r2 += x * 2
				else:
					r1 += x
					r2 += x
					j += 1
			elif j + 1 >= m and N[i][j] == '.':
				r1 += x
				r2 += x
				j += 1
			else:
				j += 1
	print(min(r1, r2))


_n = int(input())
for i in range(_n):
	(n, m, x, y) = list(map(int, input().split()))
	s2 = max(x + x - y, 0)
	tc = 0
	for j in range(n):
		p = '*'
		for c in input():
			if c == '*':
				p = c
			elif p == '.':
				tc += x - s2
				p = '*'
			else:
				tc += x
				p = c
	print(tc)


import math as ma
t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for i in range(n):
		a.append(input())
	if 2 * x <= y:
		s = 0
		for i in range(n):
			s += a[i].count('.')
		print(s * x)
	else:
		s = 0
		for i in range(n):
			j = 0
			while j < m:
				if j + 1 < m:
					if a[i][j + 1] == a[i][j] == '.':
						j += 2
						s += y
					elif a[i][j] == '.':
						j += 1
						s += x
					else:
						j += 1
				elif a[i][j] == '.':
					s += x
					j += 1
				else:
					j += 1
		print(s)


import sys
import math
lines = sys.stdin.readlines()
start = True
for (i, l) in enumerate(lines):
	lines[i] = l.strip()
tittle = lines[1]
first = int(lines[0])
lines = lines[1:]
for block in range(first):
	tittle = list(map(int, lines[0].split(' ')))
	del lines[0]
	total_price = 0
	if tittle[2] * 2 < tittle[3]:
		tittle[3] = tittle[2] * 2
	for row in range(tittle[0]):
		double = False
		for (i, char) in enumerate(lines[row]):
			if double:
				double = False
				continue
			if char == '.':
				if tittle[1] > i + 1 and lines[row][i + 1] == '.':
					total_price += tittle[3]
					double = True
				else:
					total_price += tittle[2]
					double = False
			else:
				double = False
	print(total_price)
	del lines[0:row + 1]


def solve():
	(n, m, x, y) = [int(i) for i in input().split()]
	arr = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		arr[i] = list(input())
	cost = 0
	conse = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.':
				conse += 1
			else:
				if conse >= 1:
					cost += min(conse * x, conse // 2 * y + conse % 2 * x)
				conse = 0
		if conse >= 1:
			cost += min(conse * x, conse // 2 * y + conse % 2 * x)
			conse = 0
	print(cost)
t = int(input())
while t:
	solve()
	t -= 1


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	res = 0
	for _ in range(n):
		a = input()
		i = 0
		while i < m:
			if a[i] == '.':
				if i + 1 < m and a[i + 1] == '.':
					res += y
					i += 1
				else:
					res += x
			i += 1
	print(res)


I = lambda : list(map(int, input().split()))
t = I()[0]
while t:
	t -= 1
	(n, m, x, y) = I()
	grid = []
	ans = 0
	for i in range(n):
		grid.append(input())
		ans += grid[i].count('.')
	ans2 = 0
	for i in range(n):
		j = 1
		while j < m:
			if grid[i][j] == '.' and grid[i][j - 1] == grid[i][j]:
				ans2 += 1
				j += 1
			j += 1
	ans2 = ans2 * y + abs(2 * ans2 - ans) * x
	ans *= x
	print(min(ans, ans2))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	s = 0
	for i in range(n):
		a.append(input())
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					s += x
	else:
		for i in range(n):
			flag = 0
			for j in range(m):
				if flag == 1:
					flag = 0
					continue
				if a[i][j] == '.' and (j + 1 < m and a[i][j + 1] == '.'):
					s += y
					flag = 1
				elif a[i][j] == '*':
					continue
				else:
					s += x
	print(s)


import math
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	matrix = []
	for i in range(n):
		a = input()
		l = list(a)
		matrix.append(l)
	dp = [[0 for i in range(m + 1)] for i in range(n)]
	for i in range(n):
		if matrix[i][0] == '.':
			dp[i][1] = x
	for i in range(n):
		for j in range(2, m + 1):
			if matrix[i][j - 1] == '.' and matrix[i][j - 2] == '.':
				dp[i][j] = min(dp[i][j - 1] + x, dp[i][j - 2] + y)
			elif matrix[i][j - 1] == '.' and matrix[i][j - 2] != '.':
				dp[i][j] = dp[i][j - 1] + x
			elif matrix[i][j - 1] != '.':
				dp[i][j] = dp[i][j - 1]
	sum = 0
	for i in range(n):
		sum += dp[i][m]
	print(sum)


for _ in range(int(input())):
	(r, c, x, y) = list(map(int, input().split()))
	d = 2 * x
	d = 2 * x if d < y else y
	cost = 0
	for _ in range(r):
		row = input()
		count = 0
		for i in range(len(row)):
			if row[i] == '*':
				cost += count // 2 * d + count % 2 * x
				count = 0
			else:
				count += 1
		cost += count // 2 * d + count % 2 * x
	print(cost)


for _ in range(int(input())):
	(h, w, x, y) = map(int, input().split())
	if x * 2 <= y:
		print(x * sum((input().count('.') for _ in range(h))))
		continue
	ans = 0
	for _ in range(h):
		s = list(input())
		for i in range(w - 1):
			if s[i] == '*':
				continue
			if s[i + 1] == '.':
				s[i] = s[i + 1] = '*'
				ans += y
			else:
				s[i] = '*'
				ans += x
		if s[w - 1] == '.':
			ans += x
	print(ans)


def main():
	(n, m, x, y) = map(int, input().split())
	ans = 0
	if y < 2 * x:
		for _ in range(n):
			line = input().split('*')
			for run in line:
				if run != '':
					ans += len(run) % 2 * x + len(run) // 2 * y
	else:
		for _ in range(n):
			line = input().split('*')
			for run in line:
				if run != '':
					ans += len(run) * x
	print(ans)
for _ in range(int(input())):
	main()


t = int(input())
B = []
for i in range(0, t):
	T = 0
	A = []
	s = input()
	split = s.split(' ')
	split2 = [int(i) for i in split]
	A.append(split2)
	for j in range(0, split2[0]):
		u = input()
		split3 = list(u)
		A.append(split3)
	n = split2[0]
	m = split2[1]
	x = split2[2]
	y = split2[3]
	if 2 * x > y:
		for j in range(1, n + 1):
			for k in range(0, m - 1):
				if A[j][k] == '.' and A[j][k + 1] == '.':
					T = T + y
					A[j][k + 1] = '*'
				elif A[j][k] == '.':
					T = T + x
		for j in range(1, n + 1):
			if A[j][m - 1] == '.':
				T = T + x
			else:
				T = T
	else:
		for j in range(1, n + 1):
			count = A[j].count('.')
			T = T + count * x
	B.append(T)
for i in B:
	print(i)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	count = 0
	for i in range(n):
		a = input()
		j = 0
		cur = 0
		for j in range(m):
			if a[j] == '.':
				cur += 1
			else:
				count += cur % 2 * x + min(cur // 2 * y, cur // 2 * 2 * x)
				cur = 0
		count += cur % 2 * x + min(cur // 2 * y, cur // 2 * 2 * x)
	print(count)


g = int(input())
for i in range(g):
	value = 0
	testcase = [int(j) for j in input().split()]
	for rowNumber in range(testcase[0]):
		row = input().split('*')
		for batch in row:
			if testcase[3] <= testcase[2] * 2:
				value += int(len(batch) // 2 * testcase[3] + len(batch) % 2 * testcase[2])
			else:
				value += len(batch) * testcase[2]
	print(value)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	total_cost1 = 0
	total_cost2 = 0
	for j in range(n):
		s = input()
		total_cost1 += s.count('.') * x
		if m >= 2:
			k = 0
			flag = 0
			while k < m - 1:
				if s[k] == s[k + 1] and s[k] == '.':
					total_cost2 += y
					if k + 1 == m - 1:
						flag = 1
					k += 1
				elif s[k] == '.':
					total_cost2 += x
				k += 1
		if m >= 2:
			if s[m - 1] == '.' and flag == 0:
				total_cost2 += x
		else:
			total_cost2 = 99999999999
	print(min(total_cost1, total_cost2))


import math
n = int(input())

def cal(n, m, a, b, layout):
	res = 0
	for j in range(n):
		c = 0
		for i in range(m):
			if c == m:
				break
			if lay[j][c] == '.':
				if c < m - 1:
					if (b <= a or b <= a * 2) and lay[j][c + 1] == '.':
						res += b
						c += 1
					else:
						res += a
				else:
					res += a
			c += 1
	return res
for i in range(n):
	(n, m, a, b) = map(int, input().split())
	lay = [[]] * n
	for i in range(n):
		lay[i] = input()
	print(cal(n, m, a, b, lay))


nn = int(input())
for k in range(nn):
	(n, m, x, y) = [int(x) for x in input().split()]
	c = 0
	double = False
	if y > 2 * x:
		y = 2 * x
	for i in range(n):
		t = input()
		for j in range(m):
			if double:
				double = False
				continue
			if t[j] == '.':
				if j + 1 < len(t) and t[j + 1] == '.':
					c += y
					double = True
				else:
					c += x
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	d = {}
	for i in range(n):
		d[i] = []
	price_1 = 0
	for i in range(n):
		s = input() + '@'
		price_1 += s.count('.') * x
		j = 0
		while j < m:
			if s[j] == '.' and s[j + 1] == '.':
				d[i].append(2)
				j += 2
			elif s[j] == '.' and s[j + 1] != '.':
				d[i].append(1)
				j += 1
			elif s[j] == '*':
				j += 1
	price = 0
	for i in range(n):
		price += d[i].count(1) * x + d[i].count(2) * y
	print(min(price, price_1))


t = int(input())
for cases in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	y = min(2 * x, y)
	for i in range(n):
		s = input()
		a = 0
		while a < m:
			if s[a] == '*':
				a += 1
				continue
			j = a
			while j + 1 < m and s[j + 1] == '.':
				j += 1
			l = j - a + 1
			cost += l % 2 * x + l // 2 * y
			a = j + 1
	print(cost)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def main():
	try:
		(n, m, x, y) = In()
		l = []
		for i in range(n):
			temp = list(input())
			l.append(temp)
		ans = 0
		for i in range(n):
			temp = l[i]
			dot = 0
			j = 0
			flag = 0
			while j < m - 1:
				if temp[j] == '.' and temp[j + 1] == '.':
					if 2 * x < y:
						ans += 2 * x
					else:
						ans += y
					j += 1
				elif temp[j] == '.':
					ans += x
				j += 1
				if j >= m:
					flag = 1
			if flag == 0:
				if temp[-1] == '.':
					ans += x
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
tc = int(input())
for zzz in range(tc):
	(r, c, x, y) = map(int, input().split())
	arr = [None] * r
	for i in range(r):
		arr[i] = input()
	low = min(2 * x, y)
	su = 0
	for i in range(r):
		j = 0
		while j < c:
			if arr[i][j] == '.':
				if j + 1 < c:
					if arr[i][j + 1] == '.':
						su += low
					else:
						su += x
				else:
					su += x
				j += 2
			else:
				j += 1
	print(su)


num = int(input())
for _ in range(num):
	(n, m, x, y) = [int(i) for i in input().split()]
	if 2 * x <= y:
		k = 2 * x
	else:
		k = y
	l = []
	for __ in range(n):
		l.append(input())
	c = 0
	ans = 0
	for i in range(n):
		for j in range(m):
			if l[i][j] == '.':
				if c == 1:
					ans += k
					c = 0
				elif c == 0:
					c += 1
			else:
				ans += c * x
				c = 0
		if c:
			ans += c * x
		c = 0
	print(ans)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	characters = []
	for i in range(n):
		inner = []
		row = input()
		for j in range(m):
			inner.append(row[j])
		characters.append(inner)
	if 2 * x <= y:
		single = 0
		for i in range(n):
			for j in range(m):
				if characters[i][j] == '.':
					single += 1
		cost = single * x
	else:
		single = 0
		pair = 0
		for i in range(n):
			j = 0
			while j < m:
				adjac = 0
				while j < m and characters[i][j] == '.':
					adjac += 1
					j += 1
				if adjac % 2 == 0:
					pair += adjac // 2
				else:
					pair += adjac // 2
					single += 1
				j += 1
		cost = single * x + pair * y
	print(cost)


t = int(input())
for case in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	lines = [input().strip() for _ in range(n)]
	covered = [[False for _ in range(m)] for __ in range(n)]
	c = 0
	for a in range(n):
		for b in range(m):
			if lines[a][b] == '.' and (not covered[a][b]):
				if b < m - 1 and lines[a][b + 1] == '.' and (y < 2 * x):
					covered[a][b] = True
					covered[a][b + 1] = True
					c += y
				else:
					c += x
					covered[a][b] = True
	print(c)


def cost(m, x, y, row):
	if x + x <= y:
		return x * len([char for char in row if char == '.'])
	else:
		total = 0
		pos = 0
		while pos < m:
			if pos < m - 1 and row[pos] == '.' and (row[pos + 1] == '.'):
				pos += 2
				total += y
			elif row[pos] == '.':
				pos += 1
				total += x
			else:
				pos += 1
		return total
numberofcases = int(input())
for _ in range(numberofcases):
	(n, m, x, y) = [int(t) for t in input().split()]
	total = 0
	for _ in range(n):
		total += cost(m, x, y, input())
	print(total)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	k = 0
	for i in range(n):
		s = input()
		dp = [0] * m
		c = 0
		st = 0
		for i in range(m):
			if s[i] == '.':
				if i > 0:
					dp[i] = dp[i - 1] + 1
				elif i == 0:
					dp[i] = 1
		for i in range(m):
			if dp[i] > 1:
				c += 1
				if i < m - 1:
					if dp[i + 1] < 2:
						if dp[i] % 2 == 0:
							if y * dp[i] // 2 < dp[i] * x:
								ans += dp[i] // 2 * y
							else:
								ans += dp[i] * x
						elif dp[i] % 2 != 0:
							v = dp[i] // 2
							if y * v + x < dp[i] * x:
								ans += v * y + x
							else:
								ans += dp[i] * x
				if i == m - 1:
					if dp[i] % 2 == 0:
						if y * dp[i] // 2 < dp[i] * x:
							ans += dp[i] // 2 * y
						else:
							ans += dp[i] * x
					elif dp[i] % 2 != 0:
						v = dp[i] // 2
						if y * v + x < dp[i] * x:
							ans += v * y + x
						else:
							ans += dp[i] * x
			elif m == 1:
				if s[i] == '.':
					st += 1
			else:
				if i < m - 1:
					if dp[i] == 1 and dp[i + 1] == 0:
						st += 1
				if i == m - 1:
					if dp[i - 1] == 0 and dp[i] == 1:
						st += 1
		ans += st * x
	print(ans)


import sys, random, math
from bisect import bisect_left, bisect_right
input = sys.stdin.readline

def main():
	inf = 10 ** 20
	t = int(input())
	for _ in range(1, t + 1):
		(n, m, x, y) = map(int, input().split())
		y = min(y, x * 2)
		ans = 0
		for i in range(n):
			pat = input().strip()
			pat += '*'
			(l, before) = (-1, 0)
			for i in range(m + 1):
				if pat[i] == '*':
					if before:
						ans += (i - l) // 2 * y
						ans += (i - l) % 2 * x
						before = 0
				elif not before:
					l = i
					before = 1
		print(ans)
main()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	pnt1 = 0
	pnt2 = 0
	cont = 0
	ans = 0
	for i in range(n):
		grid.append(list(input()))
	for r in range(n):
		for c in range(m):
			if grid[r][c] == '.' and (not cont):
				pnt1 = c
				cont = 1
			elif grid[r][c] == '*' and cont:
				if not pnt2:
					pnt2 = c - 1
				ans += min((pnt2 - pnt1 + 1) // 2 * y + (pnt2 - pnt1 + 1) % 2 * x, (pnt2 - pnt1 + 1) * x)
				cont = 0
				pnt1 = 0
				pnt2 = 0
		if cont:
			pnt2 = m - 1
			ans += min((pnt2 - pnt1 + 1) // 2 * y + (pnt2 - pnt1 + 1) % 2 * x, (pnt2 - pnt1 + 1) * x)
			pnt1 = 0
			pnt2 = 0
			cont = 0
	print(ans)


def solve(mat, n, m, x, y):
	i = 0
	count1 = 0
	count2 = 0
	while i < m:
		if i < m - 1 and mat[i] == '.' and (mat[i + 1] == '.'):
			count2 += 1
			i += 1
		elif mat[i] == '.':
			count1 += 1
		i += 1
	a1 = (count2 * 2 + count1) * x
	a2 = count2 * y + count1 * x
	return min(a1, a2)
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for _ in range(n):
		arr = list(input().rstrip())
		ans += solve(arr, n, m, x, y)
	print(ans)


import math
for _ in range(int(input())):
	cost = 0
	(r, c, x, y) = map(int, input().split())
	for i in range(r):
		s = list(input())
		for j in range(c):
			if s[j] == '.':
				if j <= c - 2 and s[j + 1] == '.':
					if 2 * x >= y:
						cost += y
						s[j] = '*'
						s[j + 1] = '*'
					else:
						cost += 2 * x
						s[j] = '*'
						s[j + 1] = '*'
				else:
					cost += x
					s[j] = '*'
	print(cost)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
entries = inp()
for i in range(entries):
	l = inlt()
	n = l[0]
	m = l[1]
	x = l[2]
	y = l[3]
	seats = []
	for j in range(n):
		seats.append(insr())
	p2 = min(y, 2 * x)
	res = 0
	for r in seats:
		j = 0
		if m == 1:
			if r[j] == '.':
				res += x
		else:
			while j < m:
				if j < m - 1:
					if r[j] == '.' and r[j + 1] == '.':
						res += p2
						j += 2
						continue
				if r[j] == '.':
					res += x
					j += 1
				else:
					j += 1
	print(res)


import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest

def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))[::-1]
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
t = INT()
while t != 0:
	(n, m, x, y) = MAP()
	grid = []
	for _ in range(n):
		grid.append(list(input()))
	if y >= 2 * x:
		ans = 0
		for i in range(n):
			for j in range(m):
				if grid[i][j] == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if grid[i][j] == '.':
					if j < m - 1 and grid[i][j + 1] == '.':
						ans += y
						j += 2
					else:
						ans += x
						j += 1
				else:
					j += 1
		print(ans)
	t -= 1


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(y, x * 2)
	ans = 0
	for i in range(n):
		p = 0
		for c in input():
			if c == '.':
				p += 1
			else:
				ans += p // 2 * y + p % 2 * x
				p = 0
		ans += p // 2 * y + p % 2 * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		adj = s.count('..')
		single = s.count('.')
		if y > 2 * x:
			ans += single * x
		else:
			ans += adj * y + x * (single - 2 * adj)
	print(ans)


ans = []
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	spi = [input() for _ in range(n)]
	s = 0
	for elem in spi:
		while elem.count('*') > 0:
			a = elem.find('*')
			b = len(elem[:a])
			elem = elem[a + 1:]
			if x * 2 > y:
				s += b // 2 * y + b % 2 * x
			else:
				s += b * x
		if len(elem) > 0:
			b = len(elem)
			if x * 2 > y:
				s += b // 2 * y + b % 2 * x
			else:
				s += b * x
	ans.append(str(s))
print('\n'.join(ans))


def solve(matriz, n, m, preco1, preco2):
	total = 0
	skip = False
	for linha in matriz:
		for (index, char) in enumerate(linha):
			if skip:
				skip = False
				continue
			if char == '*':
				continue
			if index == m - 1:
				total += preco1
				continue
			if linha[index + 1] == '.' and preco2 < preco1 * 2:
				total += preco2
				skip = True
			else:
				total += preco1
	return total
t = int(input())
for _ in range(t):
	(n, m, preco1, preco2) = map(int, input().split())
	matriz = []
	for i in range(n):
		linha = input()
		matriz.append(linha)
	print(solve(matriz, n, m, preco1, preco2))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	arr = []
	for i in range(n):
		arr.append(input().strip().split('*'))
	ans = 0
	for i in range(n):
		for j in arr[i]:
			l = len(j)
			ans += l // 2 * y + l % 2 * x
	print(ans)


def count_element(matrix, el):
	count = 0
	for i in range(len(matrix)):
		line = matrix[i]
		for j in line:
			if j == el:
				count += 1
	return count
T = int(input())
for t in range(T):
	(n, m, x, y) = list(map(int, input().split()))
	matrix = [list(input()) for i in range(n)]
	if y >= 2 * x:
		res = count_element(matrix, '.') * x
	else:
		adj_lines = []
		for i in range(len(matrix)):
			line = matrix[i]
			adj = []
			cur = 0
			for j in range(len(line)):
				c = line[j]
				if c == '.':
					cur += 1
				else:
					adj.append(cur)
					cur = 0
			adj.append(cur)
			adj_lines.append(adj)
		res = 0
		for i in range(len(adj_lines)):
			adj_line = adj_lines[i]
			for j in adj_line:
				q = j // 2
				r = j % 2
				res += q * y + r * x
	print(res)


t = int(input())
for i in range(t):
	til = []
	cost = 0
	url = False
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	for a in range(n):
		til = [str(i) for i in input()]
		for l in range(m):
			if l == 0:
				url = False
			if til[l] == '.':
				if url == False:
					url = True
					cost += x
				else:
					url = False
					cost += y - x
			else:
				url = False
	print(cost)


def F(lst, x, y):
	ans = 0
	if y >= x * 2:
		for i in lst:
			ans += i.count('.')
		return x * ans
	else:
		for i in lst:
			ans += i.count('..') * y
			ans += (i.count('.') - i.count('..') * 2) * x
		return ans
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	lst = []
	for i in range(n):
		lst.append(input())
	print(F(lst, x, y))


from sys import stdin, stdout
input = lambda : stdin.readline().strip()
fast_print = stdout.write
from math import sqrt, floor, ceil
from bisect import bisect_right
T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	matrix = []
	for i in range(n):
		matrix.append(list(input()))
	cnt1 = cnt2 = 0
	for i in matrix:
		whites = ''.join(i).split('*')
		for j in whites:
			cnt1 += len(j) % 2
			cnt2 += len(j) // 2
	fast_print(str(x * cnt1 + min(y * cnt2, x * cnt2 * 2)) + '\n')


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	l = [0] * n
	for j in range(n):
		l[j] = input()
	a = sum([x.count('.') for x in l])
	b = sum([x.count('..') for x in l])
	if 2 * x <= y:
		res = a * x
	else:
		res = (a - 2 * b) * x + b * y
	print(res)


for i in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	(op, price, c, tile_one, tile_two) = (0, 0, 0, 0, 0)
	if 2 * x > y:
		op = y
	else:
		op = x
	for i in range(n):
		i = 0
		s = input()
		while i < m:
			if s[i] == '*':
				i += 1
			else:
				while i < m and s[i] != '*':
					c += 1
					i += 1
				if op == y:
					tile_one = c % 2
					tile_two = c // 2
					price += tile_two * op + tile_one * x
				else:
					price += c * op
				c = 0
				i += 1
	print(price)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	(n, m, x, y) = mapi()
	mat = []
	for i in range(n):
		tmp = input().strip()
		mat.append([j for j in tmp])
	if 2 * x <= y:
		print(x * sum([sum((1 for i in tmp if i == '.')) for tmp in mat]))
	else:
		res = 0
		for i in range(n):
			cnt = 0
			for j in range(m):
				if mat[i][j] == '.':
					cnt += 1
				else:
					res += min(x * cnt, y * (cnt // 2) + x * (cnt % 2))
					cnt = 0
			if cnt:
				res += min(x * cnt, y * (cnt // 2) + x * (cnt % 2))
				cnt = 0
		print(res)


from sys import stdin
N = int(stdin.readline())
for _ in range(N):
	(n, m, x, y) = map(int, stdin.readline().split())
	maps = []
	ans = 0
	for i in range(n):
		maps.append([i for i in stdin.readline()])
	if x <= y // 2:
		for i in range(n):
			for j in range(m):
				if maps[i][j] == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		for i in range(n):
			for j in range(m):
				if maps[i][j] == '.':
					if j < m - 1:
						if maps[i][j + 1] == '.':
							maps[i][j + 1] = '*'
							ans += y
						else:
							ans += x
					else:
						ans += x
		print(ans)


t = int(input())
for k in range(t):
	parede = []
	(l, c, x, y) = [int(i) for i in input().split()]
	for i in range(l):
		linha = list(input())
		parede.append(linha)
	totalazulejos = 0
	for i in range(l):
		totalazulejos += parede[i].count('.')
	precosimples = totalazulejos * x
	precoduplo = 0
	for i in range(l):
		j = 0
		while j < c:
			if parede[i][j] == '.':
				if j == c - 1:
					precoduplo += x
					j += 1
				elif parede[i][j + 1] == '.':
					precoduplo += y
					j += 2
				else:
					precoduplo += x
					j += 1
			else:
				j += 1
	if precosimples > precoduplo:
		print(precoduplo)
	else:
		print(precosimples)


from math import *

def r1(t):
	return t(input().strip())

def r2(t):
	return [t(i) for i in input().strip().split()]

def r3(t):
	return [t(i) for i in input().strip()]
for _ in range(int(input())):
	(n, m, x, y) = r2(int)
	ans = 0
	cnt = 0
	for i in range(n):
		for j in input():
			if j == '.':
				cnt += 1
				if 2 * x <= y:
					ans += x
					cnt = 0
				elif cnt == 2:
					ans += y
					cnt = 0
			elif cnt == 1:
				ans += x
				cnt = 0
		if cnt == 1:
			ans += x
		cnt = 0
	print(ans)


import bisect
import sys
import math
input = sys.stdin.readline
from collections import defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solve(case):
	[n, m, x, y] = inlt()
	rows = []
	for i in range(n):
		rows.append(insr())
	if 2 * x <= y:
		return sum((sum((x if c == '.' else 0 for c in row)) for row in rows))
	res = 0
	for row in rows:
		i = 0
		while i < m:
			if row[i] == '.' and i < m - 1 and (row[i + 1] == '.'):
				i += 2
				res += y
			elif row[i] == '.':
				i += 1
				res += x
			else:
				i += 1
	return res
if len(sys.argv) > 1 and sys.argv[1].startswith('input'):
	f = open('./' + sys.argv[1], 'r')
	input = f.readline
T = inp()
for i in range(T):
	res = solve(i + 1)
	print(str(res))


import math
for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	p = 0
	k = 0
	for i in range(n):
		a = input()
		lengg = len(a)
		j = 0
		while j < lengg:
			if a[j] == '*':
				j += 1
			elif j + 1 <= lengg - 1 and a[j] == '.' and (a[j + 1] == '.'):
				p += 1
				j += 2
			else:
				k += 1
				j += 1
	if y >= 2 * x:
		print((k + 2 * p) * x)
	else:
		print(k * x + p * y)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for t in range(n):
		row = input()
		i = 0
		while i < m:
			if row[i] == '.':
				j = i + 1
				count = 1
				while j < m and row[i] == row[j]:
					count += 1
					j += 1
				arr.append(count)
				i = j
			else:
				i += 1
	sec = 0
	for e in arr:
		sec += y * (e // 2) + x * (e % 2)
	first = sum(arr) * x
	print(min(first, sec))


for ad in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	l = []
	a = []
	for i in range(n):
		v = list(input())
		l.append(v)
		a += v
	if 2 * x <= y:
		print(x * a.count('.'))
	else:
		c = 0
		al = 0
		for i in l:
			j = 0
			while j < m - 1:
				if i[j] == '.':
					if i[j + 1] == i[j]:
						c += y
					else:
						c += x
					j += 2
					al = 2
				else:
					j += 1
					al = 1
			if j == m - 1 and m > 1:
				if i[-1] == '.':
					c += x
			if m == 1:
				if i[0] == '.':
					c += x
		print(c)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
import math
for _ in range(ii()):
	(n, m, x, y) = f()
	a = [0] * n
	ans = []
	dot = 0
	for i in range(0, n):
		a[i] = si()
	for i in range(0, n):
		ps = 0
		for j in range(0, m):
			if a[i][j] == '.':
				ps = ps + 1
				dot = dot + 1
			else:
				pi = ps // 2
				px = ps % 2
				ans.append(y * pi + x * px)
				ps = 0
		else:
			ans.append(x * (ps % 2) + y * (ps // 2))
	print(min(x * dot, sum(ans)))


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	l = [list(input()) for i in range(n)]
	x = 0
	for i in range(n):
		p = 0
		for j in range(m):
			if l[i][j] == '.':
				p += 1
			else:
				if p % 2 == 0:
					x += min(p // 2 * b, p * a)
				else:
					x += min(p // 2 * b + a, p * a)
				p = 0
		if p:
			if p % 2 == 0:
				x += min(p // 2 * b, p * a)
			else:
				x += min(p // 2 * b + a, p * a)
			p = 0
	print(x)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	squares = [input() for _ in range(n)]
	if 2 * x < y:
		print(x * sum((row.count('.') for row in squares)))
	else:
		print(sum((y * row.count('..') + x * row.replace('..', '**').count('.') for row in squares)))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().rstrip().split(' '))
	price_2 = min(2 * x, y)
	r = []
	total = 0
	while n:
		n -= 1
		l = list(input())
		r.append(l)
	for ni in range(len(r)):
		for mi in range(m - 1):
			if r[ni][mi] == r[ni][mi + 1] and r[ni][mi] == '.':
				total += price_2
				r[ni][mi] = 0
				r[ni][mi + 1] = 0
			elif r[ni][mi] == '.':
				total += x
				r[ni][mi] = 0
		if r[ni][-1] == '.':
			total += x
	print(total)


test = int(input())
total_money = 0
go = True
for i in range(test):
	(length, width, money_1, money_2) = map(int, input().split(' '))
	for j in range(length):
		stone = [s for s in input()][:width]
		if width > 1:
			r = 1
			a = 0
			b = 0
			for item in stone:
				if item == stone[r] and item == '.' and (money_2 <= 2 * money_1) and go and (a != len(stone) - 1):
					go = False
					b = a + 1
					total_money += money_2
				elif item == '.' and go:
					total_money += money_1
				if len(stone) - 1 > r:
					r += 1
				if a == b:
					go = True
				a += 1
		elif stone[0] == '.':
			total_money += money_1
	print(total_money)
	total_money = 0


def main():
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		l = [list(input()) for i in range(n)]
		if y >= 2 * x:
			print(x * sum([l[i].count('.') for i in range(n)]))
		else:
			s = 0
			for i in range(n):
				j = 0
				while j < m - 1:
					if l[i][j] == l[i][j + 1] and l[i][j] == '.':
						s += 1
						j += 2
					else:
						j += 1
			print(y * s + x * (sum([l[i].count('.') for i in range(n)]) - 2 * s))
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = [list(input()) for i in range(n)]
	ans = 0
	if x * 2 <= y:
		for i in l:
			ans += i.count('.') * x
	elif m == 1:
		for i in l:
			ans += i.count('.') * x
	else:
		for i in range(n):
			for j in range(m - 1):
				if l[i][j] == '.':
					if l[i][j + 1] == '.':
						ans += y
						l[i][j] = '#'
						l[i][j + 1] = '#'
					else:
						l[i][j] = '#'
						ans += x
			if l[i][-1] == '.':
				ans += x
	print(ans)


def main_function():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		(matrix, ans) = ([], 0)
		if x <= y // 2:
			better = x * 2
		else:
			better = y
		for _ in range(n):
			matrix.append(input())
			ans += matrix[_].count('..') * better
			for i in range(matrix[_].count('..')):
				matrix[_] = matrix[_].replace('..', '')
			ans += matrix[_].count('.') * x
		print(f'{ans}\n')
main_function()


test_cases = int(input())
for z in range(test_cases):
	(n, m, x, y) = list(map(int, input().split()))
	pattern = []
	for _ in range(n):
		row = input()
		pattern.append(row)
	if y >= 2 * x:
		count = 0
		for i in range(n):
			for j in range(m):
				if pattern[i][j] == '.':
					count += 1
		price = count * x
	else:
		price = 0
		for i in range(n):
			count = 1
			for j in range(m - 1):
				if pattern[i][j] == '.':
					if pattern[i][j + 1] == '.':
						count += 1
					else:
						price += y * (count // 2)
						if count % 2 != 0:
							price += x
						count = 1
			if pattern[i][-1] != '*':
				price += y * (count // 2)
				if count % 2 != 0:
					price += x
	print(price)


for T in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		lis = list(input())
		arr.append(lis)
	cost = 0
	visit = [[False] * m for i in range(n)]
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.' and visit[i][j] == False:
				if j + 1 < m and arr[i][j + 1] == '.':
					if 2 * x > y:
						cost += y
					else:
						cost += 2 * x
					visit[i][j] = True
					visit[i][j + 1] = True
				else:
					cost += x
					visit[i][j] = True
	print(cost)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict as dd

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		ar = []
		for i in range(n):
			ar.append(list(input()))
		if 2 * x <= y:
			ans = 0
			for i in range(n):
				ans += ar[i].count('.')
			print(x * ans)
		else:
			ans = 0
			for i in range(n):
				j = 0
				ct = 0
				while j < m:
					flg = 1
					while j < m and ar[i][j] == '.':
						ct += 1
						j += 1
						flg = 0
					ans += ct // 2 * y
					ans += ct % 2 * x
					ct = 0
					if flg:
						j += 1
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import io, os
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	tiles = []
	for j in range(n):
		pq = [i for i in input()]
		tiles.append(pq)
	ans = 0
	for ii in range(n):
		visited = dict()
		for j in range(m):
			if j in visited:
				continue
			if tiles[ii][j] == '.':
				if j + 1 < m and tiles[ii][j + 1] == '.':
					ans += min(2 * x, y)
					visited[j] = 1
					visited[j + 1] = 1
				elif tiles[ii][j] == '.':
					visited[j] = 1
					ans += x
	print(ans)


T = int(input())
for _ in range(T):
	(n, m, X, Y) = map(int, input().split())
	ans = 0
	if X * 2 <= Y:
		grid = [input() for i in range(n)]
		ans = 0
		for i in range(n):
			ans += grid[i].count('.') * X
		print(ans)
		continue
	else:
		for _ in range(n):
			S = input()
			cnt = 0
			for i in range(m):
				if S[i] == '*':
					cnt = 0
					continue
				cnt += 1
				ans += X
				if cnt % 2 == 0:
					ans -= 2 * X
					ans += Y
		print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(i) for i in input().split()]
	s = []
	for i in range(n):
		s.append(input())
		s[i] += '*'
	ans = 0
	if y >= 2 * x:
		for i in s:
			for j in i:
				if j == '.':
					ans += x
	else:
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.' and s[i][j + 1] == '.':
					ans += y
					s[i] = s[i][:j + 1] + '*' + s[i][j + 2:]
				elif s[i][j] == '.':
					ans += x
	print(ans)


t = int(input())
for _ in range(t):
	(m, n, x1, x2) = map(int, input().split())
	if x1 * 2 < x2:
		x2 = 2 * x1
	cost = 0
	for row in range(m):
		c = 0
		line = input()
		while c < n:
			if c + 1 < n and line[c] == '.' and (line[c] == line[c + 1]):
				cost += x2
				c += 2
			elif line[c] == '.':
				cost += x1
				c += 1
			else:
				c += 1
	print(cost)


import math
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	p = 0
	for j in range(n):
		c = []
		s = input()
		for j in s:
			if j == '.':
				p += 1
			c.append(j)
		a.append(c)
	if y >= 2 * x:
		print(p * x)
	else:
		r = 0
		for i in range(n):
			j = 0
			while j < m:
				if j < m - 1 and a[i][j] == '.' and (a[i][j + 1] == '.'):
					r += y
					j += 2
				else:
					if a[i][j] == '.':
						r += x
					j += 1
		print(r)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(list(input()))
	a = []
	s = 0
	for i in range(n):
		c = 0
		for j in range(m):
			if l[i][j] == '.':
				c += 1
			else:
				if c != 0:
					s += 1
					a.append(c)
				c = 0
		if c != 0:
			s += 1
			a.append(c)
	ans = 0
	if 2 * x <= y:
		print(sum(a) * x)
	else:
		for i in range(s):
			d = a[i] // 2
			ans += d * y
			e = a[i] % 2
			ans += e * x
		print(ans)


t = int(input())
for z in range(t):
	a = [int(i) for i in input().split(' ')]
	n = a[0]
	m = a[1]
	x = a[2]
	y = a[3]
	k_1 = 0
	k_2 = 0
	for i in range(n):
		s = str(input())
		k = 0
		for j in range(m):
			if s[j] == '.':
				k += 1
				k_1 += 1
			else:
				k = 0
			if k == 2:
				k = 0
				k_2 += 1
	if x * 2 <= y or k_2 == 0:
		print(x * k_1)
	else:
		print(k_2 * y + (k_1 - k_2 * 2) * x)


arr = []
rra = []
for k in range(int(input())):
	arr.append([int(k) for k in input().split(' ')])
	l = []
	for k in range(arr[-1][0]):
		l.append(input())
	rra.append(l)
for line in range(len(arr)):
	(n, m, x, y) = arr[line]
	u = rra[line]
	cost = 0
	for p in u:
		if y < 2 * x:
			cost += p.count('..') * y
		else:
			cost += p.count('..') * x * 2
		p = p.replace('..', '')
		cost += p.count('.') * x
	print(cost)


ff = input()
nn = int(ff)
for jjj in range(0, nn, 1):
	import array
	arr = array.array('l', [])
	arr = [int(i) for i in input().split()]
	n = int(arr[0])
	m = int(arr[1])
	x1 = int(arr[2])
	y2 = int(arr[3])
	a = 0
	if 2 * x1 <= y2:
		a = 1
	ss = 0
	for jj in range(0, n, 1):
		p = input()
		lk = 0
		hhh = 0
		for ii in range(0, m, 1):
			cc = p[ii]
			if a == 1:
				if cc == '.':
					ss = ss + x1
			elif cc == '.':
				lk = lk + 1
			else:
				if lk % 2 == 0:
					ss = ss + y2 * (lk / 2)
				else:
					hjk = int(lk / 2)
					ss = ss + hjk * y2 + x1
				lk = 0
		if lk != 0:
			if lk % 2 == 0:
				ss = ss + y2 * (lk / 2)
			else:
				hjk = int(lk / 2)
				ss = ss + hjk * y2 + x1
	ljk = int(ss)
	print(ljk)


import math
s = int(input())
answers = []
for awq in range(s):
	(n, m, priceSmall, priceLarge) = list(map(int, input().split()))
	if priceLarge > priceSmall * 2:
		priceLarge = priceSmall * 2
	answer = 0
	for i in range(n):
		line = input()
		pices = line.split('*')
		for j in pices:
			if len(j) < 1:
				continue
			x = math.floor(len(j) / 2)
			x_ = x * priceLarge
			y = (len(j) - x * 2) * priceSmall
			answer += x_ + y
	answers.append(answer)
for answ in answers:
	print(answ)


t = int(input())
for caseNr in range(1, t + 1):
	a = list(map(int, input().split()))
	n = a[0]
	m = a[1]
	x = a[2]
	y = a[3]
	matrix = []
	for row in range(n):
		temp = str(input())
		matrix.append(list(temp))
	if y >= 2 * x:
		cost = 0
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == '.':
					cost += x
		print(cost)
	else:
		cost = 0
		for i in range(n):
			for j in range(1, m):
				if m >= 2 and matrix[i][j - 1] == '.' and (matrix[i][j] == '.'):
					cost += y
					matrix[i][j - 1] = '*'
					matrix[i][j] = '*'
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == '.':
					cost += x
		print(cost)


t = int(input())
for case in range(t):
	(n, m, x, y) = [int(s) for s in input().split(' ')]
	a = []
	for i in range(n):
		a.extend([len(z) for z in input().split('*')])
	tiles = 0
	for b in a:
		tiles += min(x * b, y * (b // 2) + x * (b % 2))
	print(tiles)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, stdin.readline().split())
	if y > 2 * x:
		y = 2 * x
	ans = 0
	for i in range(n):
		l = input()
		flag = 0
		for j in range(m):
			if l[j] == '.' and flag == 0:
				if j != m - 1 and l[j + 1] == '.':
					ans += y
					flag = 1
				else:
					ans += x
			elif flag == 1:
				flag = 0
	print(ans)


Answer = []
for _ in range(int(input())):
	(X, Sum) = (list(map(int, input().split())), 0)
	for i in range(X[0]):
		(Temp, index) = (input(), 0)
		while index < X[1]:
			if Temp[index] == '.':
				if index + 1 < X[1] and Temp[index + 1] == '.' and (X[-2] * 2 > X[-1]):
					index += 1
					Sum += X[-1]
				else:
					Sum += X[-2]
			index += 1
	Answer.append(str(Sum))
print('\n'.join(Answer))


tc = int(input())
for _ in range(tc):
	(n, m, x, y) = map(int, input().split(' '))
	price = 0
	mat = [[] * m] * n
	for i in range(n):
		mat[i] = list(input())
		for j in range(m):
			if mat[i][j] == '*':
				continue
			if mat[i][j] == '.':
				if j == 0:
					price += x
				elif mat[i][j - 1] == '.':
					if price - x + y < price + x:
						mat[i][j] = '*'
						price = price - x + y
					else:
						price = price + x
				else:
					price = price + x
	print(price)


def solve():
	a = [int(x) for x in input().split()]
	row = a[0]
	col = a[1]
	one = a[2]
	two = a[3]
	ans = 0
	mat = []
	for i in range(row):
		temp = input()
		arr = []
		for ele in temp:
			if ele != ' ':
				arr.append(ele)
		mat.append(arr)
	flag = 0
	for i in range(row):
		for j in range(col - 1):
			flag = 0
			if mat[i][j] == '.':
				if mat[i][j + 1] == '.':
					if 2 * one < two:
						ans = ans + 2 * one
						mat[i][j] = mat[i][j + 1] = '*'
					else:
						ans = ans + two
						mat[i][j] = mat[i][j + 1] = '*'
					flag = 1
				else:
					mat[i][j] = '*'
					ans = ans + one
			if flag == 1:
				j += 1
		if mat[i][col - 1] == '.':
			mat[i][col - 1] = '*'
			ans = ans + one
	return ans
t = int(input())
while t > 0:
	t -= 1
	print(solve())


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	d = []
	for i in range(n):
		s = input()
		d += [s]
	c = 0
	f = 0
	for i in range(n):
		e = 0
		for j in range(m):
			if d[i][j] == '.':
				e += 1
				f += 1
				if j == m - 1:
					if e == 1:
						c += a
					elif e % 2 == 0:
						c += e // 2 * min(b, 2 * a)
					elif e % 2 == 1:
						c += e // 2 * min(b, 2 * a)
						c += a
					e = 0
			elif d[i][j] == '*':
				if e == 1:
					c += a
				elif e % 2 == 0:
					c += e // 2 * min(b, 2 * a)
				elif e % 2 == 1:
					c += e // 2 * min(b, 2 * a)
					c += a
				e = 0
	print(min(c, f * a))


def ans(matrix, a, b, p1, p2):
	cheaper = min(p1 * 2, p2)
	price = 0
	curr = 0
	for i in range(a):
		for j in range(b):
			if matrix[i][j] == '.':
				curr += 1
			else:
				double = curr // 2 * cheaper
				single = curr % 2 * p1
				price += double + single
				curr = 0
		price += curr // 2 * cheaper + curr % 2 * p1
		curr = 0
	return price
t = int(input())
for _ in range(t):
	(a, b, p1, p2) = map(int, input().split())
	matrix = []
	for i in range(a):
		line = str(input())
		matrix.append(line)
	print(ans(matrix, a, b, p1, p2))


from math import ceil
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = [input() for _ in range(n)]
	ans = 0
	y = min(y, 2 * x)
	for i in arr:
		j = 0
		while j < m:
			if i[j] == '.' and j + 1 < m and (i[j + 1] == '.'):
				ans += y
				j += 1
			elif i[j] == '.':
				ans += x
			j += 1
	print(ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import math

def solve(t):
	(n, m, x, y) = map(int, input().strip().split())
	data = []
	for i in range(n):
		data.append(input().strip())
	longer_costly = y > 2 * x
	ret = 0
	for i in range(n):
		j = 0
		while j < m:
			l = 0
			while j < m and data[i][j] == '.':
				l += 1
				j += 1
			if longer_costly:
				ret += l * x
			else:
				ret += l // 2 * y + l % 2 * x
			j += 1
	return ret

def main():
	tc = int(input().strip())
	for t in range(1, tc + 1):
		print(solve(t))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())

def solve(s):
	f = len(s)
	dp = [0] * (f + 1)
	for i in range(f):
		if s[i] == '.':
			dp[i + 1] = dp[i] + x
			if i >= 1 and s[i - 1] == '.':
				dp[i + 1] = min(dp[i + 1], dp[i - 1] + y)
		else:
			dp[i + 1] = dp[i]
	return dp[f]
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for j in range(n):
		ans += solve(input())
	print(ans)


T = int(input())
for i in range(T):
	(n, m, x, y) = input().split()
	n = int(n)
	m = int(m)
	x = int(x)
	y = int(y)
	tiles = []
	values = []
	for line in range(n):
		a = []
		for k in range(m):
			a.append(0)
		values.append(a)
	for j in range(n):
		tiles.append(list(input()))
	sum = 0
	for j in range(n):
		if tiles[j][0] == '.':
			values[j][0] = x
		for k in range(1, m):
			if tiles[j][k] == '.':
				if tiles[j][k - 1] == '.':
					values[j][k] = min(values[j][k - 1] - x + y, values[j][k - 1] + x)
					tiles[j][k] = '*'
				else:
					values[j][k] = values[j][k - 1] + x
			else:
				values[j][k] = values[j][k - 1]
		sum += values[j][m - 1]
	print(sum)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	p = 0
	for j in range(n):
		a = str(input()) + 'o'
		for k in range(m):
			if a[k] == '.' and a[k + 1] == '.' and (y < 2 * x):
				p += y
				a = a[:k + 1] + 'o' + a[k + 2:]
			elif a[k] == '.':
				p += x
	print(p)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		f = 0
		while f <= m - 2:
			if s[f] == '.':
				if s[f + 1] == '.':
					ans += min(x * 2, y)
					f += 2
				else:
					ans += x
					f += 2
			else:
				f += 1
		if f == m - 1:
			if s[f] == '.':
				ans += x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	li = []
	dot = 0
	for i in range(n):
		s = input()
		li.append(s)
		dot += s.count('.')
	ans = 0
	if 2 * x > y:
		for i in li:
			ans += i.count('..') * y
			dot -= i.count('..') * 2
		ans += dot * x
	else:
		ans += dot * x
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(n, m, x, y) = map(int, input().split())
	MAP = [input().strip() + '*' for i in range(n)]
	ANS = 0
	for i in range(n):
		DP = [1 << 31] * (m + 1)
		DP[-1] = 0
		for j in range(m):
			if MAP[i][j] == '*':
				DP[j] = DP[j - 1]
			if MAP[i][j] == '.':
				DP[j] = min(DP[j], DP[j - 1] + x)
			if MAP[i][j] == '.' and MAP[i][j - 1] == '.':
				DP[j] = min(DP[j], DP[j - 2] + y)
		ANS += DP[m - 1]
	print(ANS)


import sys, heapq
from collections import *
from functools import lru_cache
import bisect

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split(' '))
		pair = True
		if x * 2 <= y:
			pair = False
		res = 0
		for i in range(n):
			s = input()
			cnt = 0
			for (j, ch) in enumerate(s + '*'):
				if ch == '.':
					cnt += 1
				elif ch == '*' and cnt > 0:
					if pair:
						res += cnt // 2 * y + (cnt & 1) * x
					else:
						res += cnt * x
					cnt = 0
		print(res)
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().rstrip().split()))
	ct_pair = 0
	ct_1 = 0
	prev = -1
	for i in range(n):
		s = input()
		l = list(s)
		for i in range(m):
			if l[i] == '.':
				ct_1 += 1
				if prev == 1:
					ct_pair += 1
					prev = -1
				else:
					prev = 1
			else:
				prev = -1
		prev = -1
	ans = min(ct_1 * x, (ct_1 - ct_pair * 2) * x + ct_pair * y)
	print(ans)


import sys
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	matrix = [list(input()) for i in range(n)]
	c1 = 0
	c2 = 0
	for i in range(n):
		j = 0
		while j < m:
			if matrix[i][j] == '.':
				c1 += 1
				if j + 1 < m and matrix[i][j + 1] == '.':
					c2 += 1
					c1 -= 1
					j += 1
			j += 1
	if 2 * x < y:
		print((c1 + 2 * c2) * x)
	else:
		print(c1 * x + c2 * y)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def S():
	return input().strip()

def print_list(l):
	print(' '.join(map(str, l)))
import math
for _ in range(N()):
	(n, m, x, y) = RL()
	y = min(y, x * 2)
	res = 0
	for _ in range(n):
		s = input().strip() + '#'
		i = 0
		while i < m:
			if s[i] == '.':
				res += y if s[i + 1] == '.' else x
				i += 2
			else:
				i += 1
	print(res)


t = int(input())
for q in range(t):
	ch = input()
	L = [int(i) for i in ch.split()]
	(n, m, x, y) = (L[0], L[1], L[2], L[3])
	s = 0
	for i in range(n):
		ch = input()
		L = [len(i) for i in ch.split('*')]
		for j in L:
			s += j // 2 * min(2 * x, y)
			s += j % 2 * x
	print(s)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		p = pairp = 0
		for i in range(n):
			row = input()
			pr = row.count('.')
			ppr = row.count('..')
			pairp += x * (pr - 2 * ppr) + y * ppr
			p += x * pr
		print(min(p, pairp))
main()


from itertools import groupby
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = [[] for i in range(n)]
	for i in range(n):
		s = input()
		for j in range(m):
			a[i].append(s[j])
	if y > 2 * x:
		res = 0
		for i in range(n):
			res += a[i].count('.') * x
		print(res)
	else:
		res = 0
		for i in range(n):
			a[i] = groupby(a[i])
			for (key, group) in a[i]:
				g = len(list(group))
				if key == '.':
					res += g // 2 * y + g % 2 * x
		print(res)


def solve(nmxy, rect):
	n = nmxy[0]
	m = nmxy[1]
	x = nmxy[2]
	y = nmxy[3]
	if 2 * x <= y:
		count = 0
		for i in range(n):
			for j in rect[i]:
				if j == '.':
					count += x
		return count
	else:
		count = 0
		for i in range(n):
			j = 0
			while j < m:
				if j < m - 1:
					if rect[i][j] == '.' and rect[i][j + 1] == '.':
						count += y
						j += 2
						continue
				if rect[i][j] == '.':
					count += x
					j += 1
				else:
					j += 1
		return count
t = int(input())
for i in range(t):
	nmxy = list(map(int, input().strip().split()))[:4]
	rect = []
	for j in range(nmxy[0]):
		sq = input()
		rect.append(sq)
	print(solve(nmxy, rect))


from sys import stdin, stdout
lis = lambda : list(map(int, input().split()))
Ma = lambda : map(int, input().split())
inte = lambda : int(input())
st = lambda : input()
import math
from collections import Counter
for i in range(inte()):
	(n, m, x, y) = Ma()
	a = []
	c = 0
	for i in range(n):
		a.append(st())
	if y >= 2 * x:
		for i in range(n):
			c += a[i].count('.') * x
	else:
		for i in range(n):
			j = 0
			while m - 1 > j:
				if a[i][j] == '.' and a[i][j + 1] == '.':
					c += y
					j += 2
				elif a[i][j] == '.' and a[i][j + 1] == '*':
					c += x
					j += 2
				else:
					j += 1
			if a[i][-1] == '.' and j == m - 1:
				c += x
	print(c)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	g = [input() for i in range(n)]
	(s, d) = (0, 0)
	for i in g:
		j = 0
		while j < m:
			if i[j] == '.':
				if j < m - 1 and i[j + 1] == '.':
					j += 1
					d += 1
				else:
					s += 1
			j += 1
	print(min(x * (s + d + d), x * s + y * d))


test = int(input())
for t in range(test):
	arr = input().split()
	n = int(arr[0])
	m = int(arr[1])
	x = int(arr[2])
	y = int(arr[3])
	matrix = []
	price = 0
	for i in range(n):
		p = input()
		l = []
		for j in range(m):
			l.append(p[j])
		matrix.append(l)
	if 2 * x <= y:
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == '.':
					matrix[i][j] = '*'
					price += x
	else:
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == '.':
					if j < m - 1 and matrix[i][j + 1] == '.':
						price += y
						matrix[i][j + 1] = '*'
						matrix[i][j] = '*'
					else:
						price += x
						matrix[i][j] = '*'
	print(price)


import math
for w in range(int(input())):
	n = list(map(int, input().split()))
	total = 0
	if n[2] * 2 < n[3]:
		n[3] = n[2] * 2
	for i in range(n[0]):
		a = list(input())
		for k in range(n[1] - 1):
			if a[k] == '.' and a[k + 1] == '.':
				total += n[3]
				a[k + 1] = '*'
			elif a[k] == '.':
				total += n[2]
			elif '.' not in a[k:]:
				break
		if a[-1] == '.':
			total += n[2]
	print(total)


t = int(input())
while t > 0:
	l = [int(x) for x in input().strip().split()]
	(n, m, x, y) = (l[0], l[1], l[2], l[3])
	grid = []
	for i in range(n):
		grid.append(input())
	isOneCheap = True if 2 * x <= y else False
	ans = 0
	for i in range(n):
		j = 0
		whiteTiles = 0
		while j < m:
			if grid[i][j] == '.':
				whiteTiles += 1
			else:
				if isOneCheap:
					ans += whiteTiles * x
				else:
					ans += whiteTiles // 2 * y
					if whiteTiles % 2 != 0:
						ans += x
				whiteTiles = 0
			j += 1
		if whiteTiles != 0:
			if isOneCheap:
				ans += whiteTiles * x
			else:
				ans += whiteTiles // 2 * y
				if whiteTiles % 2 != 0:
					ans += x
	print(ans)
	t -= 1


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	a = [input()[:-1] for _ in range(n)]
	ans = 0
	for ai in a:
		l = []
		cnt = 0
		for i in range(m):
			if ai[i] == '*':
				l.append(cnt)
				cnt = 0
			else:
				cnt += 1
		l.append(cnt)
		for c in l:
			ans += c // 2 * y + c % 2 * x
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	table = [[x for x in input()] for _ in range(n)]
	res = 0
	flag = 2 * x < y
	for i in range(n):
		strick = 0
		for j in range(m):
			if table[i][j] == '.':
				strick += 1
			elif strick:
				res += strick * x if flag else strick // 2 * y + strick % 2 * x
				strick = 0
		res += strick * x if flag else strick // 2 * y + strick % 2 * x
	print(res)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	z = min(c * 2, d)
	q = 0
	for i in range(a):
		s = input()
		w = s.count('.')
		e = s.count('..')
		q += e * z + (w - e * 2) * c
	print(q)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, stdin.readline().strip().split())
	mat = []
	for i in range(n):
		mat.append(input())
	ans = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if mat[i][j] == '.':
				cnt += 1
			else:
				if y <= 2 * x:
					ans = ans + cnt // 2 * y
					cnt = cnt % 2
					ans = ans + cnt * x
				else:
					ans = ans + cnt * x
				cnt = 0
		if cnt != 0 and y <= 2 * x:
			ans = ans + cnt // 2 * y
			cnt = cnt % 2
			ans = ans + cnt * x
		else:
			ans = ans + cnt * x
	print(ans)


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	(n, m, x, y) = map(int, stdin.readline().split())
	tiles = ''
	for _ in range(n):
		tiles += stdin.readline()
	c1 = tiles.count('.')
	c2 = tiles.count('..')
	if x * 2 <= y:
		print(c1 * x)
	else:
		print(c2 * y + (c1 - 2 * c2) * x)


def price(t1, t2, n, m, tiles):
	p = 0
	if t2 < t1 * 2:
		b2 = 1
	else:
		b2 = 0
	if b2 == 0:
		for x in range(n):
			for y in range(m):
				if tiles[x][y] == '.':
					p += t1
	else:
		for x in range(n):
			i = 0
			c = 0
			while i < m:
				if tiles[x][i] == '*':
					p += t2 * (c // 2)
					p += t1 * (c % 2)
					c = 0
				else:
					c += 1
				i += 1
			if c > 0:
				p += t2 * (c // 2)
				p += t1 * (c % 2)
	return p
te = int(input())
ans = []
for i in range(te):
	(n, m, t1, t2) = list(map(int, input().split()))
	tiles = []
	for j in range(n):
		tiles.append(list(input()))
	ans.append(price(t1, t2, n, m, tiles))
for u in ans:
	print(u)


t = int(input())
for i in range(t):
	(l, b, x, y) = map(int, input().split())
	flag = 0
	sum = 0
	for j in range(l):
		flag = 0
		n = input()
		n = list(n)
		if b == 1:
			if n[0] == '.':
				sum = sum + x
		else:
			k = 0
			while k < b - 1:
				if n[k] == '.' and n[k + 1] == '.':
					if k == b - 2:
						flag = 1
					if 2 * x >= y:
						sum = sum + y
						k = k + 2
					else:
						sum = sum + 2 * x
						k = k + 2
				elif n[k] == '.' and n[k + 1] == '*':
					sum = sum + x
					k = k + 1
				else:
					k = k + 1
			if flag == 0 and n[b - 1] == '.':
				sum = sum + x
	print(sum)


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(stdin.readline())):
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	t = []
	c = 0
	for _ in range(n):
		t.append(str(stdin.readline().strip('\n')))
	for i in t:
		dd = i.count('..')
		sd = i.count('.')
		sd = sd - 2 * dd
		if y >= 2 * x:
			c += sd * x + dd * (2 * x)
		else:
			c += sd * x + dd * y
	print(c)


t = int(input())
for i in range(t):
	(n, m, x, y) = [int(n) for n in input().split()]
	mat = []
	for i in range(n):
		arr = input()
		mat.append(arr)
		arr = []
	if 2 * x <= y:
		cost = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == '.':
					cost = cost + x
		print(cost)
	elif 2 * x > y:
		cost = 0
		for i in range(n):
			flag = 1
			for j in range(m):
				if j == 0:
					if mat[i][j] == '.':
						cost = cost + x
				elif j > 0:
					if mat[i][j] == '.' and mat[i][j - 1] != '.':
						cost = cost + x
						flag = 1
					elif mat[i][j] == '.' and mat[i][j] == '.' and (flag == 0):
						cost = cost + x
						flag = 1
					elif mat[i][j] == '.' and mat[i][j - 1] == '.' and (flag == 1):
						cost = cost + y - x
						flag = 0
		print(cost)


from sys import stdin
from math import ceil

def func():
	return
for _ in range(int(stdin.readline())):
	(n, m, x, y) = map(int, stdin.readline().split())
	cost = 0
	for i in range(n):
		arr = stdin.readline().strip()
		if 2 * x <= y:
			cost += arr.count('.') * x
		else:
			count = 0
			for ele in arr:
				if ele == '.':
					count += 1
				else:
					cost += count // 2 * y
					cost += count % 2 * x
					count = 0
			cost += count // 2 * y
			cost += count % 2 * x
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	single = double = 0
	v = []
	for i in range(n):
		s = input()
		v.append(s)
	for i in range(n):
		for j in range(m):
			if v[i][j] == '.':
				single += 1
	if 2 * x <= y:
		print(single * x)
	else:
		for i in range(n):
			j = 0
			while j < m - 1:
				if v[i][j] == '.' and v[i][j + 1] == '.':
					double += 1
					j += 2
				else:
					j += 1
		print(double * y + (single - 2 * double) * x)


t = int(input())
for i in range(t):
	(n, m, one, two) = map(int, input().split())
	onetile = 0
	twotile = 0
	for j in range(n):
		s = input()
		k = 0
		while k < m:
			if s[k] == '.':
				if k < m - 1 and s[k + 1] == '.':
					twotile += 1
					k += 2
				else:
					onetile += 1
					k += 1
			else:
				k += 1
	if 2 * one < two:
		print((2 * twotile + onetile) * one)
	else:
		print(one * onetile + two * twotile)


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		s = input()
		be = s.count('.')
		ch = m - be
		if x * 2 <= y:
			ans += be * x
			continue
		j = 0
		while j < m:
			if s[j] == '*':
				j += 1
				continue
			if j + 1 == m or s[j + 1] == '*':
				ans += x
				j += 1
				continue
			ans += y
			j += 2
	print(ans)


def adjacent_white(line):
	adjacents = numbers(line)
	total = 0
	for n_adjacent_white in adjacents:
		if n_adjacent_white == 0:
			continue
		if n_adjacent_white == 1:
			total += a
			continue
		if mapp.get(n_adjacent_white) != None:
			total += mapp.get(n_adjacent_white)
			continue
		if n_adjacent_white % 2 == 0:
			comb_a = [n_adjacent_white - x for x in range(0, n_adjacent_white + 1, 2)]
			comb_b = list(range(0, n_adjacent_white // 2 + 1))
		elif n_adjacent_white % 2 == 1:
			comb_a = list(range(n_adjacent_white, 0, -2))
			comb_b = list(range(0, n_adjacent_white // 2 + 1))
		res = min([a * x + b * y for (x, y) in zip(comb_a, comb_b)])
		mapp[n_adjacent_white] = res
		total += res
	return total

def numbers(line):
	l = []
	x = 0
	for j in line:
		if j == '*':
			l.append(x)
			x = 0
			continue
		x += 1
	l.append(x)
	return l
for _ in range(int(input())):
	(n, m, a, b) = [int(x) for x in input().split()]
	matrix = []
	mapp = {}
	for i in range(n):
		matrix.append(list(input()))
	total = 0
	for line in matrix:
		res = adjacent_white(line)
		total += res
	print(total)


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		cost = 0
		(n, m, x, y) = [int(x) for x in input().split()]
		for i in range(n):
			temp = input().split('*')
			row = []
			for t in temp:
				if len(t) >= 1:
					row.append(t)
			if 2 * x <= y:
				for r in row:
					cost += x * len(r)
			else:
				for r in row:
					cost += y * (len(r) // 2)
					cost += x * (len(r) % 2)
		print(cost)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	field = []
	for i in range(n):
		field.append(str(input()))
	if 2 * x <= y:
		E = 0
		for i in range(n):
			E += field[i].count('.')
		print(E * x)
	else:
		E = 0
		tt = -1
		for i in range(n):
			tt = -1
			for j in range(m):
				if field[i][j] == '.':
					if tt == -1:
						E += x
					else:
						E -= x
						E += y
					tt = -tt
				else:
					tt = -1
		print(E)


from sys import stdin, stdout
T = int(stdin.readline().strip())

def ceil(x, y):
	s = x // y
	if x % y != 0:
		s += 1
	return s
for caso in range(T):
	(n, m, x, y) = map(int, stdin.readline().strip().split())
	mat = [stdin.readline().strip() for i in range(n)]
	if x * 2 <= y:
		c = 0
		for i in mat:
			c += i.count('.')
		stdout.write('%d\n' % (c * x))
		continue
	ans = 0
	for i in range(n):
		x1 = 0
		for j in range(m):
			if mat[i][j] == '.':
				x1 += 1
			else:
				q = x1 // 2
				ans += q * y
				if x1 % 2 != 0:
					ans += x
				x1 = 0
		q = x1 // 2
		ans += q * y
		if x1 % 2 != 0:
			ans += x
		x1 = 0
	stdout.write('%d\n' % ans)


import sys
TEST = 0
if TEST == 1:
	input_file = open('inputB.txt', 'r')
	input_from_file = input_file.readlines()

def read_int():
	if TEST:
		return int(input_from_file.pop(0))
	else:
		return int(sys.stdin.readline())

def read_ints():
	if TEST:
		return list(map(int, input_from_file.pop(0).rstrip().split()))
	else:
		return list(map(int, sys.stdin.readline().split()))

def read_str():
	if TEST:
		in1 = input_from_file.pop(0).rstrip()
	else:
		in1 = sys.stdin.readline().rstrip()
	return in1
t = read_int()
for test in range(t):
	(n, m, x, y) = read_ints()
	ans = 0
	for _ in range(n):
		stroka = read_str()
		if y < 2 * x:
			num2 = 0
			num1 = 0
			numWhite = 0
			for c in stroka:
				if c is '.':
					numWhite += 1
				else:
					if numWhite > 0:
						num2 += numWhite // 2
						num1 += numWhite % 2
					numWhite = 0
			if numWhite > 0:
				num2 += numWhite // 2
				num1 += numWhite % 2
			ans += num1 * x + num2 * y
		else:
			numWhite = sum([1 for x in stroka if x is '.'])
			ans += numWhite * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = [int(i) for i in input().split()]
	lst = []
	for i in range(0, n):
		lst.append(input())
	if x * 2 <= y:
		sum1 = 0
		for i in lst:
			for j in i:
				if j == '.':
					sum1 = sum1 + x
		print(sum1)
	else:
		sum1 = 0
		for i in lst:
			a = i.count('.')
			b = i.count('..')
			sum1 = sum1 + b * y + (a - b * 2) * x
		print(sum1)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	b = []
	c = 0
	p = 0
	t = 0
	for i in range(n):
		a = list(input())
		k = 0
		j = 0
		for j in range(len(a)):
			if a[j] == '.':
				t += x
			if a[j] == '.' and k == 0:
				c += x
				k += 1
			elif a[j] == '.' and k == 1:
				c += y
				c -= x
				k = 0
			else:
				k = 0
	print(min(c, t))


t = int(input())
while t > 0:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	if x * 2 <= y:
		y = x * 2
	mat = []
	tile = 0
	sum = 0
	for i in range(n):
		p = input()
		mat.append(p)
	for i in range(n):
		tile = 0
		p = mat[i]
		l = len(p)
		for j in range(l):
			if p[j] == '.':
				if tile == 0:
					tile = 1
				elif tile == 1:
					sum += y
					tile = 0
			elif tile == 1:
				sum += x
				tile = 0
		if tile == 1:
			sum += x
			tile = 0
	print(sum)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	S = []
	A1 = 0
	for i in range(n):
		s = input()
		s = list(s)
		S.append(s)
		A1 += s.count('.') * x
	A2 = 0
	for i in range(0, n):
		for j in range(0, m - 1):
			if S[i][j] == '.' and S[i][j + 1] == '.':
				S[i][j] = '*'
				S[i][j + 1] = '*'
				A2 += y
	for i in range(n):
		for j in range(m):
			if S[i][j] == '.':
				A2 += x
	print(min(A1, A2))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	count = 0
	for i in range(n):
		patt = input().split('*')
		if y < 2 * x:
			for i in range(len(patt)):
				count += len(patt[i]) // 2 * y + len(patt[i]) % 2 * x
		else:
			for i in range(len(patt)):
				count += len(patt[i]) * x
	print(count)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().strip().split())
	price = 0
	best_option = min(2 * x, y)
	for j in range(n):
		s = input()
		current_seq = 0
		for element in s:
			if element == '.':
				current_seq += 1
			else:
				if current_seq % 2 == 0:
					price += current_seq // 2 * best_option
				else:
					price += (current_seq - 1) // 2 * best_option + x
				current_seq = 0
		if current_seq % 2 == 0:
			price += current_seq // 2 * best_option
		else:
			price += (current_seq - 1) // 2 * best_option + x
	print(price)


for _ in range(int(input())):
	(a, b, x, y) = map(int, input().split())
	ans = 0
	for _ in range(a):
		s = input()
		h = s.count('.')
		p = s.count('..')
		if 2 * x <= y:
			ans += h * x
		else:
			ans += p * y + (h - 2 * p) * x
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	x1 = 0
	y1 = 0
	for i in range(n):
		a = input()
		y1 += a.count('..')
		x1 += a.count('.')
	if y < 2 * x:
		print(y1 * y + x * (x1 - 2 * y1))
	else:
		print(x1 * x)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		y = 2 * x
	z = []
	for i in range(n):
		l = list(map(str, input().split('*')))
		z.append(l)
	c1 = 0
	c2 = 0
	for i in range(n):
		for j in range(len(z[i])):
			l1 = len(z[i][j])
			if l1 == 1:
				c1 += 1
			else:
				c2 += l1 // 2
				c1 += l1 % 2
	count = c1 * x + c2 * y
	print(count)


def solution():
	(a, b, c, d) = map(int, input().split())
	ans = 0
	for loop in range(a):
		di = input().split('*')
		k = min(c * 2, d)
		for i in di:
			ans += len(i) // 2 * k + len(i) % 2 * c
	print(int(ans))
t = int(input())
for loop in range(t):
	solution()


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if x <= y / 2:
		for i in range(n):
			t1 = input()
			for j in t1:
				if j == '.':
					cost = cost + x
	else:
		for i in range(n):
			t1 = input()
			q = 0
			for j in t1:
				if j == '.' and q == 1:
					cost = cost - x
					cost = cost + y
					q = 0
				elif j == '.':
					cost = cost + x
					q = 1
				else:
					q = 0
	print(cost)


from sys import stdin
from math import ceil
input = stdin.readline

def list_input():
	return list(map(int, input().split()))

def sep_input():
	return map(int, input().split())
for _ in range(int(input())):
	(n, m, x, y) = sep_input()
	l = []
	for i in range(n):
		l.append([i for i in input()[:-1]])
	i = 0
	c = 0
	while i < n:
		j = 0
		while j < m:
			if l[i][j] == '.':
				if j != m - 1 and l[i][j + 1] == '.':
					c += min(2 * x, y)
					j += 2
					continue
				else:
					c += x
					j += 1
					continue
			j += 1
		i += 1
	print(c)


from sys import stdin
input = stdin.readline

def licz(row, x1, x2):
	row = [-1] + [i for (i, e) in enumerate(row) if e == '*'] + [len(row)]
	pyk = [x - y - 1 for (x, y) in zip(row[1:], row[:-1])]
	return sum([a // 2 * x2 + (a - 2 * (a // 2)) * x1 for a in pyk])
q = int(input())
for _ in range(q):
	(r, c, x1, x2) = map(int, input().split())
	x2 = min(x2, 2 * x1)
	print(sum([licz(input().strip(), x1, x2) for i in range(r)]))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())

	def go(s):
		c = [1000010000] * (m + 1)
		c[0] = 0
		for i in range(1, m + 1):
			if '.' == s[i - 1]:
				c[i] = c[i - 1] + x
				if i - 2 >= 0 and '.' == s[i - 2]:
					c[i] = min(c[i], c[i - 2] + y)
			else:
				c[i] = c[i - 1]
		return c[m]
	print(sum((go(s) for s in (input() for _ in range(n)))))


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	(N, M, x, y) = map(int, input().split())
	state = [list(input().rstrip()) for _ in range(N)]
	Query.append((N, M, x, y, state))
for (N, M, x, y, state) in Query:
	white = 0
	two = 0
	for r in range(N):
		go = False
		for c in range(M):
			if go:
				go = False
				continue
			if c < M - 1 and state[r][c] == '.' and (state[r][c + 1] == '.'):
				go = True
				two += 1
			elif state[r][c] == '.':
				white += 1
	print(min(two * y + white * x, (2 * two + white) * x))


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	y = min(2 * x, y)
	cost = 0
	for i in range(n):
		tem = list(input())
		j = 0
		while j < len(tem):
			if j < len(tem) - 1 and tem[j] == '.' and (tem[j + 1] == '.'):
				cost += y
				j += 2
			elif tem[j] == '.':
				cost += x
				j += 1
			else:
				j += 1
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append([len(k) for k in list(map(str, input().split('*')))])
	if 2 * x <= y:
		print(sum([sum(kk) for kk in a]) * x)
		continue
	ans = 0
	for tt in a:
		for jj in tt:
			q = jj // 2
			r = jj % 2
			ans = ans + q * y + r * x
	print(ans)


try:
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		c1 = 0
		c2 = 0
		for j in range(n):
			s = input()
			t1 = s.count('.')
			t2 = s.count('..')
			c1 += t2 * y + (t1 - 2 * t2) * x
			c2 += t1 * x
		print(min(c1, c2))
except:
	pass


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(single, double) = (0, 0)
	for _ in range(n):
		a = input()
		a = list(a)
		for i in range(m):
			if a[i] == '.':
				single += 1
		i = 0
		while i < m - 1:
			if a[i] == '.' and a[i + 1] == '.':
				double += 1
				i += 2
				single -= 2
			else:
				i += 1
	singlecost = single * x
	if x < y / 2:
		doublecost = double * 2 * x
	else:
		doublecost = double * y
	print(singlecost + doublecost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	if 2 * x <= y:
		f = 1
	else:
		f = 0
	for i in range(n):
		a.append(input())
	if f == 1:
		count = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					count += 1
		print(x * count)
	else:
		s = 0
		j = 0
		for i in range(n):
			j = 0
			while j < m:
				if j != m - 1:
					if a[i][j] == '.' and a[i][j + 1] == '.':
						s += y
						j += 1
					elif a[i][j] == '.':
						s += x
				elif a[i][j] == '.':
					s += x
				j += 1
		print(s)


t = int(input())
for T in range(t):
	L = 0
	(n, m, x, y) = map(int, input().split())
	A = [0] * n
	for i in range(n):
		A[i] = [0] * m
	for i in range(n):
		s = input()
		for j in range(m):
			A[i][j] = s[j]
	for i in range(n):
		for j in range(m):
			if A[i][j] == '.':
				if 2 * x <= y:
					L += x
					A[i][j] = '*'
				elif j != m - 1:
					if A[i][j + 1] == '.':
						L += y
						A[i][j + 1] = '*'
					else:
						L += x
						A[i][j] = '*'
				elif j == m - 1:
					L += x
					A[i][j] = '*'
	print(L)


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
t = R2()
for _ in range(t):
	(n, m, x, y) = R1()
	s = 0
	c_1 = c_2 = 0
	for _ in range(n):
		c = 0
		for pl in input():
			if pl == '.':
				if c == 0:
					c = 1
					c_1 += 1
				else:
					c = 0
					c_1 -= 1
					c_2 += 1
			else:
				c = 0
	s = c_1 * x
	s += c_2 * y if y / 2 < x else c_2 * 2 * x
	print(s)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	dot = 0
	for j in range(n):
		S = list(input())
		S.append('*')
		for i in S:
			if i == '.':
				dot += 1
			if i == '*':
				if dot & 1 == 0:
					if x > y:
						cost += min((y * int(dot / 2), x * dot))
					else:
						cost += min(x * dot, y * int(dot / 2))
				if dot & 1 == 1:
					if dot == 1:
						cost += x
					elif x > y:
						cost += y * int(dot / 2) + x
					else:
						cost += min(y * int(dot / 2) + x, x * dot)
				dot = 0
	print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	if y > x * 2:
		y = x * 2
	sm = 0
	for j in range(n):
		st = input()
		kol = 0
		kol2 = 0
		for k in range(len(st)):
			if st[k] == '.':
				kol += 1
				kol2 += 1
			else:
				if kol2 > 0:
					sm = sm + x * kol2
					kol = 0
					kol2 = 0
				kol = 0
			if kol == 2:
				kol = 0
				kol2 = 0
				sm += y
		if kol2 > 0:
			sm = sm + x * kol2
			kol = 0
	print(sm)


from sys import stdin

def solve():
	return

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		(n, m, x, y) = map(int, inp().split())
		ans = 0
		if 2 * x <= y:
			for i in range(n):
				h = inp().strip()
				for j in range(m):
					if h[j] == '.':
						ans += x
		else:
			for i in range(n):
				h = inp().strip()
				c = 0
				for j in range(m):
					if h[j] == '.':
						c += 1
					else:
						ans += c // 2 * y + c % 2 * x
						c = 0
				ans += c // 2 * y + c % 2 * x
		print(ans)
main()


from sys import stdin
for t in range(int(input())):
	(n, m, x, y) = map(int, stdin.readline().split())
	s = [0] * n
	for i in range(n):
		s[i] = stdin.readline().strip()
	if 2 * x <= y:
		ct = 0
		for i in range(n):
			for j in range(m):
				if s[i][j] == '.':
					ct += 1
		print(ct * x)
	else:
		ans = 0
		for i in range(n):
			j = 0
			while j < m:
				if s[i][j] == '.':
					if j != m - 1 and s[i][j + 1] == '.':
						ans += y
						j += 1
					else:
						ans += x
				j += 1
		print(ans)


import math
T = int(input())
for t in range(T):
	(n, m, x, y) = map(int, input().strip().split())
	pave = []
	for i in range(n):
		pave.extend(input().split('*'))
	tiles = []
	for p in pave:
		if p == '.' or p == '..':
			tiles.append(p)
		else:
			txo = len(p) // 2
			oxo = len(p) % 2
			for i in range(txo):
				tiles.append('..')
			for j in range(oxo):
				tiles.append('.')
	cost = 0
	betterfor2 = min(2 * x, y)
	for t in tiles:
		if t == '.':
			cost += x
		elif t == '..':
			cost += betterfor2
	print(cost)


from math import ceil
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	c = 0
	l = []
	for _ in range(n):
		s = input()
		l.append(s)
	for i in range(n):
		j = 0
		while j < m:
			if l[i][j] == '.':
				if j == m - 1:
					c = c + x
					j += 1
				elif j < m - 1 and l[i][j + 1] == '.':
					c = c + min(2 * x, y)
					j += 2
				else:
					c = c + x
					j += 1
			else:
				j += 1
	print(c)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	costo = 0
	for _ in range(n):
		fila = input()
		i = 0
		while i != m:
			if fila[i] == '.':
				if i + 1 != m:
					if fila[i] == '.' and fila[i + 1] == '.' and (y < x * 2):
						costo += y
						i += 2
					else:
						i += 1
						costo += x
				else:
					i += 1
					costo += x
			else:
				i += 1
	print(costo)


import sys
import heapq
import math

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, input().split())

def rlinput():
	return list(map(int, input().split()))

def srlinput():
	return sorted(list(map(int, input().split())))

def main():
	(h, c, t) = rinput()
	if t >= h:
		print(1)
	elif 2 * t == h + c:
		print(2)
	else:
		x = (t - h) / (2 * t - h - c)
		print(x)

def main1():
	(n, m, x, y) = rinput()
	res = 0
	for i in range(n):
		q = list(input())
		fl = False
		for i in range(m):
			if fl:
				fl = False
			elif i + 1 == m:
				res += x * (q[i] == '.')
			elif q[i] == '.':
				fl = q[i + 1] == '.'
				res += fl * min(2 * x, y) + (not fl) * x
	print(res)
for i in range(iinput()):
	main1()


tests = int(input())
for i in range(tests):
	numbers = input().split(' ')
	n = int(numbers[0])
	m = int(numbers[1])
	x = int(numbers[2])
	y = int(numbers[3])
	one = 0
	pair = 0
	for j in range(n):
		line = input()
		counter = 0
		for z in range(m):
			if line[z] == '.':
				counter += 1
			else:
				one += counter % 2
				pair += counter // 2
				counter = 0
		one += counter % 2
		pair += counter // 2
	if 2 * x > y:
		print(one * x + pair * y)
	else:
		print(one * x + pair * 2 * x)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append(list(input()))
	if x * 2 <= y:
		cnt = 0
		for i in a:
			cnt += i.count('.')
		print(x * cnt)
	else:
		ans = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '*':
					continue
				if j == m - 1:
					ans += x
					continue
				if a[i][j + 1] == '.':
					a[i][j] = '*'
					a[i][j + 1] = '*'
					ans += y
				else:
					a[i][j] = '*'
					ans += x
		print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	matrix = []
	for i in range(n):
		matrix.append(input())
	if x * 2 < y:
		y = x * 2
	ans = 0
	new = []
	for s in matrix:
		for i in range(1, len(s)):
			if s[i] == '.' and s[i - 1] == '.':
				s = s[:i - 1] + '**' + s[i + 1:]
				ans += y
		new.append(s)
	for s in new:
		for i in range(len(s)):
			if s[i] == '.':
				ans += x
	print(ans)


import math
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split(' '))
	costOne = 0
	costTwo = 0
	finalCost = 0
	for i in range(n):
		row = input()
		countTwos = row.count('..')
		countW = row.count('.')
		countOnes = countW - 2 * countTwos
		costOne = countW * x
		costTwo = countTwos * y + countOnes * x
		finalCost += min(costOne, costTwo)
	print(finalCost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	for _ in range(n):
		s = input()
		temp = list(s)
		l.append(temp)
	cost = 0
	for i in range(n):
		for j in range(m):
			if j < m - 1:
				if l[i][j] == '.':
					if l[i][j + 1] == '.':
						cost = cost + min(2 * x, y)
						l[i][j + 1] = '*'
					else:
						cost = cost + x
			elif j == m - 1 and l[i][j] == '.':
				cost = cost + x
	print(cost)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	matrix = list(('' for j in range(n)))
	pop = 0
	for j in range(n):
		matrix[j] = input()
		for h in matrix[j]:
			if h == '.':
				pop += 1
	if x * 2 < y:
		print(pop * x)
		continue
	ans = 0
	dic = set()
	for j in range(n):
		for h in range(m):
			if h < m - 1:
				if not (j, h) in dic and (not (j, h + 1) in dic) and (matrix[j][h] == '.') and (matrix[j][h + 1] == '.'):
					ans += y
					dic.add((j, h))
					dic.add((j, h + 1))
					continue
			if not (j, h) in dic and matrix[j][h] == '.':
				ans += x
	print(ans)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	(n, m, p, q) = li()
	a = []
	for i in range(n):
		a.append(list(input().strip()))
	ans = 0
	for i in range(n):
		c = 0
		for j in range(m):
			if a[i][j] == '.':
				c += 1
			else:
				if c % 2:
					ans += p
					c -= 1
				if p * 2 <= q:
					ans += c * p
				else:
					ans += q * c // 2
				c = 0
		if c > 0:
			if c % 2:
				ans += p
				c -= 1
			if p * 2 <= q:
				ans += c * p
			else:
				ans += q * c // 2
	print(ans)
for _ in range(I()):
	solve()


from sys import stdin, stdout
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, stdin.readline().split())
	tot = 0
	for row in range(n):
		row = stdin.readline().strip()
		i = 0
		while i < m:
			if row[i] == '*':
				i += 1
				continue
			elif i + 1 < m and row[i + 1] == '.':
				tot += min(2 * x, y)
				i += 2
			else:
				tot += x
				i += 1
	print(tot)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(list(input()))
	cnt = 0
	single = 0
	for i in range(n):
		j = 0
		for k in range(m):
			if j < m - 1 and l[i][j] == '.' and (l[i][j + 1] == '.'):
				cnt += 1
				j += 1
			j += 1
			if l[i][k] == '.':
				single += 1
	if y >= 2 * x:
		print(single * x)
	else:
		print(cnt * y + x * (single - cnt * 2))


def main():
	t = int(input())
	for i in range(t):
		solve()

def solve():
	(n, m, x, y) = list(map(int, input().split(' ')))
	arr = [list(input()) for _ in range(n)]
	count = 0
	if 2 * x <= y:
		for i in arr:
			for j in i:
				if j == '.':
					count += x
	else:
		for row in arr:
			current = 0
			while current < m - 1:
				if row[current] == '.' and row[current + 1] == '.':
					count += y
					current += 1
				elif row[current] == '.':
					count += x
				current += 1
			if current < m and row[current] == '.':
				count += x
	print(count)
main()


import math
for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	sq = [list(input()) for _ in range(n)]
	tc = min(2 * x, y)
	res = 0
	for z in sq:
		i = 0
		while i < m:
			if i == m - 1:
				if z[i] == '.':
					res += x
					i += 1
				else:
					i += 1
			elif z[i] == '.':
				if z[i + 1] == '.':
					res += tc
					i += 2
				else:
					res += x
					i += 1
			else:
				i += 1
	print(res)


from sys import stdin
import math
t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, stdin.readline().rstrip().split(' ')))
	grid = []
	for _ in range(n):
		temp = stdin.readline().rstrip()
		grid.append(temp)
	totalCost = 0
	if x * 2 <= y:
		for row in grid:
			for ele in row:
				if ele == '.':
					totalCost += x
	else:
		for row in grid:
			prev = None
			for ele in row:
				if ele == '.' and prev == '.':
					totalCost += y
					totalCost -= x
					prev = None
				elif ele == '.':
					totalCost += x
					prev = '.'
				else:
					prev = None
	print(totalCost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	A = []
	for i in range(n):
		A.append(input())
	a = '..'
	b = '.'
	O = []
	for i in range(len(A)):
		Z = A[i].count(a)
		Y = A[i].count(b)
		Y = Y - 2 * Z
		j = min(2 * x, y)
		if Y < 0:
			Y = 0
		O.append(Y * x + Z * j)
	print(sum(O))


def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = map(int, input().split())
		if 2 * x < y:
			y = 2 * x
		cnt = 0
		for i in range(n):
			s = input()
			j = 0
			while j < m:
				if s[j] == '.':
					if j < m - 1 and s[j + 1] == '.':
						j += 2
						cnt += y
					else:
						j += 1
						cnt += x
				else:
					j += 1
		print(cnt)
main()


t = int(input())
for _ in range(t):
	(n, m, a, b) = map(int, input().split())
	cost = 0
	flag = 0
	for i in range(n):
		s = input()
		count = 0
		for j in s:
			if j == '.':
				count += 1
				flag += 1
			else:
				cost = cost + count // 2 * b + count % 2 * a
				count = 0
		if count != 0:
			cost = cost + count // 2 * b + count % 2 * a
	print(min(cost, flag * a))


T = int(input())
for _ in range(T):
	(n, m, x, y) = map(int, input().split())
	road = []
	price = 0
	for _ in range(n):
		tmp = input()
		road.append(tmp)
	for row in road:
		parts = row.split('*')
		for segment in parts:
			tmp1 = segment.count('.') * x
			tmp2 = len(segment) // 2 * y + (len(segment) - len(segment) // 2 * 2) * x
			price += min(tmp1, tmp2)
	print(price)


def main():
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = [int(x) for x in input().split()]
		arr = []
		for i in range(n):
			arr.append(input())
		if n == 1 and m == 1:
			if arr[0][0] != '*':
				print(x)
			else:
				print(0)
			continue
		sum = 0
		for i in range(n):
			prev = 0
			j = 0
			while j < m:
				if arr[i][j] == '.':
					j += 1
					continue
				if arr[i][j] == '*':
					new_j = j
					no_2 = (new_j - prev) // 2
					no_1 = new_j - prev - no_2 * 2
					sum += no_1 * x + min(no_2 * 2 * x, no_2 * y)
					prev = new_j + 1
					j += 1
			if arr[i][j - 1] == '.':
				new_j = m
				no_2 = (new_j - prev) // 2
				no_1 = new_j - prev - no_2 * 2
				sum += no_1 * x + min(no_2 * 2 * x, no_2 * y)
		print(sum)
main()


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	s = []
	st = ''
	for j in range(n):
		st = input()
		s.append(st)
	d = 0
	t = 0
	for j in range(n):
		for k in range(m):
			if s[j][k] == '.':
				d += 1
	for j in range(n):
		k = 0
		while k < m:
			if k + 1 < m and s[j][k] == '.' and (s[j][k + 1] == '.'):
				k = k + 2
				t += 1
			else:
				k += 1
	dt = d
	dt = dt - t * 2
	two = 999999999
	one = d * x
	if t > 0:
		two = t * y + dt * x
	if one > two:
		print(two)
	else:
		print(one)
	print()


inp = input
for _ in range(int(inp())):
	(n, m, x, y) = map(int, inp().strip().split(' '))
	single = False
	if 2 * x <= y:
		single = True
	rows = []
	for __ in range(n):
		rows.append(inp().strip())
	ans = 0
	if single:
		for row in rows:
			sats = row.count('.')
			ans += x * sats
	else:
		for row in rows:
			i = 0
			while i < m:
				if i < m - 1 and row[i] == row[i + 1] and (row[i] == '.'):
					ans += y
					i += 2
				elif row[i] == '.':
					ans += x
					i += 1
				else:
					i += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	ans1 = 0
	ans2 = 0
	for j in range(n):
		t = input()
		single = t.count('.')
		double = t.count('..')
		extra = t.count('.*.')
		ans2 = ans2 + x * single
		if single - 2 * double > 0:
			ans1 = ans1 + x * (single - 2 * double) + y * double
		else:
			ans1 = ans1 + y * double
	print(min(ans1, ans2))


try:
	for _ in range(int(input())):
		(n, m, x, y) = map(int, input().split())
		mat = []
		w = 0
		b = 0
		for i in range(n):
			r = input()
			row = []
			for c in r:
				if c == '.':
					row.append(0)
					w += 1
				else:
					row.append(1)
			mat.append(row)
		if 2 * x <= y:
			ans = w * x
			print(ans)
			continue
		ans = 0
		for i in range(n):
			for j in range(m):
				if mat[i][j] == 1:
					continue
				if j == m - 1:
					ans += x
					continue
				if mat[i][j + 1] == 0:
					ans += y
					mat[i][j + 1] = 1
					continue
				else:
					ans += x
		print(ans)
except Exception as e:
	print(e)
	pass


n = int(input())
for _ in range(n):
	(n, m, x, y) = map(int, input().split())
	arr = [[0 for i in range(m)] for j in range(n)]
	for i in range(0, n):
		arr[i] = list(map(str, str(input())))
	ans = 0
	cout = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if y >= 2 * x:
				if arr[i][j] == '.':
					ans += x
			elif arr[i][j] == '.':
				if j + 1 < m:
					if arr[i][j + 1] == '.':
						ans += y
						j += 1
					else:
						ans += x
				else:
					ans += x
			j += 1
		i += 1
	if cout == 0:
		print(ans)
	else:
		print()


t = int(input())
import math
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		s = input()
		mat.append([char for char in s])
	whites = []
	for i in mat:
		a = []
		c = 0
		for j in i:
			if j == '.':
				c += 1
			elif c != 0:
				a.append(c)
				c = 0
		if c != 0:
			a.append(c)
		whites.append(a)
	c1 = 0
	c2 = 0
	ans = 0
	for i in whites:
		c1 = 0
		c2 = 0
		for j in i:
			c1 += x * int(j)
		for j in i:
			if j % 2 == 0:
				c2 += j // 2 * y
			else:
				c2 += j // 2 * y + x
		ans += min(c1, c2)
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = [input() for i in range(n)]
	ans = 0
	d = [[False for j in range(m)] for i in range(n)]
	for i in range(n):
		for j in range(m):
			if s[i][j] == '.':
				ans += x
			else:
				d[i][j] = True
	temp = 0
	for i in range(n):
		for j in range(m - 1):
			if s[i][j] == '.' and d[i][j] == False:
				if s[i][j + 1] == '.':
					temp += y
					d[i][j] = True
					j += 1
					d[i][j] = True
				else:
					temp += x
					d[i][j] = True
					j += 1
		if s[i][m - 1] == '.' and d[i][m - 1] == False:
			temp += x
	ans = min(ans, temp)
	print(ans)


from sys import stdin
from math import ceil

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	(n, m, x, y) = minput()
	mat = []
	for i in range(n):
		mat.append(sinput())
	cost = 0
	for i in range(n):
		j = 0
		while j < m:
			if mat[i][j:j + 2] == '..':
				cost += 2 * x if 2 * x < y else y
				j += 2
			elif mat[i][j] == '.':
				cost += x
				j += 1
			else:
				j += 1
	print(cost)


test = int(input())
for _ in range(test):
	(n, m, x, y) = map(int, input().split())
	pav = []
	for i in range(n):
		col = input()
		pav.append(col)
	y = min(y, 2 * x)
	cost = 0
	for i in range(n):
		j = 0
		while j < m:
			if pav[i][j] == '.':
				if j < m - 1 and pav[i][j + 1] == '.':
					cost += y
					j += 2
				else:
					cost += x
					j += 1
			else:
				j += 1
	print(cost)


t = int(input())
for x in range(t):
	l1 = [int(x) for x in input().split()]
	(rows, columns, single, double) = (l1[0], l1[1], l1[2], l1[3])
	ans = 0
	if single * 2 < double:
		for x in range(rows):
			l2 = list(input())
			ans += l2.count('.')
		print(ans * single)
	else:
		l2 = []
		for x in range(rows):
			l2.append(list(input()))
		for x in range(rows):
			for y in range(columns):
				if l2[x][y] == '.':
					if y < columns - 1 and l2[x][y + 1] == '.':
						l2[x][y] = '#'
						l2[x][y + 1] = '#'
						ans += double
					elif y > 0 and l2[x][y - 1] == '.':
						l2[x][y] = '#'
						l2[x][y - 1] = '#'
						ans += double
					else:
						l2[x][y] = '#'
						ans += single
		print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	path = []
	for i in range(n):
		path.append(input())
	ans = 0
	if y > 2 * x:
		for i in range(n):
			ans += x * path[i].count('.')
		print(ans)
	else:
		for i in range(n):
			ctr = 0
			for j in range(m):
				if path[i][j] == '.':
					ctr += 1
				else:
					ans += ctr // 2 * y + ctr % 2 * x
					ctr = 0
			ans += ctr // 2 * y + ctr % 2 * x
		print(ans)


for t in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	out = 0
	count = 0
	for r in range(n):
		s = input()
		stack = 0
		for i in s:
			if i == '.':
				stack += 1
			else:
				count += stack
				out += stack // 2 * y
				out += stack % 2 * x
				stack = 0
		out += stack // 2 * y
		out += stack % 2 * x
		count += stack
	print(min(out, count * x))


for _ in range(int(input())):
	(n, m, p1, p2) = map(int, input().split())
	if 2 * p1 <= p2:
		cnt = 0
		for i in range(n):
			li = list(input())
			for (j, val) in enumerate(li):
				if val == '.':
					cnt += 1
		print(cnt * p1)
	else:
		tile2 = 0
		tile1 = 0
		for i in range(n):
			li = list(input())
			start = 0
			end = 0
			f = 0
			h = 0
			for (j, val) in enumerate(li):
				if val == '.':
					if f == 0:
						h = 0
						f = 1
						start = j - 1
						end = j - 1
					end += 1
				else:
					h = 1
					length = end - start
					if length > 0:
						tile1 += length % 2
						tile2 += length // 2
					start = end = j
					f = 0
			if h == 0:
				length = end - start
				if length > 0:
					tile1 += length % 2
					tile2 += length // 2
		print(tile1 * p1 + tile2 * p2)


import math
for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	if 2 * x < y:
		d = 2 * x
	else:
		d = y
	si = x
	pri = 0
	for i in range(n):
		s = input()
		u = 0
		ind = 0
		for j in range(m):
			if s[j] == '.' and u == 0:
				ind = j
				u = 1
			if s[j] == '*' and u == 1:
				dou = (j - ind) // 2
				if (j - ind) % 2 != 0:
					sin = 1
				else:
					sin = 0
				pri += dou * d + sin * si
				u = 0
		if u == 1:
			dou = (m - ind) // 2
			if (m - ind) % 2 != 0:
				sin = 1
			else:
				sin = 0
			pri += dou * d + sin * si
			u = 0
	print(pri)


t = int(input())
ans = []
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	cost = 0
	if y >= 2 * x:
		for j in range(n):
			a = input()
			k = 0
			while k < m:
				if a[k] == '.':
					cost += x
				k += 1
			j += 1
		ans.append(cost)
	else:
		for j in range(n):
			a = input()
			k = 0
			c = 0
			while k < m:
				if a[k] == '.':
					if c != 1:
						cost += x
						c += 1
					elif c == 1:
						cost += y - x
						c = 0
				else:
					c = 0
				k += 1
			j += 1
		ans.append(cost)
for item in ans:
	print(item)


for nt in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append(list(input()))
	(b, w) = (0, 0)
	for i in range(n):
		for j in range(m):
			if grid[i][j] == '*':
				b += 1
			else:
				w += 1
	if 2 * x <= y:
		print(w * x)
	else:
		ans = 0
		covered = [[0 for i in range(m)] for j in range(n)]
		for i in range(n):
			for j in range(m):
				if covered[i][j] == 0:
					if grid[i][j] == '.' and (j == m - 1 or grid[i][j + 1] == '*'):
						ans += x
						covered[i][j] = 1
					elif grid[i][j] == '.':
						ans += y
						covered[i][j] = 1
						covered[i][j + 1] = 1
					else:
						covered[i][j] = 1
		print(ans)


for _ in range(int(input())):
	Input = [int(i) for i in input().split()]
	n = Input[0]
	m = Input[1]
	x = Input[2]
	y = Input[3]
	Square = []
	for N in range(n):
		row = [i for i in input()]
		Square.append(row)
	if 2 * x <= y:
		count = 0
		for i in range(len(Square)):
			count += Square[i].count('.')
		print(str(count * x))
	else:
		count_x = 0
		count_y = 0
		i = 0
		j = 0
		for i in range(len(Square)):
			S = ''.join(Square[i])
			X = S.count('.')
			Y = S.count('..')
			count_x += X - 2 * Y
			count_y += Y
		result = count_x * x + count_y * y
		print(result)


import sys
q = int(input())
for i in range(q):
	(n, m, x, y) = [int(j) for j in sys.stdin.readline().split()]
	a = []
	for j in range(n):
		a.append(sys.stdin.readline())
	mincost = min(x * 2, y)
	totalcost = 0
	j = 0
	k = 0
	while j < n:
		k = 0
		while k < m:
			try:
				if a[j][k] == '.':
					if k + 1 < m and a[j][k + 1] == '.':
						totalcost += mincost
						k += 2
					else:
						totalcost += x
						k += 1
				else:
					k += 1
			except IndexError:
				k += 1
				pass
		j += 1
	print(totalcost)


from pprint import pprint
import sys
q = int(input())
for _ in range(q):
	(n, m, x, y) = map(int, input().split())
	maze = []
	for i in range(n):
		s = input()
		maze.append(list(s))
	if x * 2 <= y:
		cnt = 0
		for i in range(n):
			cnt += maze[i].count('.')
		print(cnt * x)
	else:
		cost = 0
		for i in range(n):
			j = 0
			while j < m:
				if maze[i][j] == '*':
					j += 1
					continue
				nj = j + 1
				if nj >= m:
					cost += x
					j += 1
					continue
				if maze[i][nj] == '.':
					cost += y
					j += 2
					continue
				cost += x
				j += 1
		print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for _ in range(n):
		a.append(input())
	c = 0
	for i in range(n):
		k = a[i].count('..')
		c += min(2 * k * x, k * y)
		while '..' in a[i]:
			a[i] = a[i].replace('..', '')
		c += a[i].count('.') * x
	print(c)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = min(y, 2 * x)
	a = []
	for i in range(n):
		line = list(input())
		a.append(list(map(lambda x: x == '.', line)))
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j]:
				if j < m - 1 and a[i][j + 1]:
					ans += s
					j += 1
				else:
					ans += x
			j += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x <= y:
		p = 0
		for j in range(n):
			s = input()
			p += s.count('.')
		print(p * x)
	else:
		p = 0
		for j in range(n):
			s = input()
			while '..' in s:
				s = s.replace('..', '', 1)
				p += y
			p += s.count('.') * x
		print(p)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	count_of_pair = 0
	totalcountofdot = 0
	for i in range(n):
		lst = input()
		count_of_pair += lst.count('..')
		totalcountofdot += lst.count('.')
	if y / 2 > x:
		print(totalcountofdot * x)
	else:
		leftcount = totalcountofdot - count_of_pair * 2
		print(count_of_pair * y + leftcount * x)


z = input
from math import *
for _ in range(int(z())):
	c = 0
	d = 0
	c1 = 0
	d1 = 0
	(n, m, x, y) = map(int, z().split())
	for _ in range(n):
		l = z().split('*')
		for i in l:
			p = len(i)
			d += p // 2 * y
			c += p % 2 * x
			c1 += p * x
	print(min(d + c, c1))


def list_input():
	return list(map(int, input().split()))

def multiple_input():
	return map(int, input().split())
for _ in range(int(input())):
	(n, m, x, y) = multiple_input()
	a = []
	for i in range(n):
		room = list(input().strip())
		a.append(room)
	if 2 * x <= y:
		ans = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					ans += 1
		print(ans * x)
	else:
		ans1 = 0
		ans2 = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] == '.':
					if j < m - 1 and a[i][j + 1] == '.':
						ans2 += 1
						a[i][j + 1] = '*'
					else:
						ans1 += 1
		print(ans1 * x + ans2 * y)


t = int(input())
for _ in range(t):
	(n, m, one, two) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(input().split('*')))
	if one * 2 <= two:
		ans = 0
		for i in range(n):
			for j in a[i]:
				ans += len(j) * one
		print(ans)
	else:
		ans = 0
		for i in range(n):
			for j in a[i]:
				ans += two * (len(j) // 2)
				ans += len(j) % 2 * one
		print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = tuple(map(int, input().split()))
	a = [input() for k in range(n)]
	ans = 0
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j + 1 < m and a[i][j + 1] == '.' and (2 * x > y):
					ans += y
					j += 1
				else:
					ans += x
			j += 1
	print(ans)


t = int(input())
p = 0
while p < t:
	(n, m, x, y) = [int(j) for j in input().split()]
	cost = 0
	count = 0
	for k in range(n):
		A = list(input())
		count += A.count('.')
		for i in range(m - 1):
			if A[i] != 0 and A[i] != '*':
				if A[i] == A[i + 1] == '.':
					A[i] = 0
					A[i + 1] = 0
					cost += y
				else:
					A[i] = 0
					cost += x
		if A[m - 1] == '.':
			cost += x
	print(min(cost, count * x))
	p += 1


for __ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = [['0' for i in range(m)] for j in range(n)]
	for i in range(n):
		s = input()
		for j in range(m):
			l[i][j] = s[j]
	s1 = 0
	s2 = 0
	for i in range(n):
		c = 0
		for j in range(m):
			if l[i][j] == '.':
				s1 += x
				c += 1
		j = 0
		d = 0
		while j < m - 1:
			if l[i][j] == '.' and l[i][j + 1] == '.':
				s2 += y
				j += 2
				d += 1
			else:
				j += 1
		z = abs(c - 2 * d)
		s2 = s2 + x * z
	print(min(s1, s2))


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for j in range(n):
		a = input()
		if len(a) == 1:
			if a[0] == '.':
				ans += x
		else:
			k = 0
			while k < len(a):
				if k == len(a) - 1:
					if a[k] == '.':
						ans += x
					k += 1
				elif a[k] == '.':
					if a[k + 1] == '.':
						k += 2
						if 2 * x > y:
							ans += y
						else:
							ans += 2 * x
					else:
						k += 1
						ans += x
				else:
					k += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	count = 0
	if 2 * x < y:
		l = True
	else:
		l = False
	for i in range(n):
		a = input()
		p = 0
		while p < m:
			if a[p] == '.':
				if p + 1 != m:
					if a[p] == a[p + 1]:
						if l:
							count += 2 * x
						else:
							count += y
						p += 2
					else:
						count += x
						p += 1
				else:
					count += x
					p += 1
			else:
				p += 1
	print(count)


for _1 in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for _2 in range(n):
		s = input()
		a = s.count('..')
		b = s.count('.')
		if y < 2 * x:
			ans += a * y + (b - 2 * a) * x
		else:
			ans += b * x
	print(ans)


import sys
from collections import defaultdict
from copy import copy
R = lambda t=int: t(input())
RL = lambda t=int: [t(x) for x in input().split()]
RLL = lambda n, t=int: [RL(t) for _ in range(n)]

def solve():
	(n, m, x, y) = RL()
	G = [list(R(str)) for _ in range(n)]
	if y >= 2 * x:
		print(sum((x for l in G for c in l if c == '.')))
		return
	r = i = 0
	for l in G:
		j = 0
		for c in l:
			if c == '.':
				if j < m - 1 and l[j + 1] == '.':
					l[j + 1] = '*'
					r += y
				else:
					r += x
			j += 1
		i += 1
	print(r)
T = R()
for t in range(1, T + 1):
	solve()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	lis = []
	for i in range(n):
		lis.append(list(input()))
	if m == 1:
		c = 0
		for i in lis:
			if i == ['.']:
				c += x
		print(c)
	elif x * 2 < y:
		c = 0
		for i in range(n):
			for j in range(m):
				if lis[i][j] == '.':
					c += x
		print(c)
	else:
		c = 0
		i = 0
		j = 0
		while i < n:
			j = 0
			while j < m:
				if lis[i][j] == '.':
					if j == m - 1:
						c += x
					elif lis[i][j + 1] == '.':
						c += y
						j += 1
					else:
						c += x
				j += 1
			i += 1
		print(c)


import math
t = int(input())
while t:
	t += -1
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		s = list(input())
		l.append(s)
	two = 0
	one = 0
	for i in range(n):
		j = 0
		ch = 0
		while j < m - 1:
			if l[i][j] == '.' and l[i][j + 1] == '.':
				if j == m - 2:
					ch = 1
				two -= -1
				j += 2
			elif l[i][j] == '.':
				one -= -1
				j -= -1
			else:
				j -= -1
		if ch == 0 and l[i][m - 1] == '.':
			one -= -1
	print(min((one + two * 2) * x, one * x + two * y))


def F(A, C1, C2, N, M):
	dp = []
	for n in range(N + 1):
		dp.append([0] * (M + 1))
	if A[N - 1][M - 1] == '.':
		dp[N - 1][M - 1] = C1
	for i in range(N):
		for j in range(M - 1, -1, -1):
			if A[i][j] == '.':
				if j + 1 < M and A[i][j + 1] == '.':
					dp[i][j] = min(C2 + dp[i][j + 2], C1 + dp[i][j + 1])
				else:
					dp[i][j] = C1 + dp[i][j + 1]
			elif j + 1 < M:
				dp[i][j] = dp[i][j + 1]
			else:
				dp[i][j] = 0
	cost = 0
	for i in range(len(dp)):
		cost += dp[i][0]
	return cost

def ProcessInputs():
	(N, M, X, Y) = map(int, input().strip().split())
	A = []
	for i in range(0, N):
		A.append(list(input()))
	res = F(A, X, Y, N, M)
	print(res)
T = int(input())
for t in range(T):
	ProcessInputs()


t = int(input())
while t > 0:
	(n, m, x, y) = input().split()
	(n, m, x, y) = (int(n), int(m), int(x), int(y))
	li = [input() for i in range(n)]
	ans = 0
	for i in range(n):
		s = li[i]
		j = 0
		while j < m:
			if j < m - 1 and s[j] == '.' and (s[j + 1] == '.'):
				ans = ans + min(2 * x, y)
				j += 1
			elif s[j] == '.':
				ans = ans + x
			j += 1
	print(ans)
	t -= 1


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 6)
	t = int(input())
	for _ in range(t):
		(n, m, x, y) = [int(x) for x in input().strip().split()]
		ans = 0
		if x <= y / 2:
			for i in range(n):
				ans += sum([x if j == '.' else 0 for j in list(input().strip())])
		else:
			for i in range(n):
				f = False
				for j in list(input().strip()):
					if j == '.':
						if f:
							ans += y
							f = False
						else:
							f = True
					elif f:
						ans += x
						f = False
				else:
					if f:
						ans += x
		print(ans)
main()


for _ in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	f = 0 if y < 2 * x else 1
	c = 0
	for i in range(n):
		s = input()
		if f:
			c += s.count('.') * x
		else:
			t = s.count('..')
			c += t * y + (s.count('.') - 2 * t) * x
	print(c)


testing_mode = True
import sys, traceback
test_data = ''
final_output = ''

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for x in range(multi)]
	else:
		next_line = next(test_data, None).strip() if test_data else sys.stdin.readline()
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = next_line.split()
		elif input_format == 4:
			formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
		elif input_format == 5:
			formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
		else:
			formatted_input = [next_line]
		return formatted_input

def log(*args):
	if testing_mode:
		print(*args)

def out(output_line, output_format=0, newline=True):
	global final_output
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 1:
		formatted_output = '\n'.join(map(str, output_line))
	if testing_mode:
		final_output += formatted_output + '\n' * newline
	else:
		print(formatted_output, end='\n' if newline else '')
test_inputs = ['\n4\n1 1 10 1\n.\n1 2 10 1\n..\n2 1 10 1\n.\n.\n3 3 3 7\n..*\n*..\n.*.\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']
test_outputs = ['\n10\n1\n20\n18\n\n', '\n$$$\n', '\n$$$\n', '\n$$$\n']

def solve():
	for z in range(input(1)[0]):
		(n, m, x, y) = input(1)
		a = [list(x[0]) for x in input(3, n)]
		u = x * 2 > y
		t = 0
		for i in a:
			skip = False
			for (e, j) in enumerate(i):
				if j == '*' or skip:
					skip = False
					continue
				if e < m - 1 and u and (i[e + 1] == '.'):
					t += y
					skip = True
				else:
					t += x
		out(t)
	return

def run_all(overwrite_testing=False):
	global testing_mode, final_output, test_data
	testing_mode = overwrite_testing and testing_mode
	if testing_mode:
		tests_failed = 0
		for (test_number, (test_in, test_out)) in enumerate(zip(test_inputs, test_outputs)):
			final_output = ''
			if test_in.lstrip()[:3] == '$$$':
				continue
			test_data = iter([i for i in test_in.split('\n') if i])
			print(f'Test Case #{test_number + 1}:')
			print('<' * 20)
			fail = False
			verdict = '\x1b[92m\x1b[1mPASS\x1b[0m'
			try:
				solve()
				final = final_output.rstrip('\n')
				raw_out = iter(map(str, final.split('\n'))) if final else iter(())
			except Exception:
				raw_out = ''
				fail = True
				print('\x1b[95m')
				traceback.print_exc()
				print('\x1b[0m')
			if raw_out:
				if test_out.lstrip()[:3] == '$$$':
					verdict = '\x1b[94m\x1b[1mSKIP\x1b[0m'
					print('\x1b[94m' + '\n'.join(list(raw_out)) + '\x1b[0m')
				else:
					for line_out in [i for i in test_out.split('\n') if i]:
						line_raw = next(raw_out, '% None %')
						if line_raw == line_out.strip():
							print(f'\x1b[92m{line_raw}\x1b[0m')
						else:
							print(f'\x1b[91m{line_raw}\x1b[0m')
							fail = True
					line_raw = next(raw_out, None)
					while line_raw:
						print(f'\x1b[91m{line_raw}\x1b[0m')
						line_raw = next(raw_out, None)
						fail = True
			print('>' * 20)
			if fail:
				verdict = '\x1b[91m\x1b[1mFAIL\x1b[0m'
				tests_failed += 1
			print(verdict + '\n')
		if tests_failed:
			print(f"\x1b[91m\x1b[1m{tests_failed} TEST{('S' if tests_failed > 1 else '')} FAILED!\x1b[0m")
		else:
			print(f'\x1b[92m\x1b[1mALL TESTS PASSED!\x1b[0m')
	else:
		test_data = False
		solve()
run_all(False)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		s = str(input())
		lt = list(s)
		l.append(lt)
	if 2 * x <= y:
		ans = 0
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		c = 0
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					c += 1
				else:
					if c == 1:
						ans += x
					elif c == 2:
						ans += y
					elif c > 2:
						if c % 2 == 0:
							ans += c // 2 * y
						else:
							ans += c // 2 * y
							ans += x
					c = 0
			if c == 1:
				ans += x
			elif c == 2:
				ans += y
			elif c > 2:
				if c % 2 == 0:
					ans += c // 2 * y
				else:
					ans += c // 2 * y
					ans += x
			c = 0
		print(ans)


def price_easy(ss, x):
	ans = 0
	for ch in ss:
		if ch == '.':
			ans += x
	return ans

def price_full(ss, x, y):
	ans = 0
	flag = False
	length = len(ss)
	i = 0
	while i < length:
		ch = ss[i]
		if ch == '.' and flag:
			ans += y
			flag = False
		elif ch == '.' and i == length - 1:
			ans += x
			flag = False
		elif ch == '.':
			flag = True
		elif ch == '*' and flag:
			ans += x
			flag = False
		i += 1
	return ans
t = int(input())
for ___ in range(t):
	(n, m, x, y) = (int(k) for k in input().split(' '))
	price = 0
	if y >= 2 * x:
		for j in range(n):
			inp_str = input()
			price += price_easy(inp_str, x)
	else:
		for j in range(n):
			inp_str = input()
			price += price_full(inp_str, x, y)
	print(price)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = [list(str(input())) for _ in range(n)]
	price = 0
	for i in range(n):
		for j in range(m):
			if l[i][j] == '.':
				if j <= m - 2 and l[i][j + 1] == '.':
					price += min(2 * x, y)
					l[i][j + 1] = '*'
				else:
					price += x
	print(price)


def stl(s):
	return [i for i in s]
tc = int(input())
for _ in range(tc):
	(n, m, x, y) = map(int, input().split())
	l = [stl(input()) for _ in range(n)]
	ans = 0
	if x * 2 <= y or m == 1:
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.':
					ans += x
	else:
		for i in range(n):
			for j in range(m):
				if l[i][j] == '.' and j == m - 1:
					ans += x
					l[i][j] = '*'
				if l[i][j] == '.':
					if l[i][j + 1] == '.':
						ans += y
						l[i][j] = '*'
						l[i][j + 1] = '*'
					else:
						ans += x
						l[i][j] = '*'
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		lst = input()
		lst += '*'
		l.append(lst)
	cost = 0
	for i in range(n):
		k = 0
		s = -1
		while k <= m:
			while l[i][k] == '.':
				k += 1
			z = k - s - 1
			if z > 0 and 2 * x > y:
				cost += z // 2 * y
				cost += z % 2 * x
			elif z > 0 and 2 * x <= y:
				cost += z * x
			s = k
			k += 1
	print(cost)


for p in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	(a, b) = (0, 0)
	for i in range(n):
		s = str(input())
		c = s.count('..')
		d = s.count('.')
		a += x * d
		b += y * c + x * (d - 2 * c)
	print(min(a, b))


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	y = min(y, 2 * x)
	cost = 0
	for _ in range(n):
		S = input()
		i = 0
		while i < m:
			if S[i] == '*':
				i += 1
				continue
			if i != m - 1 and S[i + 1] == '.':
				cost += y
				i += 2
			else:
				cost += x
				i += 1
	print(cost)


t = int(input())
for i in range(t):
	nmxy = list(map(int, input().split()))
	n = nmxy[0]
	m = nmxy[1]
	x = nmxy[2]
	y = nmxy[3]
	arr = []
	for i in range(n):
		arr.append(list(input()))
	if 2 * x <= y:
		res = 0
		for i in range(len(arr)):
			for j in range(len(arr[i])):
				if arr[i][j] == '.':
					res += 1
		print(res * x)
	elif m == 1 and n == 1 and (arr[0][0] == '.'):
		print(x)
	elif m == 1 and n == 1 and (arr[0][0] == '*'):
		print(0)
	else:
		res = 0
		for i in range(len(arr)):
			j = 0
			while j < len(arr[i]):
				if j + 1 < len(arr[i]) and arr[i][j] == '.' and (arr[i][j + 1] == '.'):
					res += y
					j += 1
				elif arr[i][j] == '.':
					res += x
				j += 1
		print(res)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def solve(var, arr):
	(n, m, x, y) = var
	total_count = 0
	for row in arr:
		total_count += row.count('.')
	if 2 * x <= y:
		print(total_count * x)
	else:
		doubles = 0
		for row in arr:
			i = 0
			while i < len(row) - 1:
				if row[i] == '.' and row[i + 1] == '.':
					doubles += 1
					i += 2
				else:
					i += 1
		ans = doubles * y + (total_count - 2 * doubles) * x
		print(ans)
T = inp()
arrs = []
vars = []
for i in range(T):
	vars.append(inlt())
	arrs.append([])
	for _ in range(vars[i][0]):
		arrs[i].append(insr())
for i in range(T):
	solve(vars[i], arrs[i])


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	ll = []
	for i in range(n):
		ll.append(input())
	price = 0
	j = 0
	for i in range(n):
		while j < m:
			if ll[i][j] == '*':
				j += 1
			elif ll[i][j] == '.':
				if j != m - 1 and ll[i][j + 1] == '.':
					if y < 2 * x:
						price += y
						j += 2
					else:
						price += x
						j += 1
				elif j == m - 1 or ll[i][j + 1] == '*':
					price += x
					j += 1
		j = 0
	print(price)


def calibur(s, i):
	return s[:i] + 'f' + s[i + 1:]

def apni_bhabhi(x, y, n, m, s):
	if y >= 2 * x:
		return s.count('.') * x
	if len(s) == 1:
		return s.count('.') * x
	for i in range(n * m - 1):
		if s[i] == '.':
			if s[i + 1] == s[i] and (i + 1) % m != 0:
				s = calibur(s, i)
				s = calibur(s, i + 1)
	return s.count('f') // 2 * y + s.count('.') * x
for _ in range(int(input())):
	s = ''
	(n, m, x, y) = map(int, input().split())
	for xx in range(n):
		s += input()
	print(apni_bhabhi(x, y, n, m, s))


def just_count(arr):
	count = 0
	for row in arr:
		for elem in row:
			if elem == '.':
				count += 1
	return count
t = int(input())
for test in range(t):
	(n, m, x, y) = input().split()
	(n, m, x, y) = (int(n), int(m), int(x), int(y))
	arr = []
	for i in range(n):
		arr.append(input())
	if y >= 2 * x:
		count = just_count(arr)
		print(count * x)
		continue
	count = 0
	for i in range(n):
		for j in range(m - 1):
			if arr[i][j] == '.':
				if arr[i][j + 1] == '.':
					count += y
					arr[i] = arr[i][:j] + '**' + arr[i][j + 2:]
				else:
					count += x
					arr[i] = arr[i][:j] + '*' + arr[i][j + 1:]
		if arr[i][-1] == '.':
			count += x
	print(count)


def case():
	mat = []
	(n, m, x, y) = map(int, input().split())
	for i in range(n):
		s = input()
		s = list(s)
		mat.append(s)
	total = 0
	for i in range(n):
		for j in range(m):
			if j == m - 1:
				if mat[i][j] == '.':
					total += x
			elif mat[i][j] == '.':
				if y < x * 2:
					if mat[i][j + 1] == '.':
						total += y
						mat[i][j + 1] = '*'
					else:
						total += x
				else:
					total += x
	print(total)

def main():
	t = int(input())
	while t > 0:
		case()
		t -= 1
main()


import sys
import math
try:
	sys.stdin = open('Input.txt', 'r')
	sys.stdout = open('Output.txt', 'w')
except:
	pass
for testCases in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append(list(input()))
	cost = 0
	for i in range(n):
		for j in range(m):
			if grid[i][j] == '.':
				if j != 0 and grid[i][j - 1] == '.':
					cost = min(cost + x, cost - x + y)
					grid[i][j] = '*'
					grid[i][j - 1] = '*'
				else:
					cost += x
	print(cost)


import sys
import math
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin
t = int(input())
for ti in range(1, t + 1):
	(n, m, x, y) = list(map(int, input().split()))
	count = []
	for _ in range(n):
		s = input()
		s = s.split('*')
		for sx in s:
			if sx:
				count.append(len(sx))
	res = 0
	if x * 2 <= y:
		for sx in count:
			res += x * sx
	else:
		for sx in count:
			res += sx // 2 * y + sx % 2 * x
	print(res)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	s = 0
	c = 0
	for _ in range(n):
		l = input()
		c1 = l.count('.')
		c2 = l.count('..')
		s += c2 * y + (c1 - 2 * c2) * x
		c += c1
	print(min(s, c * x))


import io
import os
from collections import Counter, defaultdict, deque

def solve(R, C, X, Y, grid):

	def makeRepeat(s):
		repeat = [[s[0], 0]]
		for ch in s:
			if ch == repeat[-1][0]:
				repeat[-1][1] += 1
			else:
				repeat.append([ch, 1])
		return repeat
	cost = 0
	for row in grid:
		repeats = makeRepeat(row)
		for (kind, count) in repeats:
			if kind == '.':
				if 2 * X > Y:
					(q, r) = divmod(count, 2)
					cost += q * Y + r * X
				else:
					cost += count * X
	return cost
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N, M, X, Y) = [int(x) for x in input().split()]
	grid = [input().decode().rstrip() for i in range(N)]
	ans = solve(N, M, X, Y, grid)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	grid = []
	for i in range(n):
		grid.append(list(input()))
	c = 0
	for i in range(n):
		for j in range(m):
			if grid[i][j] == '.':
				if j < m - 1:
					if grid[i][j + 1] == '.':
						if x * 2 >= y:
							grid[i][j] = '*'
							grid[i][j + 1] = '*'
							c += y
							continue
				grid[i][j] = '*'
				c += x
	print(c)


import string

def main():
	t = int(input())
	for _ in range(t):
		rcxy = input().split()
		r = int(rcxy[0])
		c = int(rcxy[1])
		x = int(rcxy[2])
		y = int(rcxy[3])
		matrix = []
		for q in range(r):
			w = [ch for ch in input()]
			matrix.append(w)
		count = 0
		if x * 2 < y:
			for i in range(r):
				for j in range(c):
					if matrix[i][j] == '.':
						count += x
			print(count)
		else:
			for i in range(r):
				for j in range(c):
					if j < c - 1 and matrix[i][j] == '.' and (matrix[i][j + 1] == '.'):
						matrix[i][j] = '*'
						matrix[i][j + 1] = '*'
						count += y
					elif matrix[i][j] == '.':
						matrix[i][j] = '*'
						count += x
			print(count)
main()


from itertools import groupby
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	d = min(d, 2 * c)
	brd = [input() for _ in range(a)]
	res = 0
	for row in brd:
		g = groupby(row)
		for (k, v) in g:
			if k == '*':
				continue
			vv = len(list(v))
			res += vv // 2 * d
			res += vv % 2 * c
	print(res)


test = int(input())
for i in range(test):
	(n, m, x, y) = [int(i) for i in input().split()]
	arr = [0] * n
	for i in range(n):
		arr[i] = [i for i in input()]
	dp = [[0 for j in range(m)] for k in range(n)]
	price = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.' and (not dp[i][j]):
				if j + 1 < m and arr[i][j + 1] == '.':
					if 2 * x <= y:
						dp[i][j] = 1
						dp[i][j + 1] = 1
						price += 2 * x
					else:
						dp[i][j] = 1
						dp[i][j + 1] = 1
						price += y
				else:
					dp[i][j] = 1
					price += x
	print(price)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	if y > 2 * x:
		ans = 0
		for p in l:
			ans += x * p.count('.')
		print(ans)
	else:
		ans = 0
		for p in l:
			s = p
			if m > 1:
				i = 0
				while i < m - 1:
					if s[i] == '.':
						if s[i] == s[i + 1]:
							ans += y
							i += 2
						else:
							ans += x
							i += 1
					else:
						i += 1
				if i == m - 1:
					if s[i] == '.':
						ans += x
			elif s[0] == '.':
				ans += x
		print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for _ in range(n):
		arr.append([ch for ch in input()])
	cost = 0
	conswhites = []
	for i in range(n):
		c = 0
		for j in range(m):
			if arr[i][j] == '.' and j < m - 1:
				c += 1
			else:
				if j == m - 1 and arr[i][-1] == '.':
					c += 1
				cost += c % 2 * x + min((c - c % 2) * x, (c - c % 2) // 2 * y)
				c = 0
	print(cost)


T = int(input())

def compute_price(num, x, y):
	if num % 2 == 0:
		return num // 2 * y
	return num // 2 * y + x
for t in range(T):
	(n, m, x, y) = [int(i) for i in input().split()]
	ans = 0
	if 2 * x <= y:
		for i in range(n):
			s = input()
			for i in s:
				if i == '.':
					ans += x
		print(ans)
	else:
		ans = 0
		for i in range(n):
			s = input()
			black_points = []
			for j in range(m):
				if s[j] == '*':
					black_points.append(j)
			black_points.append(m)
			ans += compute_price(black_points[0], x, y)
			for i in range(1, len(black_points)):
				ans += compute_price(black_points[i] - black_points[i - 1] - 1, x, y)
		print(ans)


import math
t = int(input())
for g in range(t):
	(n, m, x, y) = map(int, input().split())
	S = []
	A1 = 0
	for i in range(n):
		s = input()
		s = list(s)
		S.append(s)
		A1 += s.count('.') * x
	A2 = 0
	for i in range(0, n):
		for j in range(0, m - 1):
			if S[i][j] == '.' and S[i][j + 1] == '.':
				S[i][j] = '*'
				S[i][j + 1] = '*'
				A2 += y
	for i in range(n):
		for j in range(m):
			if S[i][j] == '.':
				A2 += x
	print(min(A1, A2))


ppp = int(input())
for ___ in range(ppp):
	(row, col, x, y) = map(int, input().split())
	area = []
	white = 0
	for i in range(row):
		rows = input()
		area.append(rows)
		white += rows.count('.')
	final = 0
	if 2 * x > y:
		for string in area:
			final += string.count('..') * y
			white -= string.count('..') * 2
		final += white * x
	else:
		final += white * x
	print(final)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	t1 = t2 = 0
	for i in range(n):
		s = list(input())
		l = 0
		for z in range(m):
			if s[z] == '.':
				l += 1
			else:
				t1 += l % 2
				t2 += l // 2
				l = 0
			if z == m - 1:
				t1 += l % 2
				t2 += l // 2
				l = 0
	if y > 2 * x:
		print((t1 + 2 * t2) * x)
	else:
		print(t1 * x + t2 * y)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		row = input()
		single = row.count('.')
		double = row.count('..')
		ans += single * x
		if y < 2 * x:
			ans += double * (y - 2 * x)
	print(ans)


T = int(input())

def main():
	(n, m, x, y) = tuple(map(int, input().split()))
	matrix = []
	for i in range(n):
		arr = list(input())
		matrix.append(arr)
	if y >= 2 * x:
		single_count = 0
		for i in range(n):
			single_count = single_count + matrix[i].count('.')
		return single_count * x
	else:
		total_count = 0
		y_count = 0
		for i in range(n):
			dot_count = matrix[i].count('.')
			total_count = total_count + dot_count
			if dot_count >= 2:
				pointer = 0
				while pointer < m - 1:
					if matrix[i][pointer] == '.':
						if matrix[i][pointer + 1] == '.':
							y_count = y_count + 1
							pointer = pointer + 2
						else:
							pointer = pointer + 2
					else:
						pointer = pointer + 1
			else:
				continue
		x_count = total_count - 2 * y_count
		cost = y_count * y + x_count * x
		return cost
for i in range(T):
	print(main())


tc = int(input())
for i in range(tc):
	(n, m, x, y) = [int(x) for x in input().split()]
	cost = 0
	square = []
	white = 0
	for j in range(n):
		row = []
		r = input()
		for symbol in r:
			if symbol == '.':
				white += 1
			row.append(symbol)
		square.append(row)
	for k in range(n):
		if m == 1:
			cost = n * x
		else:
			l = 0
			while l < m:
				if square[k][l] == '.':
					if l == m - 1:
						cost += x
					elif square[k][l] + square[k][l + 1] == '..':
						cost += y
						l += 1
					else:
						cost += x
				l += 1
	cost = min(cost, x * white)
	print(cost)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	theater = []
	count = 0
	flag = 0
	if 2 * x > y:
		flag = 1
	for _ in range(n):
		line = input()
		if flag == 1:
			seq = 0
			for square in line:
				if seq < 2:
					if square == '.':
						seq += 1
						if seq == 2:
							count += y
							seq = 0
					elif square == '*' and seq == 1:
						count += x
						seq = 0
			if seq == 1:
				count += x
		else:
			count += x * line.count('.')
	print(count)


import sys
import math
import operator as op
from functools import reduce

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer / denom

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def gcdExtended(a, b):
	if a == 0:
		return (b, 0, 1)
	(gcd, x1, y1) = gcdExtended(b % a, a)
	x = y1 - b // a * x1
	y = x1
	return (gcd, x, y)
t = read_int()
for i in range(t):
	(n, m, x, y) = read_int_line()
	mat = []
	for i in range(n):
		r = read_line()
		mat.append(r)
	ans = 0
	if m == 1:
		for i in range(n):
			if mat[i][0] == '.':
				ans += x
	else:
		i = 0
		j = 0
		while i < n:
			j = 0
			while j < m:
				if j == m - 1:
					if mat[i][j] == '.':
						ans += x
						break
				if mat[i][j] == '.' and mat[i][j + 1] == '.':
					j += 2
					if 2 * x > y:
						ans += y
					else:
						ans += 2 * x
				elif mat[i][j] == '.' and mat[i][j + 1] != '.':
					j += 2
					ans += x
				else:
					j += 1
			i += 1
	print(ans)


import math

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
t = inp()
while t:
	(m, n, x, y) = invr()
	a = []
	cost = 0
	for _ in range(m):
		temp = instr()
		if n == 1 and temp[0] == '.':
			cost += x
		else:
			i = 0
			while i < n:
				if temp[i] == '.':
					if i < n - 1 and temp[i + 1] == '.':
						cost += min(2 * x, y)
						i += 1
					else:
						cost += x
				i += 1
	print(cost)
	t -= 1


def answer():
	(n, m, x, y) = list(map(int, input().split()))
	a = []
	for _ in range(n):
		a.append(input())
	if x <= y / 2:
		cnt = 0
		for i in range(n):
			cnt += a[i].count('.')
		return x * cnt
	else:
		summ = 0
		for i in range(n):
			cnt = 0
			cntd = 0
			cnt += a[i].count('.')
			cntd += a[i].count('..')
			summ += cntd * y + (cnt - cntd * 2) * x
		return summ
	return
for _ in range(int(input())):
	print(answer())


t = int(input())
for v in range(t):
	(n, m, x, y) = map(int, input().split(' '))
	mat = []
	for i in range(n):
		li = list(input())
		mat.append(li)
	t_c = 0
	i = 0
	while i < n:
		j = 0
		while j < m:
			if mat[i][j] == '.':
				if j + 1 < m and mat[i][j + 1] == '.':
					if 2 * x <= y:
						t_c += 2 * x
						j += 2
					else:
						t_c += y
						j += 2
				elif j + 1 < m and mat[i][j + 1] == '*':
					t_c += x
					j += 1
				else:
					t_c += x
					j += 1
			else:
				j += 1
		i += 1
	print(t_c)


t = int(input())
while t > 0:
	(n, m, x, y) = list(map(int, input().strip(' ').split()))
	cost = 0
	for i in range(n):
		c1 = 0
		l1 = input()
		l2 = l1
		p = l1.count('..')
		c1 = c1 + p * y
		l1 = l1.replace('..', '', p)
		p = l1.count('.')
		c1 = c1 + p * x
		l1 = l1.replace('.', '', p)
		c2 = 0
		p = l2.count('.')
		c2 = c2 + p * x
		l1 = l1.replace('.', '', p)
		cost = cost + min(c1, c2)
	print(cost)
	t = t - 1


def func(elem, m, z, x):
	i = 0
	prev = elem[0]
	counter = 0
	ans = 0
	while i < m:
		if prev == '.' and elem[i] == '.':
			counter += 1
		elif prev == '*' and elem[i] == '.':
			prev = '.'
			counter = 1
		elif prev == '.' and elem[i] == '*':
			if counter % 2 == 0:
				ans += z * (counter // 2)
			else:
				ans += z * (counter // 2) + x
			counter = 0
			prev = '*'
		i += 1
	if counter > 0:
		if counter % 2 == 0:
			ans += z * (counter // 2)
		else:
			ans += z * (counter // 2) + x
	return ans
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	z = min(2 * x, y)
	a = []
	for i in range(n):
		s = list(input())
		a.append(s)
	ans = 0
	for elem in a:
		ans += func(elem, m, z, x)
	print(ans)


for _ in ' ' * int(input()):
	(a, b, c, d) = map(int, input().split())
	s = 0
	for i in range(a):
		k = input().split('*')
		for j in k:
			if j == '':
				continue
			else:
				p = len(j)
				if p % 2 == 0:
					s += min(d * (p // 2), p * c)
				else:
					s += min(d * (p // 2) + c, p * c)
	print(s)


def main():
	t = int(input())
	for _ in range(t):
		(n, m, p1, p2) = input().split()
		(n, m, p1, p2) = (int(n), int(m), int(p1), int(p2))
		arr = [[0 for _ in range(m)] for _ in range(n)]
		for i in range(n):
			t = input()
			for j in range(m):
				arr[i][j] = t[j]
		use_p1 = False
		if p1 * 2 <= p2:
			use_p1 = True
		cost = 0
		row_eval = [False for _ in range(n)]
		for i in range(n):
			j = 0
			while j < m - 1:
				if arr[i][j] == '.' and arr[i][j] == arr[i][j + 1]:
					if j + 1 == m - 1:
						row_eval[i] = True
					j += 2
					if use_p1:
						cost += p1 * 2
					else:
						cost += p2
				elif arr[i][j] == '.' and arr[i][j] != arr[i][j + 1]:
					cost += p1
					j += 2
				elif arr[i][j] == '*':
					j += 1
		for i in range(n):
			if not row_eval[i] and arr[i][m - 1] == '.':
				cost += p1
		print(cost)
main()


from collections import *
from bisect import *
from math import *
from heapq import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		s = input().rstrip('\n')
		a.append(s)
	co = 0
	for i in range(n):
		no = 0
		for j in range(m):
			if no == 0 and a[i][j] == '*':
				continue
			if no and a[i][j] == '*':
				re = no // 2
				re1 = no % 2
				co += min(re1 * x + re * y, no * x)
				no = 0
			else:
				no += 1
		if no:
			re = no // 2
			re1 = no % 2
			co += min(re1 * x + re * y, no * x)
	print(co)


for t in range(int(input())):
	(m, n, x, y) = [int(i) for i in input().split()]
	grid = [input() for i in range(m)]
	y = min(y, 2 * x)
	ans = 0
	for r in grid:
		i = 0
		while i < n:
			if r[i] == '*':
				i += 1
				continue
			j = i
			while j + 1 < n and r[j + 1] == '.':
				j += 1
			ans += (j - i + 1) // 2 * y + (j - i + 1) % 2 * x
			i = j + 1
	print(ans)


from sys import stdin
for _ in range(int(input())):
	(n, m, x, y) = map(int, stdin.readline().split())
	li = [list(input().rstrip()) for _ in range(n)]
	if 2 * x <= y:
		num = 0
		for i in range(n):
			for j in range(m):
				if li[i][j] == '.':
					num += 1
		res = num * x
	else:
		res = 0
		for i in range(n):
			for j in range(m):
				if li[i][j] == '.':
					if j != m - 1 and li[i][j + 1] == '.':
						res += y
						li[i][j + 1] = '*'
					else:
						res += x
	print(res)


q = int(input())
for w in range(q):
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for i in range(n):
		s = input()
		fl = 0
		j = 0
		while j < m:
			if x * 2 > y and j > 0 and (s[j] == '.') and (fl == 1) and (s[j] == s[j - 1]):
				fl = False
				ans -= x
				ans += y
			elif s[j] == '.':
				ans += x
				fl = 1
			j += 1
	print(ans)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	xx = 0
	yy = 0
	for i in range(n):
		a.append(list(map(str, input())))
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j + 1 < m:
					if a[i][j + 1] == '.':
						yy += 1
						j += 1
					else:
						xx += 1
				else:
					xx += 1
			j += 1
		i += 1
	ans1 = xx * x + yy * y
	ans2 = xx * x + yy * 2 * x
	print(min(ans1, ans2))


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7

def solve():
	(n, m, x, y) = list(map(int, stdin.readline().split()))
	ans = 0
	a = [''] * n
	for i in range(n):
		a[i] = stdin.readline().strip('\n')
	for i in range(n):
		j = 0
		while j < m:
			if a[i][j] == '.':
				if j <= m - 2:
					if a[i][j + 1] == '.' and y < 2 * x:
						ans += y
						j += 2
					else:
						ans += x
						j += 1
				else:
					ans += x
					j += 1
			else:
				j += 1
	print(ans)
t = 1
t = int(stdin.readline())
for _ in range(t):
	solve()


from sys import stdin
input = stdin.readline
from collections import deque
for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	price = min(y, 2 * x)
	count = 0
	cost = 0
	for _ in range(n):
		s = deque(input().rstrip())
		s.append('*')
		for i in s:
			if i == '.':
				count += 1
				if not count & 1:
					cost += count // 2 * price
					count = 0
			else:
				if not count & 1:
					cost += count // 2 * price
				else:
					cost += x
				count = 0
	print(cost)


def solve(n, m, x, y, board):
	total = 0
	for row in board:
		index = 0
		two = 0
		one = 0
		while index < m:
			if row[index] == '.' and index < m - 1 and (row[index + 1] == '.'):
				two += 1
				index += 2
			elif row[index] == '.':
				one += 1
				index += 1
			else:
				index += 1
		use_x = (one + 2 * two) * x
		use_y = two * y + one * x
		total += min(use_x, use_y)
	return total

def main():
	t = int(input())
	for i in range(t):
		d = input()
		d = [int(i) for i in d.split()]
		n = d[0]
		a = d[1]
		b = d[2]
		c = d[3]
		board = []
		for i in range(n):
			row = input()
			board.append(row)
		ans = solve(n, a, b, c, board)
		print(ans)
main()


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(list(input()))
	pairs = 0
	cost = 0
	for i in range(n):
		for j in range(m):
			if arr[i][j] == '.':
				if j + 1 < m:
					if arr[i][j + 1] == '.':
						(arr[i][j], arr[i][j + 1]) = ('*', '*')
						pairs += 1
					else:
						cost += x
				else:
					cost += x
	print(min(cost + pairs * 2 * x, cost + pairs * y))


for _ in range(int(input())):
	x = input()
	(n, m, x, y) = list(map(int, x.split()))
	cost = 0
	for i in range(n):
		row = input()
		if 2 * x >= y:
			j = 0
			while j < m:
				if row[j] == '.':
					try:
						if row[j + 1] == '.':
							cost += y
							j += 2
						else:
							cost += x
							j += 2
					except:
						cost += x
						j += 1
				else:
					j += 1
		else:
			for k in range(m):
				if row[k] == '.':
					cost += x
	print(cost)


t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	ls = []
	for j in range(n):
		ls.append(input())
		ls[-1] += '*'
	(ones, tws) = ([], [])
	for j in range(n):
		k = 0
		while k < m:
			if ls[j][k] == '.':
				if ls[j][k + 1] == '.':
					tws.append(1)
					k += 1
				else:
					ones.append(1)
			k += 1
	if y < 2 * x:
		cost = len(tws) * y + len(ones) * x
	else:
		cost = (len(tws) * 2 + len(ones)) * x
	print(cost)


N = int(input())
for w in range(N):
	data = list(map(int, input().split()))
	m = data[0]
	n = data[1]
	x = data[2]
	y = data[3]
	mat = []
	for v in range(m):
		minMat = list(input())
		mat.append(minMat)
	T = 0
	i = 0
	while i < m:
		j = 0
		while j < n:
			if mat[i][j] == '.' and j + 1 < n and (mat[i][j + 1] == '.'):
				T += min(2 * x, y)
				j += 1
			elif mat[i][j] == '.':
				T += x
			j += 1
		i += 1
	print(T)


def main():
	t = int(input())
	for i in range(t):
		(n, m, x, y) = map(int, input().split())
		mat = []
		po_jedan = 0
		for i in range(n):
			a = list(input())
			mat.append(a)
			po_jedan += a.count('.')
		mini = po_jedan * x
		mjesovito = po_jedan * x
		smjer1 = [0, 0]
		smjer2 = [-1, 1]
		for r in range(n):
			for s in range(m):
				if mat[r][s] == '.':
					for i in range(2):
						if 0 <= r + smjer1[i] < n and 0 <= s + smjer2[i] < m:
							if mat[r + smjer1[i]][s + smjer2[i]] == '.':
								mat[r][s] = '*'
								mat[r + smjer1[i]][s + smjer2[i]] = '*'
								mjesovito -= x * 2
								mjesovito += y
								break
					mini = min(mini, mjesovito)
		print(mini)
main()


import math
t = int(input())
for i in range(t):
	(n, m, x, y) = map(int, input().split())
	if y >= 2 * x:
		num = 0
		for j in range(n):
			s = input()
			for k in range(m):
				if s[k] == '.':
					num += x
		print(num)
	else:
		num = 0
		for j in range(n):
			s = input()
			s = s + '*'
			k = 0
			while k < m:
				if s[k] == '.':
					if s[k + 1] == '.':
						num += y
					else:
						num += x
					k += 2
				else:
					k += 1
		print(num)


import math
for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	f = 0
	if 2 * a <= b:
		f = 1
	c = 0
	for i in range(n):
		s = input()
		if len(s) > 1:
			k = 0
			for j in range(len(s)):
				if s[j] == '.':
					k += 1
				else:
					if k % 2 == 0:
						if f == 1:
							c += k * a
						else:
							c += k // 2 * b
					elif f == 1:
						c += k * a
					else:
						c += (k - 1) // 2 * b + a
					k = 0
			if k % 2 == 0:
				if f == 1:
					c += k * a
				else:
					c += k // 2 * b
			elif f == 1:
				c += k * a
			else:
				c += (k - 1) // 2 * b + a
		elif s[0] == '.':
			c += a
	print(c)


def solve():
	(n, m, x, y) = map(int, input().split())
	square = [input() for _ in range(n)]
	(total, two) = (0, 0)
	for i in range(n):
		j = 0
		while j < m:
			if square[i][j] == '.':
				total += 1
				if j + 1 < m and square[i][j + 1] == '.':
					total += 1
					two += 1
					j += 1
			j += 1
	one = total - 2 * two
	print(min(one * x + two * y, total * x))
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	tot = 0
	for i in range(n):
		mn = 10 ** 9
		(xc, yc) = (0, 0)
		for j in range(m):
			if a[i][j] == '.':
				xc += x
		(j, f) = (0, 0)
		while j < m - 1:
			if a[i][j] == '.' and a[i][j + 1] == '.':
				if j + 1 == m - 1:
					f = 1
				yc += y
				j += 2
			elif a[i][j] == '.':
				yc += x
				j += 1
			else:
				j += 1
		if f == 0:
			if a[i][m - 1] == '.':
				yc += x
		mn = min(mn, yc, xc)
		tot += mn
	print(tot)


t = int(input())
while t:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	ans2 = 0
	ans1 = 0
	for i in range(0, n):
		s = input()
		if len(s) == 1:
			if s == '.':
				ans1 += 1
		else:
			j = 0
			z = 0
			while j < len(s):
				if s[j] == '.':
					if z == 0:
						z = 1
						if j == len(s) - 1:
							ans1 += 1
					else:
						ans2 += 1
						z = 0
				elif z == 1:
					ans1 += 1
					z = 0
				else:
					z = 0
				j += 1
	if 2 * x <= y:
		print((ans1 + 2 * ans2) * x)
	else:
		print(ans2 * y + ans1 * x)


def use_one_flow(horizontal, price):
	dot_count = 0
	for y in range(horizontal):
		line = input()
		line_list = list(line)
		for dot in line_list:
			if dot == '.':
				dot_count += 1
	print(dot_count * price)

def use_two_flow(horizontal, price_one, price_two):
	one_dots = 0
	two_dots = 0
	for y in range(horizontal):
		line = input()
		line_list = list(line)
		is_previous_dot = False
		for i in range(len(line_list)):
			if line_list[i] == '.':
				if i == 0:
					is_previous_dot = True
					one_dots += 1
				elif is_previous_dot:
					two_dots += 1
					one_dots -= 1
					is_previous_dot = False
				else:
					one_dots += 1
					is_previous_dot = True
			else:
				is_previous_dot = False
	print(one_dots * price_one + two_dots * price_two)
n_tests = int(input())
for i in range(n_tests):
	(hor, ver, one_one, one_two) = [int(i) for i in input().split()]
	use_two = False
	if one_two < one_one * 2:
		use_two = True
	if not use_two:
		use_one_flow(hor, one_one)
	else:
		use_two_flow(hor, one_one, one_two)


test = int(input())
for j in range(test):
	list = input().split()
	sum = 0
	for i in range(int(list[0])):
		string = input()
		if 2 * int(list[2]) < int(list[3]):
			k = string.count('.')
			sum += k * int(list[2])
		else:
			k = string.count('..')
			sum += k * int(list[3])
			k = string.count('.') - k * 2
			sum += k * int(list[2])
	print(sum)


t = int(input())
for _ in range(t):
	(n, m, x, y) = [int(x) for x in input().split()]
	ans = 0
	for _ in range(n):
		line = input()
		count = 0
		for tile in line:
			if tile == '.':
				count += 1
			else:
				ans += min(x * count, count // 2 * y + count % 2 * x)
				count = 0
		ans += min(x * count, count // 2 * y + count % 2 * x)
	print(ans)


t = int(input())
count = 0
while count < t:
	count += 1
	l = list(map(int, input().split(' ')))
	n = l[0]
	m = l[1]
	one = l[2]
	two = l[3]
	board = []
	for i in range(n):
		board.append(input())
	prof = min(2 * one, two)
	ans = 0
	for i in board:
		if m == 1 and i[0] == '.':
			ans += one
		else:
			j = 0
			while j < m:
				if j == m - 1:
					if i[j] == '.':
						ans += one
				elif i[j] == '.':
					if i[j + 1] == '.':
						ans += prof
						j += 1
					else:
						ans += one
				j += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	mat = []
	for i in range(n):
		mat.append(list(input()))
	if float(y) / 2 >= x:
		t = 0
		for j in mat:
			t = t + j.count('.')
		print(t * x)
	else:
		t = 0
		for k in range(n):
			for l in range(m):
				if mat[k][l] == '.':
					if l + 1 < m and mat[k][l + 1] == '.':
						t = t + y
						mat[k][l + 1] = '*'
					else:
						t = t + x
		print(t)


from math import *

def solve():
	(n, m, x, y) = map(int, input().split())
	total = 0
	for i in range(n):
		a = input().split('*')
		y = min(y, x * 2)
		for i in a:
			k = len(i)
			total += floor(k / 2) * y + k % 2 * x
	print(total)
t = int(input())
for x in range(t):
	solve()


for _ in range(int(input())):
	(n, m, x, y) = [int(k) for k in input().split()]
	tiles = []
	amt = 0
	for i in range(n):
		tilesele = input()
		tiles.append(list(tilesele))
	flag = 0
	if 2 * x <= y:
		for ii in range(n):
			amt += tiles[ii].count('.') * x
	else:
		for ii in range(n):
			flag = 0
			for jj in range(m):
				if tiles[ii][jj] == '.' and flag == 0:
					flag = 1
					amt += x
				elif tiles[ii][jj] == '.' and flag == 1:
					flag = 0
					amt += y - x
				else:
					flag = 0
	print(amt)


import sys
[t] = [int(i) for i in sys.stdin.readline().split()]
for _ in range(t):
	[n, m, x, y] = [int(i) for i in sys.stdin.readline().split()]
	cost = 0
	for i in range(n):
		row = sys.stdin.readline().strip()
		index = 0
		while index < m:
			if row[index] == '.':
				if index < m - 1 and row[index + 1] == '.' and (y <= 2 * x):
					cost += y
					index += 2
				else:
					cost += x
					index += 1
			else:
				index += 1
	print(cost)


t = int(input())
for i in range(t):
	sq = []
	[n, m, x, y] = [int(i) for i in input().split()]
	cp1 = x
	cp2 = y / 2
	wh1 = 0
	wh2 = 0
	for i in range(n):
		tmp = input()
		sq.append(tmp)
		wh1 += tmp.count('.')
		wh2 += tmp.count('..')
	if cp1 <= cp2:
		print(wh1 * x)
	else:
		print(wh2 * y + (wh1 - wh2 * 2) * x)


import getopt
import sys
import math
onlyCase = -1
verbose = False
try:
	(opts, args) = getopt.getopt(sys.argv[1:], 'vc:', ['case='])
except getopt.GetoptError as err:
	print(str(err))
	sys.exit(2)
for (o, a) in opts:
	if o in ('-c', '--case'):
		onlyCase = int(a)
	elif o == '-v':
		verbose = True
	else:
		sys.exit()
ncases = int(input())
for c in range(1, ncases + 1):
	(n, m, x, y) = [int(x) for x in input().split()]
	A = []
	for i in range(n):
		A.append(list(input()))
	if onlyCase != -1 and onlyCase != c:
		continue
	price = 0
	y = min(2 * x, y)
	i = 0
	while i < n:
		j = 0
		while j < m:
			if A[i][j] == '*':
				j += 1
				continue
			if j < m - 1 and A[i][j + 1] == '.':
				price += y
				j += 2
			else:
				price += x
				j += 1
		i += 1
	print('%d' % price)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	S = [str(input()) for _ in range(n)]
	ans = 0
	for i in range(n):
		cnt = 0
		for j in range(m):
			if S[i][j] != '.':
				(q, r) = divmod(cnt, 2)
				ans += min(cnt * x, q * y + r * x)
				cnt = 0
			else:
				cnt += 1
		else:
			(q, r) = divmod(cnt, 2)
			ans += min(cnt * x, q * y + r * x)
	print(ans)


def get_answer(s, x, y):
	if y >= 2 * x:
		answer = 0
		for e in s:
			if e == '.':
				answer += 1
		return answer * x
	else:
		answer = 0
		i = 0
		while i <= len(s) - 1:
			if i != len(s) - 1 and s[i] == s[i + 1] and (s[i + 1] == '.'):
				answer += y
				i += 2
			elif s[i] == '*':
				i += 1
			else:
				answer += x
				i += 1
		return answer
num_testcases = int(input())
for i in range(num_testcases):
	(n, m, x, y) = map(int, input().split())
	answer = 0
	for i in range(n):
		s = input()
		answer += get_answer(s, x, y)
	print(answer)


for i in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	z = 0
	for i in range(n):
		s = input()
		k = s.count('.')
		a = s.count('..')
		if a * 2 == k:
			z = min(k * x, a * y) + z
		if a * 2 != k:
			z = min(a * y + abs(k - 2 * a) * x + z, k * x + z)
	print(z)


from math import *
from collections import *
from itertools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(1000000)
d = defaultdict(lambda : 0, {})

def io():
	return map(int, input().split())

def op():
	return list(map(int, input().split()))

def o():
	return int(input())

def l(x):
	return len(x)

def r(x):
	if type(x) == int:
		return range(x)
	else:
		return range(len(x))

def kl(con, x=0):
	if x == 0:
		print('Yes') if con else print('No')
	elif x == 1:
		print('yes') if con else print('no')
	elif x == 2:
		print('YES') if con else print('NO')
MOD = 1000000007
MAX = float('inf')
MIN = -float('inf')
p = input
for _ in range(o()):
	(n, m, x, y) = io()
	ans = 0
	for i in r(n):
		s = p()
		c = 0
		for j in r(s):
			if s[j] == '*':
				ans += c // 2 * min(2 * x, y)
				ans += c % 2 * x
				c = 0
			else:
				c += 1
		ans += c // 2 * min(2 * x, y)
		ans += c % 2 * x
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, x, y) = list(map(int, input().split()))
	res = 0
	for i in range(n):
		item = input()
		p = item.count('..')
		q = item.count('.')
		if 2 * x <= y:
			res += q * x
		else:
			res += p * y + (q - 2 * p) * x
	print(res)


t = int(input())
while t != 0:
	t -= 1
	(n, m, x, y) = map(int, input().split())
	ans = 0
	for _ in range(n):
		s = input().split('*')
		k = []
		for p in s:
			if p == '':
				continue
			else:
				k.append(len(p))
		for p in k:
			if p == 1:
				ans += x
			else:
				c = 100000000
				for r in range(0, p + 1, 2):
					c = min(c, (p - r) * x + r // 2 * y)
				ans += c
	print(ans)


from sys import stdin, exit, setrecursionlimit
from collections import deque
from heapq import _heappop_max, heappush, heappop
from math import gcd
from math import ceil, log2
setrecursionlimit(10000000)
from collections import deque
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	(n, m, x, y) = mi()
	dp = [0 for i in range(m)]
	tot = 0
	for i in range(n):
		cur = si()
		dp = [float('inf') for j in range(m + 1)]
		dp[-1] = 0
		for j in range(m):
			if cur[j] == '*':
				dp[j] = dp[j - 1]
			if cur[j] != '*':
				dp[j] = min(dp[j], dp[j - 1] + x)
			if cur[j] != '*' and cur[j - 1] != '*':
				if j != 0:
					dp[j] = min(dp[j], dp[j - 2] + y)
		tot += dp[m - 1]
	print(tot)


t = int(input())
for i in range(t):
	a = []
	a = list(map(int, input().split()))
	n = a[0]
	m = a[1]
	x = a[2]
	y = a[3]
	ans = 0
	for j in range(n):
		s = input()
		s = s + '*'
		if x * 2 <= y:
			ans += s.count('.') * x
		else:
			j = 0
			while j != len(s) - 1:
				if s[j:j + 2] == '..':
					ans += y
					j += 2
				elif s[j:j + 1] == '.':
					j += 1
					ans += x
				else:
					j += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x, y) = map(int, input().split())
	sq = []
	for i in range(n):
		sq.append(list(input()))
	ones = 0
	twos = 0
	skip = False
	for i in range(n):
		for j in range(m):
			if skip == True:
				skip = False
			elif sq[i][j] == '.':
				if j != m - 1:
					if sq[i][j + 1] == '.':
						twos += 1
						skip = True
					else:
						ones += 1
				else:
					ones += 1
	cost = ones * x
	if 2 * x < y:
		cost += twos * 2 * x
	else:
		cost += twos * y
	print(cost)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	l = []
	r = []
	for i in range(n):
		s = input()
		k = s.count('..')
		a = s.count('.')
		l.append(k)
		r.append(a)
	if y >= 2 * x:
		s = 0
		for i in range(len(r)):
			r[i] = r[i] * x
			s = s + r[i]
		print(s)
	else:
		c = 0
		s = 0
		for i in range(len(l)):
			r[i] = r[i] - 2 * l[i]
			l[i] = l[i] * y
			c = c + l[i]
			s = s + r[i] * x
		print(c + s)


t = int(input())
for _ in range(t):
	(n, m, x, y) = map(int, input().split())
	if 2 * x < y:
		f = 1
	else:
		f = 0
	c = 0
	if f == 0 and m != 1:
		for i in range(n):
			xx = input()
			j = 0
			while j < m - 1:
				if xx[j] == '.' and xx[j + 1] == '.':
					c += y
					j += 2
				elif xx[j] == '.':
					c += x
					j += 1
				else:
					j += 1
			if j == m - 1:
				if xx[-1] == '.':
					c += x
	else:
		for i in range(n):
			xx = input()
			for j in xx:
				if j == '.':
					c += x
	print(c)


t = int(input())
list_ans = []
for i in range(t):
	ans = 0
	(n, m, x, y) = map(int, input().split())
	if y >= 2 * x:
		for s in range(n):
			str1 = input()
			ans += str1.count('.') * x
		list_ans.append(ans)
	else:
		for s in range(n):
			str1 = input()
			ans += str1.count('..') * y + (str1.count('.') - str1.count('..') * 2) * x
		list_ans.append(ans)
for item in list_ans:
	print(item)


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	t = 0
	for i in range(n):
		l = input()
		u = 0
		i = 0
		while i < m:
			if l[i] == '.':
				if i < m - 1 and l[i + 1] == '.':
					u += 1
				else:
					t += a
				i += 2
				continue
			i += 1
		if u * b > 2 * u * a:
			t += 2 * u * a
		else:
			t += u * b
	print(t)