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)

//package atcoder.beginnner_176;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {

    static Stack<Integer> Ih = new Stack<>();
    static Stack<Integer> Iw = new Stack<>();

    public static void main (String[] args) throws Exception {

        String s = "4 4\n" +
                "1 1\n" +
                "4 4\n" +
                "#..#\n" +
                "####\n" +
                "##.#\n" +
                "###.";

//        br = new BufferedReader(new StringReader(s));
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));

        rl(); int H = nin(), W=nin();
        rl(); int ch = nin()-1, cw=nin()-1;
        rl(); int dh = nin()-1, dw=nin()-1;

        char S[][] = new char[H][]; int G[][] = new int[H][];
        for (int h=0; h<H; ++h) {
            rl();S[h]=nca(); G[h]=new int[W];
        }

        
        if (ch==dh && cw==dw) {
            bw.write(0 + "\n");
        } else if (S[dh][dw]=='#') {
            bw.write(-1 + "\n");
        } else {
            int c=S[ch][cw]=='.'?1:2;
            Ih.push(ch); Iw.push(cw);
            while (G[dh][dw]==0 && !Ih.isEmpty()) {
                fill(S,G,H,W,c);
                ++c;
            }
            bw.write((G[dh][dw]==0?-1:G[dh][dw]-1)+"\n");
        }

        bw.flush();
    }

    static int m[][] = new int[][]{new int[]{1,0}, new int[]{-1,0}, new int[]{0,1}, new int[]{0,-1}};

    static void fill(char S[][], int G[][], int H, int W, int c) {
        Set<Integer> B = new HashSet<>();
        while (!Ih.isEmpty()) {
            int _h = Ih.pop(); int _w=Iw.pop();
            if (G[_h][_w]==c) continue;
            if (S[_h][_w]=='.') {
                G[_h][_w]=c;
            } else {
                B.add((_h*10000)+_w);
                continue;
            }
            for (int[] _m:m) {
                int h2=_h+_m[0]; int w2=_w+_m[1];
                if (h2>H-1 || h2<0 || w2>W-1 || w2<0) continue;
                Ih.push(h2); Iw.push(w2);
            }
        }

        for (int b:B) {
            int _h=b/10000, _w=b%10000;
            for (int[] _m:m) {
                int h2=_h+_m[0]; int w2=_w+_m[1];
                if (h2>H-1 || h2<0 || w2>W-1 || w2<0) continue;
                if (S[h2][w2]=='#' || G[h2][w2]!=0) continue;
                Ih.push(h2); Iw.push(w2);
            }
        };
    }

    static BufferedReader br; static BufferedWriter bw; static StringTokenizer st;
    static void rl() throws Exception{ st = new StringTokenizer(br.readLine()); }
    static long nlo(){ return Long.parseLong(st.nextToken()); }
    static int nin(){ return Integer.parseInt(st.nextToken()); }
    /*private static void te(){}*/
    static double ndo(){ return Double.parseDouble(st.nextToken()); }
    static char[] nca(){ return st.nextToken().toCharArray(); }
}