/*
* Copyright (c) 2024 Emon Thakur
* All rights reserved.
*/
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
char a[2002][2002];
ll n,m,x,y,ans=1e18;
void find(int i,int j,int dir,ll cost)
{
if(i>n || i<1 || j>m || j<1) return;
if(i==n && j==m)
{
ans = min(ans,cost);
return;
}
if(a[i][j]=='#') return;
if(dir==0)
{
find(i,j+1,0,cost+x);
find(i+1,j,2,cost+y);
}
else if(dir==1)
{
find(i,j-1,1,cost+x);
find(i+1,j,2,cost+y);
}
else
{
find(i+1,j,2,cost+x);
find(i,j+1,0,cost+y);
find(i,j-1,1,cost+y);
}
}
void solve()
{
cin >> n >> m >> x >> y;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cin >> a[i][j];
}
find(1,2,0,0);
find(2,1,2,0);
if(ans >= 1e18) cout<<-1<<endl;
else cout<<ans<<endl;
ans = 1e18;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin >> t; while(t--) solve(),ans =1e18;
}