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

import java.awt.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Scanner;

public class Main {

    public static final int MOD = 1000000007;

    public static int cnt = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        int ch = Integer.parseInt(sc.next());
        int cw = Integer.parseInt(sc.next());
        int dh = Integer.parseInt(sc.next());
        int dw = Integer.parseInt(sc.next());
        char[][] hw = new char[h + 2][w + 2];
        Deque<Point> dq = new ArrayDeque<>();
        boolean[][] isReached = new boolean[h + 2][w + 2];
        int[][] d = new int[h + 2][w + 2];
        for (int[] dd : d) {
            Arrays.fill(dd, Integer.MAX_VALUE);
        }
        d[ch][cw] = 0;
//        dq.add(new Point(cw, ch));
        for (char[] a : hw) {
            Arrays.fill(a, '#');
        }
        for (int i = 0; i < h; i++) {
            String s = sc.next();
            for (int j = 0; j < w; j++) {
                hw[i + 1][j + 1] = s.charAt(j);
            }
        }
//        bfs(hw, d, dw + 1, dh + 1, dq);
        hw[dh][dw] = 'g';
        if (dfs(0, cw, ch, h, w, hw, isReached, dq)) {
            System.out.println(cnt);
        } else {
            System.out.println(-1);
        }
    }

    private static boolean dfs(int jump, int x, int y, int h, int w, char[][] hw, boolean[][] isReached, Deque<Point> dq) {
        if (hw[y][x] == '#') {
            if (y - 1 >= 1 && !isReached[y - 1][x] && (hw[y - 1][x] == '.' || hw[y - 1][x] == 'g')) {
                dq.addFirst(new Point(x, y - 1));
            }
            if (y + 1 <= h && !isReached[y + 1][x] && (hw[y + 1][x] == '.' || hw[y + 1][x] == 'g')) {
                dq.addFirst(new Point(x, y + 1));
            }
            if (x - 1 >= 1 && !isReached[y][x - 1] && (hw[y][x - 1] == '.' || hw[y][x - 1] == 'g')) {
                dq.addFirst(new Point(x - 1, y));
            }
            if (x + 1 <= w && !isReached[y][x + 1] && (hw[y][x + 1] == '.' || hw[y][x + 1] == 'g')) {
                dq.addFirst(new Point(x + 1, y));
            }
            if (y - 1 >= 1 && x - 1 >= 1 && !isReached[y - 1][x - 1] && (hw[y - 1][x - 1] == '.' || hw[y - 1][x - 1] == 'g')) {
                dq.addFirst(new Point(x - 1, y - 1));
            }
            if (y - 1 >= 1 && x + 1 <= w && !isReached[y - 1][x + 1] && (hw[y - 1][x + 1] == '.' || hw[y - 1][x + 1] == 'g')) {
                dq.addFirst(new Point(x + 1, y - 1));
            }
            if (y + 1 <= h && x - 1 >= 1 && !isReached[y + 1][x - 1] && (hw[y + 1][x - 1] == '.' || hw[y + 1][x - 1] == 'g')) {
                dq.addFirst(new Point(x - 1, y + 1));
            }
            if (y + 1 <= h && x + 1 <= w && !isReached[y + 1][x + 1] && (hw[y + 1][x + 1] == '.' || hw[y + 1][x + 1] == 'g')) {
                dq.addFirst(new Point(x + 1, y + 1));
            }
            return false;
        }
        if (isReached[y][x]) {
            return false;
        }
        if (hw[y][x] == 'g') {
            cnt = Math.min(cnt, jump);
            return true;
        }
        isReached[y][x] = true;
        if (dfs(jump, x + 1, y, h, w, hw, isReached, dq)) {
            return true;
        }
        if (dfs(jump, x - 1, y, h, w, hw, isReached, dq)) {
            return true;
        }
        if (dfs(jump, x, y + 1, h, w, hw, isReached, dq)) {
            return true;
        }
        if (dfs(jump, x, y - 1, h, w, hw, isReached, dq)) {
            return true;
        }
        if (!dq.isEmpty()) {
            Point p = dq.pollLast();
            if (dfs(jump + 1, p.x, p.y, h, w, hw, isReached, dq)) {
                return true;
            }
        }
        return false;
    }

//    private static int bfs(char[][] hw, int[][] d, int gx, int gy, Deque<Point> dq) {
//        int[] dx = {1, 0, -1, 0};
//        int[] dy = {0, 1, 0, -1};
//        while (dq.size() > 0) {
//            Point p = dq.pollLast();
//            if (p.x == gx && p.y == gy) {
//                break;
//            }
//            for (int i = 0; i < 4; i++) {
//                int nx = p.x + dx[i];
//                int ny = p.y + dy[i];
//                if (hw[ny][nx] != '#' && d[ny][nx] == Integer.MAX_VALUE) {
//                    dq.addFirst(new Point(nx, ny));
//                    d[ny][nx] = Math.min(d[ny][nx], d[p.y][p.x] + 1);
//                }
//            }
//        }
//        return d[gy][gx];
//    }
}