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: Time Limit Exceeded)

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define repr(i, r, l) for (ll i = (ll)(r); i > (ll)(l); i--)
#define print(v) for (const auto itr : v){cout << itr << ' ';} cout << endl;
#define all(p) p.begin(), p.end()
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 998244353
#define endl "\n"
#define inf 1e18
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 20005;
ll ceel(ll a, ll b){if (a % b == 0)return a / b;return a / b + 1;}
ll modneg(ll a,ll m){a = a % m;if(a<0)a=a+m;return a;}
ll powermod(ll x, unsigned ll y, ll p){ll res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
ll dp[1005][1005], n, m, d1, d2;
bool arr[1005][1005];
vector <ll> x{-1, 1, 0, 0};
vector <ll> y{0, 0, -1, +1};
// void dfs(ll a,ll b)
// {
//     cout<<a<<" "<<b<<" "<<dp[a][b]<<endl;
//     if(a==d1 and b==d2)
//     {
//         cout<<dp[a][b];
//         exit(0);
//     }
//     rep(i,0,4)
//     {
//         ll x1=min(max(b+x[i],(ll)0),m-1);
//         ll y1=min(max(a+y[i],(ll)0),n-1);
//         // cout<<" "<<y1<<" "<<x1<<" "<<arr[y1][x1]<<" "<<dp[y1][x1]<<endl;
//         if(!arr[y1][x1] and dp[y1][x1]>dp[a][b])
//         {
//             dp[y1][x1]=dp[a][b];
//             dfs(y1,x1);
//         }
//     }
//     rep(i,max(a-2,(ll)0),min(a+3,n))
//     {
//         rep(j,max(b-2,(ll)0),min(b+3,m))
//         {
//             // cout<<" "<<i<<" "<<j<<" "<<dp[i][j]<<" "<<arr[i][j]<<endl;
//             if(!arr[i][j] and dp[i][j]>dp[a][b]+1)
//             {
//                 dp[i][j]=dp[a][b]+1;
//                 dfs(i,j);
//             }
//         }
//     }
// }

int main() 
{

fastIO;
ll test,s1,s2,c,d,i,j,sum=0;
cin>>n>>m>>s1>>s2>>d1>>d2;
rep(i,0,n)
{
    rep(j,0,m)
    {
        char c;
        cin>>c;
        if(c=='#')
            arr[i][j]=1;
    }
}

rep(i,0,n+3)
{
    rep(j,0,m+3)
        dp[i][j]=inf;
}
s1--, s2--, d1--, d2--;
dp[s1][s2]=0;
queue <pair<ll,ll>> q;
q.push({s1,s2});
while(!q.empty())
{
    auto p=q.front();
    q.pop();
    ll a=p.fi, b=p.se;
    
    rep(i, 0, 4)
    {
        ll x1 = min(max(b + x[i], (ll)0), m - 1);
        ll y1 = min(max(a + y[i], (ll)0), n - 1);
        // cout<<" "<<y1<<" "<<x1<<" "<<arr[y1][x1]<<" "<<dp[y1][x1]<<endl;
        if (!arr[y1][x1] and dp[y1][x1] > dp[a][b])
        {
            dp[y1][x1] = dp[a][b];
            q.push({y1, x1});
        }
    }
    rep(i, max(a - 2, (ll)0), min(a + 3, n))
    {
        rep(j, max(b - 2, (ll)0), min(b + 3, m))
        {
            // cout<<" "<<i<<" "<<j<<" "<<dp[i][j]<<" "<<arr[i][j]<<endl;
            if (!arr[i][j] and dp[i][j] > dp[a][b] + 1)
            {
                dp[i][j] = dp[a][b] + 1;
                q.push({i, j});
            }
        }
    }
}

// dfs(s1,s2);
if(dp[d1][d2]==inf)
    cout<<"-1\n";
else
    cout<<dp[d1][d2]<<endl;
return 0;
}