// Always check for base cases
            // Check for overflows
            // Do something instead of sitting idle
            #include<bits/stdc++.h>
            #define ll long long
            #define f   first
            #define s   second
            #define pb          push_back
            #define mod        1000000007
            #define mod1       1000000009
            #define hell        998244353
            #define inf         1000000000000000000LL
            #define pi          3.14159265358979323
            //#define N           100005 
            const int N=1005;
            using namespace std;
            ll n,k,sum,q,res,m,val,ans;
            ll x,y;
            double p;
            ll xx[]={1,0,-1,0};
            ll yy[]={0,1,0,-1};
            ll dia=0;
            ll dis[N][N];
            bool vis[N][N];
            bool issafe(ll x,ll y,vector<string> &v)
            {
                return (x>=0 and x<n and y>=0 and y<m and !vis[x][y] and v[x][y]=='.');
            }
            void check(ll x,ll y,vector<string> &v,queue<pair<ll,ll>> &q)
            {
                ll l1=max(0LL,x-2);
                ll l2=min(n-1,x+2);
                ll c1=max(0LL,y-2);
                ll c2=min(m-1,y+2);
                for(int i=l1;i<=l2;i++){
                    for(int j=c1;j<=c2;j++)
                    {
                        if(i==x and j==y) continue;
                        if(v[i][j]=='.' and !vis[i][j]) {
                            dis[i][j]=min(dis[i][j],dis[x][y]+1);
                            vis[i][j]=1;
                            q.push({i,j});}
                    }
                }
            }
            void solve()    
            {   
                cin>>n>>m;
                ll r1,c1,r2,c2;
                cin>>r1>>c1;
                cin>>r2>>c2;
                r1--;c1--;r2--;c2--;
                vector<string> v(n);
                for(int i=0;i<n;i++) cin>>v[i];
                queue<pair<ll,ll>> q;
                q.push({r1,c1});
                for(int i=0;i<N;i++) for(int j=0;j<N;j++) dis[i][j]=inf;
                vis[r1][c1]=1;
                dis[r1][c1]=0;
                stack<pair<ll,ll>> st;
                while(1){
                while(!q.empty())
                {
                    x=q.front().f;
                    y=q.front().s;
                    st.push({x,y});
                    q.pop();
                    //cout<<x<<" "<<y<<"\n";
                    for(int i=0;i<4;i++)
                    {
                        if(issafe(x+xx[i],y+yy[i],v))
                        {   dis[x+xx[i]][y+yy[i]]=min(dis[x+xx[i]][y+yy[i]],dis[x][y]);
                            q.push({x+xx[i],y+yy[i]});
                            vis[x+xx[i]][y+yy[i]]=1;
                            //check(x+xx[i],y+yy[i],v,q);
                        }
                    }
                }
                if(st.size()==0) break;
                while(!st.empty()) {check(st.top().f,st.top().s,v,q);st.pop();}
            }
            //cout<<dis[r2][c2];
                if(dis[r2][c2]==inf) cout<<"-1";
                else cout<<dis[r2][c2];

                
            }
            int main()  
            {   ios_base::sync_with_stdio(false);
                cin.tie(0);cout.tie(0);
                int t=1;
                //cin>>t;
                 while(t--)
                    solve();
                
            }            


Measured memory_bytes: 13636