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 Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Main {

  private static final char BLOCK = '#';
  private static final char ROAD = '.';

  public static void main(String[] args) {
    resolve(System.in, System.out);
  }

  static void resolve(InputStream is, PrintStream pw) {
    Scanner sc = new Scanner(is);
    int h = Integer.parseInt(sc.next());
    int w = Integer.parseInt(sc.next());
    int ch = Integer.parseInt(sc.next()) - 1;
    int cw = Integer.parseInt(sc.next()) - 1;
    int dh = Integer.parseInt(sc.next()) - 1;
    int dw = Integer.parseInt(sc.next()) - 1;

    char maze[][] = new char[h][w];
    int cost[][] = new int[h][w];
    for (int i = 0; i < h; i++) {
      String line = sc.next();
      for (int j = 0; j < w; j++) {
        maze[i][j] = line.charAt(j);
        cost[i][j] = -1;
      }
    }
    SafeIndexMaze safeIndexMaze = new SafeIndexMaze(h, w, maze);
    int distance = 0;
    WalkResult result;
    List<int[]> nextArea = new ArrayList<>();
    nextArea.add(new int[]{ch, cw});
    while ((result = walk(
      safeIndexMaze,
      nextArea,
      dh, dw,
      cost, distance)).nextRequired()) {
      nextArea = warp(safeIndexMaze, result.sameDistanceArea, cost);
      distance++;
    }
    if (result.isGoalFound) {
      pw.println(distance);
    } else {
      pw.println(-1);
    }
  }

  static WalkResult walk(SafeIndexMaze safeIndexMaze,
    List<int[]> nextArea,
    int goalH, int goalW,
    int cost[][], int currentDistance) {
    Queue<int[]> dfsQueue = new LinkedList<>();
    WalkResult result = new WalkResult();
    List<int[]> sameDistanceArea = new ArrayList<>();
    result.sameDistanceArea = sameDistanceArea;
    dfsQueue.addAll(nextArea);
    boolean queued[][] = new boolean[safeIndexMaze.h][safeIndexMaze.w];
    int target[];
    while ((target = dfsQueue.poll()) != null) {
      int i = target[0];
      int j = target[1];
      if (cost[i][j] != -1) {
        continue;
      }
      cost[i][j] = currentDistance;
      sameDistanceArea.add(target);
      if (goalH == i && goalW == j) {
        result.isGoalFound = true;
        return result;
      }
      {
        int nextI = i + 1;
        int nextJ = j;
        if (safeIndexMaze.isRoad(nextI, nextJ)) {
          if (cost[nextI][nextJ] == -1 && !queued[nextI][nextJ]) {
            dfsQueue.add(new int[]{nextI, nextJ});
            queued[nextI][nextJ] = true;
          }
        }
      }
      {
        int nextI = i - 1;
        int nextJ = j;
        if (safeIndexMaze.isRoad(nextI, nextJ)) {
          if (cost[nextI][nextJ] == -1 && !queued[nextI][nextJ]) {
            dfsQueue.add(new int[]{nextI, nextJ});
            queued[nextI][nextJ] = true;
          }
        }
      }
      {
        int nextI = i;
        int nextJ = j + 1;
        if (safeIndexMaze.isRoad(nextI, nextJ)) {
          if (cost[nextI][nextJ] == -1 && !queued[nextI][nextJ]) {
            dfsQueue.add(new int[]{nextI, nextJ});
            queued[nextI][nextJ] = true;
          }
        }
      }
      {
        int nextI = i;
        int nextJ = j - 1;
        if (safeIndexMaze.isRoad(nextI, nextJ)) {
          if (cost[nextI][nextJ] == -1 && !queued[nextI][nextJ]) {
            dfsQueue.add(new int[]{nextI, nextJ});
            queued[nextI][nextJ] = true;
          }
        }
      }
    }
    return result;
  }

  static class WalkResult {

    boolean isGoalFound = false;
    List<int[]> sameDistanceArea;

    boolean nextRequired() {
      if (isGoalFound) {
        return false;
      }
      return !sameDistanceArea.isEmpty();
    }
  }

  static List<int[]> warp(SafeIndexMaze safeIndexMaze, List<int[]> startArea, int cost[][]) {
    boolean visited[][] = new boolean[safeIndexMaze.h][safeIndexMaze.w];
    List<int[]> canBeWarped = new ArrayList<>();
    for (int[] start : startArea) {
      for (int i = -2; i <= 2; i++) {
        int nextI = start[0] + i;
        if (nextI < 0 || nextI >= safeIndexMaze.h) {
          continue;
        }
        for (int j = -2; j <= 2; j++) {
          int nextJ = start[1] + j;
          if (safeIndexMaze.isRoad(nextI, nextJ)
            && !visited[nextI][nextJ]
            && cost[nextI][nextJ] == -1) {
            canBeWarped.add(new int[]{nextI, nextJ});
            visited[nextI][nextJ] = true;
          }
        }
      }
    }
    return canBeWarped;
  }

  static class SafeIndexMaze {

    int h;
    int w;
    char[][] area;

    SafeIndexMaze(int h, int w, char[][] area) {
      this.h = h;
      this.w = w;
      this.area = area;
    }

    char get(int i, int j) {
      if (i >= 0 && i < h && j >= 0 && j < w) {
        return area[i][j];
      }
      return BLOCK;
    }

    boolean isRoad(int i, int j) {
      return get(i, j) == ROAD;
    }
  }
}