#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define pb push_back
#define mod 1000000007
#define f(i,l,n) for(int i=l;i<n;i++)

vector<pair<ll,ll> > v[1000001];
ll mat[1000][1000];
ll vis[1000][1000];
ll vis2[1000][1000];
string s[1000];
deque<ll> dq;
//ncr mod p ends here
void bfs(ll val,ll i,ll j,ll h,ll w)
{
    if(s[i][j]=='.'&&!vis[i][j])
    {
      // cout<<"value "<<val<<" assigned to "<<i<<","<<j<<endl;
        vis[i][j]=1;
        mat[i][j]=val;
        v[val].pb({i,j});
        if(i-1>=0&&s[i-1][j]=='.'&&!vis[i-1][j])
        bfs(val,i-1,j,h,w);
        if(i+1<h&&s[i+1][j]=='.'&&!vis[i+1][j])
        bfs(val,i+1,j,h,w);
        if(j-1>=0&&s[i][j-1]=='.'&&!vis[i][j-1])
        bfs(val,i,j-1,h,w);
        if(j+1<w&&s[i][j+1]=='.'&&!vis[i][j+1])
        bfs(val,i,j+1,h,w);
        
        
    }
    
    
   return; 
    
}

void bfs2(ll i,ll j,ll states[],ll visstates[],ll h,ll w)
{
    
    //cout<<"bfs2 for "<<i<<" and "<<j<<endl;
    if(!vis2[i][j]&&s[i][j]=='.')
    {
        vis2[i][j]=1;
        f(ii,i-2,i+3)
        {
            f(jj,j-2,j+3)
            {

               
               
                if(ii>=0&&ii<h&&jj>=0&&jj<w&&!vis2[ii][jj]&&s[ii][jj]=='.'&&mat[ii][jj]!=mat[i][j]&&!visstates[mat[ii][jj]])
            {   
              //  cout<<"pushed "<<mat[ii][jj]<<" in the deque"<<endl;
                dq.push_back(mat[ii][jj]);
                  
                    states[mat[ii][jj]]=min(states[mat[ii][jj]],states[mat[i][j]]+1);
                    
                    visstates[mat[ii][jj]]=1;
                    
                
            }   
            }
        }
        
        
        
        
    }
    
    
  return;  
}


int main() {
 
ll h,w;
cin>>h>>w;
ll components=1;


f(i,0,h)
{f(j,0,w)
{vis[i][j]=0;
mat[i][j]=-2;

}
}

ll sh,sw;
cin>>sh>>sw;
ll eh,ew;
cin>>eh>>ew;
sh--;
sw--;
eh--;
ew--;
f(i,0,h)
cin>>s[i];
//cout<<70<<endl;
f(i,0,h)
{
    f(j,0,w)
    {
        
        if(!vis[i][j]&&s[i][j]=='.')
     {   bfs(components,i,j,h,w);
            components++;
         
     }
    }
}
//cout<<69<<endl;
/*f(i,0,h)
{
    f(j,0,w)
    {
        cout<<mat[i][j]<<" ";
    }
    cout<<endl;
}
cout<<endl;
*/
if(mat[sh][sw]==mat[eh][ew])
cout<<0;
else
{
  ll states[components+1];
  f(i,0,components+1)
  states[i]=100000000000000;
  
  dq.pb(mat[sh][sw]);
  
  
   ll visstates[components+1]={0};

  f(i,0,h)
  f(j,0,w)
  vis2[i][j]=0;

    states[mat[sh][sw]]=0;
    while(!dq.empty())
    {
        ll temp=dq.front();
   //     cout<<"temp= "<<temp<<endl;
      

       deque <ll> :: iterator it; 
    /*for (it = dq.begin(); it != dq.end(); ++it) 
        cout << '\t' << *it; 
    cout << '\n'; */
        f(i,0,v[temp].size())
        {
        
         bfs2(v[temp][i].first,v[temp][i].second,states,visstates,h,w);
         
        }
        
  //  for (it = dq.begin(); it != dq.end(); ++it) 
  //      cout << '\t' << *it; 
  //  cout << '\n'; 
        dq.pop_front();
    
        
        
        
    }
    
    
    
    
    
    
    if(!visstates[mat[eh][ew]])
    cout<<-1;
    else
    cout<<states[mat[eh][ew]]<<endl;
    
}

  return 0;  
}

Measured memory_bytes: 65052