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)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>   
using namespace std;
using namespace __gnu_pbds;
typedef long long ll; 
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll> > vv;
typedef vector<string>  vs;
typedef vector<pair<ll,ll>> vpr;
typedef vector<bool>vb;
typedef vector<double>vd;
typedef long double ld;
#define f(i,n) for(ll i = 0; i < n; i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define amax(x,y) if(y>x) x=y;
#define amin(x,y) if(y<x)x=y;
#define bg(x) x.begin()
#define sz(x) (ll)x.size()
#define in(x,n) for(ll i=0;i<n;i++)cin>>x[i]
#define out(x,n) for(ll i=0;i<n;i++)cout<<x[i]<<" " 
#define mxt(a) *(max_element(a.begin(),a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc ll t;cin>>t;while(t--)
typedef pair<ll,ll> pi;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define yesno(f) if(f) yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1,ll y1){if((x1%y1)==0)return x1/y1;else return x1/y1+1;}
ll power(ll a,ll b,ll m)
{
    if(b==0)
        return 1;
    ll d=power(a,b/2,m);
    d=(d*d)%m;
    if(b&1)
        d=(d*a)%m;
    return d;
}
const ll mod=1e9+7;
int MOD(int a)
{
    if(a<0)
        a+=mod;
    if(a>=mod)
        a%=mod;
    return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element (counting from zero) in the set in O(logn) time
// set_name.order_of_key(k) It returns to the number of items that are strictly smaller than our item k in O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
#define valid(i,j) (i>=0 && i<n && j>=0 && j<m)
void dfs(int x,int y,vector<vb> &floodfill,vs &grid,queue<pi>&que,vector<vb> &vst,vv &dist)
{
	vpr positions{{x+1,y},{x-1,y},{x,y-1},{x,y+1}};
	int n=sz(grid),m=sz(grid[0]);
	for(auto it:positions)
	{
		if(valid(it.fi,it.se) && grid[it.fi][it.se]=='.' && !floodfill[it.fi][it.se])
		{
			floodfill[it.fi][it.se]=true;
			vst[it.fi][it.se]=true;
			dist[it.fi][it.se]=dist[x][y];
			que.push({it.fi,it.se});
			dfs(it.fi,it.se,floodfill,grid,que,vst,dist);
		}
	}
}
int32_t main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int n,m;cin>>n>>m;
    int a,b,c,d;cin>>a>>b>>c>>d;
    a--;
    b--;
    c--;
    d--;
    vs grid(n);in(grid,n);
    vv dist(n,v(m,0));
    vector<vb> vst(n,vb(m,false)),floodfill(n,vb(m,false));
    vst[a][b]=true;
    queue<pi>que;
    que.push({a,b});

    while(!que.empty())
    {

    	queue<pi>que2;

    	while(!que.empty())
    	{

    		int x=que.front().fi,y=que.front().se;
    		que.pop();
    		que2.push({x,y});
    		if(floodfill[x][y])
    		{
    			continue;
    		}

    		dfs(x,y,floodfill,grid,que2,vst,dist);
    	}
    
    	while(!que2.empty())
    	{
    		int x=que2.front().fi,y=que2.front().se;
    		que2.pop();
    		for(int i=x-2;i<=x+2;i++)
    		{
    			for(int j=y-2;j<=y+2;j++)
    			{
    				if(valid(i,j) && !vst[i][j] && grid[i][j]=='.')
    				{
    					dist[i][j]=dist[x][y]+1;
    					vst[i][j]=true;
    					que.push({i,j});
    				}
    			}
    		}
    	}
    	// return 0;
    }
    if(!vst[c][d])
    {
    	cout<<-1<<endl;
    }
    else
    	cout<<dist[c][d]<<endl;
    return 0;
}