#include<bits/stdc++.h>
using namespace std;
string nm[205];int arr[205][205];
bool visited[205][205];int m,n;int x2,y2;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
#define set1 pair< int,pair<int,int> >
#define dist first
#define a second.first
#define b second.second
#define mp(a1,b1,c1) make_pair(a1,make_pair(b1,c1))
int dfs(int x,int y)
{
priority_queue< set1,vector<set1>,greater<set1> >pq;
pq.push(mp(0,x,y));
while(!pq.empty())
{
set1 temp=pq.top();pq.pop();
if(temp.a==x2 && temp.b==y2)
return temp.dist;
if(visited[temp.a][temp.b]==true)
continue;
//cout<<temp.a<<" "<<temp.b<<temp.dist<<"\n";
visited[temp.a][temp.b]=true;
for(int i=0;i<4;i++)
{
int nx=temp.a+dx[i];
int ny=temp.b+dy[i];
if(nx<0 || nx>=m || ny<0 || ny>=n || visited[nx][ny])
continue;
if(nm[nx][ny]=='#')
{
if (temp.dist+1>=arr[nx][ny])
pq.push(mp(temp.dist+1,nx,ny));
else
pq.push(mp(arr[nx][ny],nx,ny));
} else
if(nm[nx][ny]=='.')
pq.push(mp(temp.dist+1,nx,ny));
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;cin>>t;
while(t--)
{
cin>>m>>n;
for(int i=0;i<m;i++)
cin>>nm[i];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
int x1,y1;
cin>>x1>>y1>>x2>>y2;
memset(visited,false,sizeof visited);
cout<<dfs(x1,y1)<<"\n";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RyaW5nIG5tWzIwNV07aW50IGFyclsyMDVdWzIwNV07CmJvb2wgdmlzaXRlZFsyMDVdWzIwNV07aW50IG0sbjtpbnQgeDIseTI7CmludCBkeFtdPXsxLC0xLDAsMH07CmludCBkeVtdPXswLDAsMSwtMX07CiNkZWZpbmUgc2V0MSBwYWlyPCBpbnQscGFpcjxpbnQsaW50PiA+CiNkZWZpbmUgZGlzdCBmaXJzdAojZGVmaW5lIGEgc2Vjb25kLmZpcnN0CiNkZWZpbmUgYiBzZWNvbmQuc2Vjb25kCiNkZWZpbmUgbXAoYTEsYjEsYzEpIG1ha2VfcGFpcihhMSxtYWtlX3BhaXIoYjEsYzEpKQppbnQgZGZzKGludCB4LGludCB5KQp7CiAgIHByaW9yaXR5X3F1ZXVlPCBzZXQxLHZlY3RvcjxzZXQxPixncmVhdGVyPHNldDE+ID5wcTsKICAgcHEucHVzaChtcCgwLHgseSkpOwogICB3aGlsZSghcHEuZW1wdHkoKSkKICAgewogICAgICAgc2V0MSB0ZW1wPXBxLnRvcCgpO3BxLnBvcCgpOwogICAgICAgaWYodGVtcC5hPT14MiAmJiB0ZW1wLmI9PXkyKQogICAgICAgIHJldHVybiB0ZW1wLmRpc3Q7CiAgICAgICBpZih2aXNpdGVkW3RlbXAuYV1bdGVtcC5iXT09dHJ1ZSkKICAgICAgICBjb250aW51ZTsKICAgICAgIC8vY291dDw8dGVtcC5hPDwiICI8PHRlbXAuYjw8dGVtcC5kaXN0PDwiXG4iOwogICAgICAgdmlzaXRlZFt0ZW1wLmFdW3RlbXAuYl09dHJ1ZTsKICAgICAgIGZvcihpbnQgaT0wO2k8NDtpKyspCiAgICAgICB7CiAgICAgICAgICAgaW50IG54PXRlbXAuYStkeFtpXTsKICAgICAgICAgICBpbnQgbnk9dGVtcC5iK2R5W2ldOwogICAgICAgICAgIGlmKG54PDAgfHwgbng+PW0gfHwgbnk8MCB8fCBueT49biB8fCB2aXNpdGVkW254XVtueV0pCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgIGlmKG5tW254XVtueV09PScjJykKICAgICAgICAgICB7CgkJCWlmICh0ZW1wLmRpc3QrMT49YXJyW254XVtueV0pCiAgICAgICAgICAgIHBxLnB1c2gobXAodGVtcC5kaXN0KzEsbngsbnkpKTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICBwcS5wdXNoKG1wKGFycltueF1bbnldLG54LG55KSk7CiAgICAgIAkgICB9IGVsc2UKIAogICAgICAgICAgIGlmKG5tW254XVtueV09PScuJykKICAgICAgICAgICAgcHEucHVzaChtcCh0ZW1wLmRpc3QrMSxueCxueSkpOwogICAgICAgfQogICB9Cn0KaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICBpbnQgdDtjaW4+PnQ7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgY2luPj5tPj5uOwogICAgICAgIGZvcihpbnQgaT0wO2k8bTtpKyspCiAgICAgICAgICAgIGNpbj4+bm1baV07CiAgICAgICAgZm9yKGludCBpPTA7aTxtO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaj0wO2o8bjtqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNpbj4+YXJyW2ldW2pdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGludCB4MSx5MTsKICAgICAgICBjaW4+PngxPj55MT4+eDI+PnkyOwogICAgICAgIG1lbXNldCh2aXNpdGVkLGZhbHNlLHNpemVvZiB2aXNpdGVkKTsKICAgICAgICBjb3V0PDxkZnMoeDEseTEpPDwiXG4iOwogICAgfQogICAgcmV0dXJuIDA7Cn0g