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 <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
string S[1000];
int area_id[1000][1000];
int move_h[4] = {-1, 1, 0, 0};
int move_w[4] = {0, 0, -1, 1};
void dfs(int cur_h, int cur_w, int id, int H, int W){
  area_id[cur_h][cur_w] = id;

  for(int i = 0; i < 4; i++){
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];
    if(0 <= next_h && next_h < H && 0 <= next_w && next_w < W
        && area_id[next_h][next_w] < 0 && S[next_h][next_w] != '#'){
      dfs(next_h, next_w, id, H, W);
    }
  }
}

int d[1000000];
set <int> edges[1000000];

int main(){
  int H;
  int W;
  cin >> H >> W;

  int C_h;
  int D_h;
  int C_w;
  int D_w;

  cin >> C_h >> C_w;
  cin >> D_h >> D_w;
  C_h --;
  C_w --;
  D_h --;
  D_w --;

  for(int i = 0; i < H; i++){
    cin >> S[i];
  }

  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      area_id[i][j] = -1;
    }
  }

  int total_id = 0;
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(area_id[i][j] < 0 && S[i][j] != '#'){
        dfs(i, j, total_id, H, W);
        total_id++;
      }
    }
  }


  for(int i = 0; i < total_id; i++){
    d[i] = INT_MAX;
  }


  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(S[i][j] == '#'){
        continue;
      }
      int cur_id = area_id[i][j];
      for(int k = -2; k <= 2; k++){
        for(int l = -2; l <= 2; l++){
          int adj_h = i + k;
          int adj_w = j + l;
          if(0 <= adj_h && adj_h < H && 0 <= adj_w && adj_w < W
              && area_id[adj_h][adj_w] != cur_id && S[adj_h][adj_w] != '#'){
            edges[cur_id].insert(area_id[adj_h][adj_w]);
          }
        }
       }
    }
  }

  d[area_id[C_h][C_w]] = 0;
  queue <int> q;
  q.push(area_id[C_h][C_w]);
  while(!q.empty()){
    int cur = q.front();
    q.pop();
    for(auto it = edges[cur].begin(); it != edges[cur].end(); it++){
      int next = *it;
      if(d[next] == INT_MAX){
        d[next] = 1 + d[cur];
        q.push(next);
      }
    }
  }

  if(d[area_id[D_h][D_w]] == INT_MAX){
    cout << -1 << endl;
  } else {
    cout << d[area_id[D_h][D_w]] << endl;
  }

  return 0;
}