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.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        Main m = new Main();
        m.solve(sc);
    }
    int[] gp;
    //Integer[][] aka;
    char[][] arr;
    int[][] dirs = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
    boolean[][] aka;
    void solve(Scanner sc){
        int h = sc.nextInt();
        int w = sc.nextInt();
        int r = sc.nextInt()-1;
        int c = sc.nextInt()-1;
        int er = sc.nextInt()-1;
        int ec = sc.nextInt()-1;
        arr = new char[h][w];
        for(int i=0;i<h;i++){
            arr[i] = sc.next().toCharArray();
        }
        aka = new boolean[h][w];
        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)->(a[2]-b[2]));
        pq.add(new int[]{r,c,0});
        aka[r][c] = true;
        while(!pq.isEmpty()){
            int[] ak = pq.poll();
            if(ak[0]==er&&ak[1]==ec){
                System.out.println(ak[2]);
                return;
            }
            dfs(ak[0],ak[1],h,w,pq,ak[2]);
            for(int k=Math.max(0,ak[0]-2);k<=Math.min(h-1,ak[0]+2);k++){
                for(int p=Math.max(0,ak[1]-2);p<=Math.min(w-1,ak[1]+2);p++){
                    if(!aka[k][p]&&arr[k][p]!='#'){
                        pq.add(new int[]{k,p,ak[2]+1});
                        aka[k][p] = true;
                    }
                }
            }

        }
        System.out.println(-1);

    }
    void dfs(int i, int j,int h, int w,PriorityQueue<int[]> pq, int v){
        for(int[] dir :dirs){
            int pi = i+dir[0];
            int pj = j+dir[1];
            if(pi<0||pj<0||pi>=h||pj>=w||aka[pi][pj]||arr[pi][pj]=='#') continue;
            pq.add(new int[]{pi,pj,v});
            aka[pi][pj] = true;
            dfs(pi,pj,h,w ,pq,v);
        }
    }
    int find(int v){
        if(gp[v]<0) return v;
        //System.out.println(v);
        return gp[v] = find(gp[v]);
    }

    void g(int a, int b){
        int pa = find(a);
        int pb = find(b);
        if(pa==pb) return;
        if(gp[pa]<gp[pb]){
            gp[pa]+=gp[pb];
            gp[pb] = pa;
        }else{
            gp[pb]+=gp[pa];
            gp[pa] =pb;
        }
    }
    static long cmp(int[] a, int[] b){
        return (long)Math.abs(a[0]-b[0])+(long)Math.abs(a[1]-b[1]);
    }
}