#include <bits/stdc++.h>
using namespace std;
// #include "debug.h"
#define int int64_t
const int inf = 1e16;
void solve() {
int n, m, x, y;
cin >> n >> m >> x >> y;
vector<vector<char>> s(n, vector<char>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> s[i][j];
}
}
vector<vector<array<int, 2>>> dp(n, vector<array<int, 2>>(m, {inf, inf}));
if (s[0][0] != '#') {
dp[0][0][0] = -1; // 0 means water from left
dp[0][0][1] = -1; // 1 means water from top
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i][j] == '#') {
continue;
}
if (i - 1 >= 0) {
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1] + x);
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + y);
}
if (j - 1 >= 0) {
dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1] + y);
dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][0] + x);
}
}
}
int ans = min(dp[n - 1][m - 1][0], dp[n - 1][m - 1][1]);
cout << (ans >= inf ? -1 : ans) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tc = 1;
cin >> tc;
while (tc--) {
solve();
}
}