#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]=='#' && temp.dist+1>=arr[nx][ny])
pq.push(mp(temp.dist+1,nx,ny));
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+PW0gfHwgbnk8MCB8fCBueT49biB8fCB2aXNpdGVkW254XVtueV0pCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgIGlmKG5tW254XVtueV09PScjJyAmJiB0ZW1wLmRpc3QrMT49YXJyW254XVtueV0pCiAgICAgICAgICAgIHBxLnB1c2gobXAodGVtcC5kaXN0KzEsbngsbnkpKTsKICAgICAgICAgICBpZihubVtueF1bbnldPT0nLicpCiAgICAgICAgICAgIHBxLnB1c2gobXAodGVtcC5kaXN0KzEsbngsbnkpKTsKICAgICAgIH0KICAgfQp9CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgaW50IHQ7Y2luPj50OwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGNpbj4+bT4+bjsKICAgICAgICBmb3IoaW50IGk9MDtpPG07aSsrKQogICAgICAgICAgICBjaW4+Pm5tW2ldOwogICAgICAgIGZvcihpbnQgaT0wO2k8bTtpKyspCiAgICAgICAgewogICAgICAgICAgICBmb3IoaW50IGo9MDtqPG47aisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjaW4+PmFycltpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpbnQgeDEseTE7CiAgICAgICAgY2luPj54MT4+eTE+PngyPj55MjsKICAgICAgICBtZW1zZXQodmlzaXRlZCxmYWxzZSxzaXplb2YgdmlzaXRlZCk7CiAgICAgICAgY291dDw8ZGZzKHgxLHkxKTw8IlxuIjsKICAgIH0KICAgIHJldHVybiAwOwp9