////////////
//||||| ||//
//   || ||//
//||||||||//
//|| ||   //
//|| |||||//
////////////
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define   ll  long long int
#define   dl double
#define   llu unsigned long long int
#define   scll(x) scanf("%lld",&x)
#define   scld(x) scanf("%lf",&x)
#define   scull(x) scanf("%llu",&x)
#define   scch(x) scanf(" %c",&x)
#define   pb(x) push_back(x)
#define   pf(x) push_front(x)
#define   ppb() pop_back()
#define   ppf() pop_front()
#define   Pi acos(-1.0)
#define   inf 1000000000000000000
#define   mod 1000000007
#define   ff first
#define   ss second
#define   mpr make_pair
#define   debug(x) cout<<x<<'\n';
#define   MAX 1000000
#define   eps 1e-8
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
char mp[10000][10000];
ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, -1, 1};
ll ans[10000][10000];
//vector<pair<ll,ll> >vec;
int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll a, b, c = 0, d, e, f = 0, g, h, i, j, k, m, n, o, p, s, t, v, w, x = 0, y, z, l,ex,ey;
  ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0,x1,y1,x3,y3,x4,y4,x2,y2;
  ll prevsum = 0, ans1 = inf,ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2, str3 = "", str4 = "";
  ll ide = 0, idx = 0, Q;
  scll(h);
  scll(w);
  scll(x);
  scll(y);
  scll(ex);
  scll(ey);
  x--;
  y--;
  ex--;
  ey--;
  for (i = 0; i < h; i++)
  {
    for (j = 0; j < w; j++)
    {
      scch(mp[i][j]);
    }
  }
  for(i=0;i<10000;i++)
  {
    for(j=0;j<10000;j++)
      ans[i][j]=inf;
  }
deque<pair<ll,ll>>dq;

   dq.pb(mpr(x,y));
   ans[x][y]=0;
   while(!dq.empty())
   {
       x1=dq.front().ff;
       y1=dq.front().ss;
       dq.ppf();
     //if(ans[x1][y1]==inf)continue;
       for(i=0;i<4;i++)
       {
         x3=x1+dx[i];
         y3=y1+dy[i];
        if(x3>=0&&x3<h&&y3>=0&&y3<w)
        {
            if(mp[x3][y3]=='.'&&ans[x3][y3]>ans[x1][y1])
             {
                 ans[x3][y3]=ans[x1][y1];
                 dq.pf(mpr(x3,y3));
             }
        }
       }

       for(i=-2;i<=2;i++)
       {
        for(j=-2;j<=2;j++)
        {
          if((x1+i)>=0&&(x1+i)<h&&(y1+j)>=0&&(y1+j)<w)
          {
               if(ans[x1+i][y1+j]>(ans[x1][y1]+1)&&mp[x1+i][y1+j]=='.')
               {
                    ans[x1+i][y1+j]=ans[x1][y1]+1;
                    dq.pb(mpr(x1+i,y1+j));
               }
          }
        }
       }
   }

   if(ans[ex][ey]!=inf)
    cout<<ans[ex][ey];
  else
    cout<<-1;
  return 0;
}

Measured memory_bytes: 790048