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: Time Limit Exceeded)

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

class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(" ");
        var H = int.Parse(input[0]);
        var W = int.Parse(input[1]);

        var input2 = Console.ReadLine().Split(" ");
        var Ch = int.Parse(input2[0]);
        var Cw = int.Parse(input2[1]);

        var input3 = Console.ReadLine().Split(" ");
        var Dh = int.Parse(input3[0]);
        var Dw = int.Parse(input3[1]);

        // Mapの外に壁を作る
        var dist = new List<List<int>>();
        dist.Add(new List<int>());
        for (int i = 0; i < W + 2; i++)
        {
            dist[0].Add(-2);
        }

        for (int i = 1; i <= H; i++)
        {
            dist.Add(new List<int>());
            dist[i].Add(-2);
            var line = Console.ReadLine();
            foreach (var item in line)
            {
                if (item == '#') dist[i].Add(-2);
                else dist[i].Add(-1);
            }
            dist[i].Add(-2);
        }

        // Mapの外に壁を作る
        dist.Add(new List<int>());
        for (int i = 0; i < W + 2; i++)
        {
            dist[H + 1].Add(-2);
        }

        // 同一エリアを幅優先探索するためのキュー
        var bfsQueue = new Queue<(int, int)>();
        dist[Ch][Cw] = 0;
        
        // 未探索のエリアを格納するキュー
        var areaQuere = new Queue<(int, int)>();
        areaQuere.Enqueue((Ch, Cw));
        var magicCount = 0;

        while (areaQuere.Count() != 0)
        {
            bfsQueue.Enqueue(areaQuere.Dequeue());
            while (bfsQueue.Count() != 0)
            {
                var v = bfsQueue.Dequeue();

                var up = (v.Item1 - 1, v.Item2);
                var down = (v.Item1 + 1, v.Item2);
                var left = (v.Item1, v.Item2 - 1);
                var right = (v.Item1, v.Item2 + 1);

                if (dist[up.Item1][up.Item2] == -1)
                {
                    dist[up.Item1][up.Item2] = magicCount;
                    bfsQueue.Enqueue(up);
                }

                if (dist[down.Item1][down.Item2] == -1)
                {
                    dist[down.Item1][down.Item2] = magicCount;
                    bfsQueue.Enqueue(down);
                }

                if (dist[left.Item1][left.Item2] == -1)
                {
                    dist[left.Item1][left.Item2] = magicCount;
                    bfsQueue.Enqueue(left);
                }

                if (dist[right.Item1][right.Item2] == -1)
                {
                    dist[right.Item1][right.Item2] = magicCount;
                    bfsQueue.Enqueue(right);
                }
            }

            // 探索済みのエリアから5x5の範囲で未探索のエリアがないかチェック
            for (int i = 1; i <= H; i++)
            {
                for (int j = 1; j <= W; j++)
                {
                    if (dist[i][j] == magicCount)
                    {
                        Check5x5(dist, (i, j), magicCount);
                    }
                }
            }

            // キューに未探索のエリアを追加
            for (int i = 1; i <= H; i++)
            {
                for (int j = 1; j <= W; j++)
                {
                    if (dist[i][j] == magicCount + 1)
                    {
                        areaQuere.Enqueue((i, j));
                    }
                }
            }
            magicCount++;
        }

        Console.WriteLine(dist[Dh][Dw]);
    }

    // 指定位置を中心に5x5の範囲で未到達のエリアがあればマーキングする
    private static void Check5x5(List<List<int>> dist, (int, int) position, int magicCount)
    {
        var W = dist[0].Count;
        var H = dist.Count;
        for (int i = -2; i <= 2; i++)
        {
            for (int j = -2; j <= 2; j++)
            {
                var pointX = position.Item2 + i;
                var pointY = position.Item1 + j;

                if (pointX >= 0 && pointY >= 0 && pointX < W && pointY < H)
                {
                    if (dist[pointY][pointX] == -1)
                    {
                        dist[pointY][pointX] = magicCount + 1;
                    }
                }
            }
        }
    }
}