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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int h = In[0];
        int w = In[1];
        int[] In2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int sx = In2[0];
        int sy = In2[1];
        sx--; sy--;
        int[] In3 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int gx = In3[0];
        int gy = In3[1];
        gx--; gy--;
        char[,] C = new char[h, w];
        for (int i = 0; i < h; i++)
        {
            string s = Console.ReadLine();
            for (int j = 0; j < w; j++)
            {
                C[i, j] = s[j];
            }
        }
        var dis = new int[h, w];
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = 100;//100で初期化
        //BFS
        var deque = new LinkedList<Pair>();
        dis[sx, sy] = 0;
        deque.AddLast(new Pair(sx, sy));
        while (deque.Count() > 0)
        {
            var p = deque.First();
            deque.RemoveFirst();
            for (int i = 0; i < 4; i++)
            {
                int nx = p.x + dx[i];
                int ny = p.y + dy[i];
                //範囲外ならスキップ
                if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
                //移動先が壁で、そこに訪れたことがないならdequeの最後尾に追加
                if (dis[nx, ny] > (dis[p.x, p.y] + 1) && C[nx, ny] == '#')
                {
                    deque.AddLast(new Pair(nx, ny));
                    dis[nx, ny] = dis[p.x, p.y] + 1;
                }
                //移動先が壁で、そこに訪れたことがないならdequeの先頭に追加
                if (dis[nx, ny] > dis[p.x, p.y] && C[nx, ny] != '#')
                {
                    deque.AddFirst(new Pair(nx, ny));
                    dis[nx, ny] = dis[p.x, p.y];
                }
            }
        }
        if (dis[gx, gy] == 100) Console.WriteLine(-1);
        else Console.WriteLine(dis[gx, gy]);
    }
    public static int[] dx = { 0, 1, 0, -1 };
    public static int[] dy = { 1, 0, -1, 0 };
    public struct Pair
    {
        public int x, y;
        public Pair(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
}


//WA
//namespace D
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int h = In[0];
//            int w = In[1];
//            int[] In2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int sx = In2[0];
//            int sy = In2[1];
//            sx--; sy--;
//            int[] In3 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int gx = In3[0];
//            int gy = In3[1];
//            gx--; gy--;
//            char[,] S = new char[h, w];
//            for (int i = 0; i < h; i++)
//            {
//                string s = Console.ReadLine();
//                for (int j = 0; j < w; j++)
//                {
//                    S[i, j] = s[j];
//                }
//            }
//            int[,] dis = new int[h, w];
//            int INF = 1001001001;
//            for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = INF;
//            dis[sx, sy] = 0;
//            var que = new Queue<Pair>();
//            que.Enqueue(new Pair(sx, sy));
//            while (dis[gx, gy] == INF)
//            {
//                //いける範囲を埋める
//                while (que.Count > 0)
//                {
//                    var p = que.Dequeue();
//                    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 (nx < 0 || h <= nx || ny < 0 || w <= ny || S[nx, ny] == '#' || dis[nx, ny] != INF) continue;
//                        que.Enqueue(new Pair(nx, ny));
//                        dis[nx, ny] = dis[p.x, p.y];
//                    }
//                }
//                int[,] Cpy= new int[h,w];
//                Array.Copy(dis, Cpy, h * w);
//                bool change = false;
//                for(int x = 0; x < h; x++)
//                {
//                    for (int y = 0; y < w; y++)
//                    {
//                        if (dis[x, y] == INF) continue;
//                        for (int i = -2; i <= 2; i++)
//                        {
//                            for (int j = -2; j <= 2; j++)
//                            {
//                                int wx = x + i;
//                                int wy = y + j;
//                                if (wx < 0 || h <= wx || wy < 0 || w <= wy || S[wx, wy] == '#' || dis[wx, wy] != INF) continue;
//                                if (Cpy[wx, wy] < dis[x, y] + 1) continue;
//                                Cpy[wx, wy] = dis[x, y] + 1;
//                                change = true;
//                            }
//                        }
//                    }
//                }
//                if (!change) break;
//                Array.Copy(Cpy,dis, h * w);
//            }
//            if (dis[gx, gy] == INF) Console.WriteLine(-1);
//            else Console.WriteLine(dis[gx,gy]);
//        }
//        public static int[] dx = { 0, 1, 0, -1 };
//        public static int[] dy = { 1, 0, -1, 0 };
//        public struct Pair
//        {
//            public int x, y;
//            public Pair(int x, int y)
//            {
//                this.x = x;
//                this.y = y;
//            }
//        }
//    }
//}

//namespace D
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int h = In[0];
//            int w = In[1];
//            int[] In2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int sx = In2[0];
//            int sy = In2[1];
//            sx--;sy--;
//            int[] In3 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
//            int gx = In3[0];
//            int gy = In3[1];
//            gx--;gy--;
//            char[,] S = new char[h, w];
//            for(int i = 0; i < h; i++)
//            {
//                string s = Console.ReadLine();
//                for (int j = 0; j < w; j++)
//                {
//                    S[i,j] = s[j];
//                }
//            }
//            int[,] dis = new int[h, w];
//            int INF = 1001001001;
//            for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) dis[i, j] = INF;
//            dis[sx, sy] = 0;
//            var que = new Queue<Pair>();
//            que.Enqueue(new Pair(sx, sy));
//            int warp = 0;
//            while (dis[gx,gy]==INF)
//            {
//                //いける範囲を埋める
//                while (que.Count > 0)
//                {
//                    var p = que.Dequeue();
//                    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 (nx < 0 || h <= nx || ny < 0 || w <= ny || S[nx, ny] == '#' || dis[nx, ny] != INF) continue;
//                        que.Enqueue(new Pair(nx, ny));
//                        dis[nx, ny] = dis[p.x, p.y] + 1;
//                    }
//                }
//                if (dis[gx, gy] != INF) break;
//                //壁を超える
//                bool change = false;
//                bool[,] renew = new bool[h, w];
//                for (int i = 0; i < h; i++)
//                {
//                    for (int j = 0; j < w; j++)
//                    {
//                        for (int x = -2; x <= 2; x++)
//                        {
//                            for (int y = -2; y <= 2; y++)
//                            {
//                                int mx = i + x;
//                                int my = j + y;
//                                if (mx < 0 || h <= mx || my < 0 || w <= my || S[mx, my] == '#'|| S[i,j]=='#') continue;
//                                if (renew[mx, my]) continue;
//                                if (dis[i, j] > dis[mx, my] + 1)
//                                {
//                                    if(i==gx && j == gy) { }
//                                    renew[i, j] = true;
//                                    dis[i, j] = dis[mx, my] + 1;
//                                    change = true;
//                                }
//                            }
//                        }
//                    }
//                }
//                if (change) warp++;
//                else 
//                {
//                    warp = -1;
//                    break;
//                };
//            }

//            Console.WriteLine(warp);
//        }
//        public static int[] dx = { 0, 1, 0, -1 };
//        public static int[] dy = { 1, 0, -1, 0 };
//        public struct Pair
//        {
//            public int x, y;
//            public Pair(int x, int y)
//            {
//                this.x = x;
//                this.y = y;
//            }
//        }
//    }
//}

//for (int x = -2; x <= 2; x++)
//{
//    for (int y = -2; y <= 2; y++)
//    {
//        int mx = nx + x;
//        int my = ny + y;
//        if (mx < 0 || h <= mx || my < 0 || w <= my || S[mx, my] == '#' || dis[mx, my] != INF) continue;
//        if (dis[ny, ny] > dis[mx, my] + 1)
//        {
//            dis[nx, ny] = dis[mx, my] + 1;
//            warp++;
//        }
//    }
//}

//while (que.Count > 0)
//{
//    var p = que.Dequeue();
//    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 (nx < 0 || h <= nx || ny < 0 || w <= ny || S[nx, ny] == '#' || dis[nx, ny] != INF) continue;
//        que.Enqueue(new Pair(nx, ny));
//        dis[nx, ny] = dis[p.x, p.y] + 1;
//    }
//}