Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

/* Aditya0412 */
#include <bits/stdc++.h>
using namespace std; 
typedef long long int ll;
#define pb push_back 
#define ve vector
#define vii vector < int >
#define vll vector < ll > 
#define pii pair < int , int >
#define pll pair < ll , ll >
#define vpl vector < pll >
#define fi first
#define all(a)      (a).begin(),(a).end()
#define si(x)       (int)((x).size())
#define debug(x)    cout<<#x<<" is "<<(x)<<endl;cout.flush();  
#define se second 
#define endl        '\n'
#define f(i,a,b)    for(int i=a;i<b;i++)
#define mem(a,x)          memset(a,x,sizeof(a))

// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
    os<<"["; 
    for(auto& x:v){os<<x<<", ";}
    return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
    return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===


// const ll mod=998244353;
const ll mod = 1000000000+7;
const ll N=1000000+6;
#define M_PI           3.14159265358979323846
void inp(ll *arr,ll n)
{
  for(int i=0;i<n;i++)
    cin>>arr[i];
}
void inp_mat(ll *arr,ll n,ll m)
{
    for(int i=0;i<n;i++)
      for(int j=0;j<m;j++)
        {
          ll x;cin>>x;
          *((arr+i*m) + j)=x;
        }
}
// syntax for inp_mat inp_mat((ll *)arr,n,m);
int x2[]={+1,-1,+0,+0};
int y2[]={+0,+0,+1,-1};
char arr[1005][1005];int r,c;
int sr,sc,er,ec;
ll ff(int i,int j)
{
  return (i-1)*c+j;
}
int par[N];
int sz[N];
int root(int  x)
{
  while(par[x]!=x)
    {
        par[x] = par[par[x]] ; 
        x=par[x];
    }
    return x;
}
void join(int x,int y)
{
    int xx=root(x),yy=root(y);
    if(xx!=yy)
    {
      if(sz[xx]>sz[yy])
      swap(xx,yy);

      par[xx]=yy;
      sz[yy]+=sz[xx];
    }
}
vector< int>  graph[N];
bool vis[N];
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
       cin>>r>>c;
       mem(vis,false);
      cin>>sr>>sc>>er>>ec;
      int snode=ff(sr,sc);
      int enode=ff(er,ec);
      for(int i=1;i<=r;i++)
      for(int j=1;j<=c;j++)
      {cin>>arr[i][j];
        if(arr[i][j]=='.')
        {
          par[ff(i,j)]=ff(i,j);
          sz[ff(i,j)]=1;
        }
      }
      for(int i=1;i<=r;i++)
      {
        for(int j=1;j<=c;j++)
        {
            for(int k=0;k<4;k++)
            {
              int ii=i+x2[k],jj=j+y2[k];
              if(ii>=1 and ii<=r and jj>=1 and jj<=c)
              {
                if(arr[ii][jj]=='.' and arr[i][j]=='.')
                {
                  join(ff(ii,jj),ff(i,j));
                  // join(ff(i,j),ff(ii,jj));
                }
              }
            }
        }
      }

      for(int i=1;i<=r;i++)
      {
        for(int j=1;j<=c;j++)
        {
          if(arr[i][j]=='.')
          {
            par[ff(i,j)]=root(ff(i,j));
            // dbg(i);dbg(j);dbg(par[ff(i,j)]);
          }
        }
      }

      for(int i=1;i<=r;i++)
      {
        for(int j=1;j<=c;j++)
        {
          for(int ii=i-2;ii<=i+2;ii++)
          {
            for(int jj=j-2;jj<=j+2;jj++)
            {
              if(ii>=1 and ii<=r and jj>=1 and jj<=c)
              {
                if(arr[ii][jj]=='.' and arr[i][j]=='.')
                {
                  if(par[ff(ii,jj)]!=par[ff(i,j)])
                  {
                    graph[par[ff(ii,jj)]].pb(par[ff(i,j)]);
                    graph[par[ff(i,j)]].pb(par[ff(ii,jj)]);
                  }
                }
              }
            }
          }
        }
      }
      
      snode=par[snode];enode=par[enode];
      if(snode==enode)
      {
        cout<<0;
        return 0;
      }
      // dbg(snode);dbg(enode);
      queue< pair< int,int > > q;
      q.push({snode,0});
      vis[snode]=true;
      while(!q.empty())
      {
        int node=q.front().fi;
        // dbg(node);
        int v=q.front().se;
        if(node==enode)
        {
          cout<<v;
          return 0;
        }
        q.pop();
        for(auto child: graph[node])
        {
          if(!vis[child])
          {
            q.push({child,v+1});
            vis[child]=true;
          }
        }

      }
      cout<<"-1";


    



}