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# (Mono-csc 3.5.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
class d_2{
    public static void Main(){
        string[] str = Console.ReadLine().Split(' ');
        int h = int.Parse(str[0]);
        int w = int.Parse(str[1]);
        int[] c = Console.ReadLine().Split(' ').Select(s => int.Parse(s)-1).ToArray();
        int[] d = Console.ReadLine().Split(' ').Select(s => int.Parse(s)-1).ToArray();
        bool[,] map = new bool[h,w];
        for(int i=0; i<h; i++){
            string line = Console.ReadLine();
            for(int j=0; j<w; j++){
                map[i,j] = (line[j] == '.');
            }
        }
        int nx, ny;
        int[,] cost = new int[h,w];
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cost[i,j] = -1;
            }
        }
        int warp = 0;
        Queue<(int,int)> queue = new Queue<(int,int)>();
        List<(int,int)> list = new List<(int,int)>();
        queue.Enqueue((c[0],c[1]));
        while(queue.Count > 0){
            //Console.WriteLine(warp);
            bool[,] already = new bool[h,w];
            while(queue.Count > 0){
                var pos = queue.Dequeue();
                int x = pos.Item1;
                int y = pos.Item2;
                //Console.WriteLine("(x,y) = ({0}, {1})",x,y);
                if(already[x,y] || cost[x,y] >= 0){
                    continue;
                }
                list.Add((x,y));
                if(x > 0){
                    nx = x-1;
                    ny = y;
                    if(map[nx,ny]){
                        if(!already[nx,ny] && cost[nx,ny] == -1){
                            queue.Enqueue((nx,ny));
                            list.Add((nx,ny));
                        }
                    }
                }
                if(x < h-1){
                    nx = x+1;
                    ny = y;
                    if(map[nx,ny]){
                        if(!already[nx,ny] && cost[nx,ny] == -1){
                            queue.Enqueue((nx,ny));
                            list.Add((nx,ny));
                        }
                    }
                }
                if(y > 0){
                    nx = x;
                    ny = y-1;
                    if(map[nx,ny]){
                        if(!already[nx,ny] && cost[nx,ny] == -1){
                            queue.Enqueue((nx,ny));
                            list.Add((nx,ny));
                        }
                    }
                }
                if(y < w-1){
                    nx = x;
                    ny = y+1;
                    if(map[nx,ny]){
                        if(!already[nx,ny] && cost[nx,ny] == -1){
                            queue.Enqueue((nx,ny));
                            list.Add((nx,ny));
                        }
                    }
                }
                already[x,y] = true;
                cost[x,y] = warp;
            }
            foreach(var sets in list){
                int x = sets.Item1;
                int y = sets.Item2;
                //Print(x,y,"");
                for(int i=(int)Math.Max(x-2,0); i<=(int)Math.Min(x+2,h-1); i++){
                    for(int j=(int)Math.Max(y-2,0); j<=(int)Math.Min(y+2,w-1); j++){
                        //Print(i,j,"For");
                        if(cost[i,j] >= 0){
                            continue;
                        }
                        if(i == x && j == y){
                            continue;
                        }
                        if(map[i,j]){
                            queue.Enqueue((i,j));
                        }
                    }
                }
            }
            //Print(cost,h,w);
            warp++;
        }
        Console.WriteLine(cost[d[0],d[1]]);
    }
    public static void Print(int[,] cost, int h, int w){
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                Console.Write("{0} ",cost[i,j]);
            }
            Console.WriteLine();
        }
        Console.WriteLine("--------------");
    }
    public static void Print(int a, int b, string s){
        Console.WriteLine("{0}: {1} {2}",s,a,b);
    }
}