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: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define mod 1000000007
int gcd(int a, int b) 
{ 
    if (a == 0) 
        return b; 
    return gcd(b % a, a); 
} //log(a+b) complexity
int Max(int a,int b){
	return a>b?a:b;
}
int Min(int a,int b){
	return a<b?a:b;
}
int h,w;
bool valid(int i,int j){
	if(i>=0&&i<h&&j>=0&&j<w){
		return true;
	}
	return false;
}
int help(char **a,int h,int w,int i,int j,int x,int y,bool **visited,int **ans){
	if(i==x&&j==y){
		return 0;
	}
	if(ans[i][j]!=-7777){
		
		return ans[i][j];
			}
	//cout<<i+1<<" "<<j+1<<endl;
	visited[i][j]=true;
	bool ok=false;
	int sa1=99999999,sa2=999999999,sa3=999999999,sa4=999999999;
	if(valid(i+1,j)&&visited[i+1][j]==false&&a[i+1][j]=='.'){
		 sa1=help(a,h,w,i+1,j,x,y,visited,ans);
		 ok=true;
		
	}
	if(valid(i,j+1)&&visited[i][j+1]==false&&a[i][j+1]=='.'){
		 sa2=help(a,h,w,i,j+1,x,y,visited,ans);
		  ok=true;
		
	}
		if(valid(i-1,j)&&visited[i][j]==false&&a[i-1][j]=='.'){
			 sa3=help(a,h,w,i-1,j,x,y,visited,ans);
			  ok=true;
		
	}
		if(valid(i,j-1)&&visited[i][j]==false&&a[i][j-1]=='.'){
			 sa4=help(a,h,w,i,j-1,x,y,visited,ans);
			  ok=true;
		
	}
	int sa=99999999;
	
	if(ok==false){
		/*for(int k=i-2;k<=i+2;k++){
		if(valid(k,j)&&a[k][j]=='.'&&visited[k][j]==false)
		sa=min(sa,help(a,h,w,k,j,x,y,visited));
		}
		for(int l=j-2;l<=j+2;l++){
			if(valid(i,l)&&a[i][l]=='.'&&visited[i][l]==false)
				sa=min(sa,help(a,h,w,i,l,x,y,visited));
			
		}
		return sa+1;*/
		for(int k=i-2;k<=i+2;k++){
		for(int l=j-2;l<=j+2;l++){
			if(valid(k,l)&&a[k][l]=='.'&&visited[k][l]==false)
		sa=Min(sa,help(a,h,w,k,l,x,y,visited,ans));
		}
	}
	visited[i][j]=false;
	return ans[i][j]= sa+1;
	}
	
	else{
		int  aa=Min(sa1,sa2);
		int bb=Min(aa,sa3);
		int cc=Min(bb,sa4);
		visited[i][j]=false;
		return ans[i][j]= cc;
	}
		
	visited[i][j]=false;
}
void testcase(){
//int h,w;
cin>>h>>w;
int ch,cw;
cin>>ch>>cw;
int dh,dw;
cin>>dh>>dw;
char **a=new char *[h];
int **anss=new int *[h];
bool **visited=new bool*[h]; 
for(int i=0;i<h;i++){
	a[i]=new char[w];
	anss[i]=new int[w];
	visited[i]=new bool[w];
	for(int j=0;j<w;j++){
		cin>>a[i][j];
		visited[i][j]=false;
		anss[i][j]=-7777;
}
}
ch--;
cw--;
dh--;
dw--;
int ans=help(a,h,w,ch,cw,dh,dw,visited,anss);
if(ans<=9999998){
	cout<<ans<<endl;
}
else{
	cout<<-1<<endl;
}
 
 
 
return;
}
int32_t main(){
ios_base::sync_with_stdio(false); 
   cin.tie(NULL);
 cout.tie(NULL); 
int t=1,k=1;
//cin>>t;
do{
//cout<<"Case #"<<k<<":"<<" ";
testcase();
k++;
t--;
}while(t>0);
 
return 0;
}