// I AM A MUSLIM
#include "bits/stdc++.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define fast_io std::ios::sync_with_stdio(0);std::cin.tie(0)
#define lli long long int
#define flush fflush(stdout)
#define new_line printf("\n")
#define yn(a, b) printf("%s\n", (a) >= (b) ? "Yes":"No")
#define amodm(a, M) (((a)%M+M)%M)
// #define int lli
using pii = std::pair<int,int>;
const int MOD = 1000000007;
const int mxN = 2005;
int n, m, x, y, stat;
char g[mxN][mxN];
lli dp[mxN][mxN][3];
int vis[mxN][mxN][3];
lli inf = 1e13;
bool OK(int r, int c) {
if (std::min(r,c) < 0 || (r == n) || (c == m)) return 0;
if (g[r][c] == '#') return 0;
return 1;
}
lli fn(int r, int c, int mv) {
if ((r == n-1) && (c == m-1)) return 0;
if ((r >= n) || (c >= m)) return inf;
if (r < 0 || c < 0) return inf;
if (g[r][c] == '#') return inf;
if (vis[r][c][mv] == stat) return dp[r][c][mv];
lli ans = inf;
if ((r == 0) && (c == 0)) {
if (OK(r+1, c)) {
ans = std::min(ans, fn(r+2, c, 0) + x);
ans = std::min(ans, fn(r+1, c+1, 2) + y);
}
if (OK(r, c+1)) {
ans = std::min(ans, fn(r, c+2, 2) + x);
ans = std::min(ans, fn(r+1, c+1, 0) + y);
}
} else {
if (mv == 0) {
ans = std::min(ans, fn(r+1, c, 0) + x);
ans = std::min(ans, fn(r, c+1, 2) + y);
ans = std::min(ans, fn(r, c-1, 1) + y);
} else if (mv == 1) {
ans = std::min(ans, fn(r, c-1, 1) + x);
ans = std::min(ans, fn(r+1, c, 0) + y);
} else {
ans = std::min(ans, fn(r, c+1, 2) + x);
ans = std::min(ans, fn(r+1, c, 0) + y);
}
}
vis[r][c][mv] = stat;
return dp[r][c][mv] = ans;
}
signed main() {
fast_io;
int testCases=1;
// scanf("%d",&testCases);
std::cin >> testCases;
for (int TC = 1; TC <= testCases; TC++) {
stat++;
// scanf("%d%d",&n,&m);
// scanf("%d%d",&x,&y);
std::cin >> n >> m >> x >> y;
for (int i = 0; i < n; i++) {
std::cin >> std::ws;
for (int j = 0; j < m; j++) std::cin >> g[i][j];
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; ++j) std::cout << g[i][j];
// std::cout << '\n';
// }
lli ans = fn(0, 0, 0);
if (ans >= inf) ans = -1;
std::cout << ans << '\n';
}
return 0;
}
/*
*/