#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;
}