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.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using E = System.Linq.Enumerable;
using System.IO.Pipes;
using System.Threading;

class Program
{
    static char[] abc = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
    static long mod = 1000000007;
    static long H;
    static long W;
    static long SY;
    static long GY;
    static long SX;
    static long GX;
    static long ans;
    static String[] map;
    static int[][] used;
    static void Main(string[] args)
    {
        //var S = Console.ReadLine();
        //var N = long.Parse(Console.ReadLine());
        //var N = long.Parse(Console.ReadLine());
        var HW = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        H = HW[0];
        W = HW[1];
        var C = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        var D = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();

        SX = C[0];
        SY = C[1];

        GX = D[0];
        GY = D[1];
        map = new string[H];
        used = new int[H][];

        //for (int i = 0; i < H; i++)
        //{
        //    for (int j = 0; j < W; j++)
        //    {
        //        used[i][j] = int.MaxValue;
        //    }
        //}

        for (int i = 0; i < H; i++)
        {
            used[i] = new int[W];
            map[i] = Console.ReadLine();
        }
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                used[i][j] = int.MaxValue;
            }
        }
        var que = new Queue<Tuple<long, long>>();
        que.Enqueue(new Tuple<long, long>(SX - 1, SY - 1));
        used[SX - 1][ SY - 1] = 1;
        WFS(que);

        Console.WriteLine(used[GX -1][GY-1] == int.MaxValue ? -1 : used[GX - 1][GY - 1] -1);
        
    }

    static void WFS(Queue<Tuple<long, long>> next,int count = 1)
    {
        
        var que = new Queue<Tuple<long, long>>(next);
        var que2 = new Queue<Tuple<long, long>>(next);

        while (que.Count() > 0)
        {
            var top = que.Dequeue();

            if(top.Item1 + 1 < H && used[top.Item1 + 1][top.Item2] > count && map[top.Item1 + 1][(int)top.Item2] == '.')
            {
                que.Enqueue(new Tuple<long, long>(top.Item1 + 1, top.Item2));
                que2.Enqueue(new Tuple<long, long>(top.Item1 + 1, top.Item2));
                used[top.Item1 + 1][top.Item2] = count;
            }

            if (top.Item2 + 1 < W && used[top.Item1][top.Item2 + 1] > count && map[top.Item1][(int)top.Item2 + 1] == '.')
            {
                que.Enqueue(new Tuple<long, long>(top.Item1, top.Item2 + 1));
                que2.Enqueue(new Tuple<long, long>(top.Item1, top.Item2 + 1));
                used[top.Item1][top.Item2 + 1] = count;
            }

            if (top.Item1 - 1 >= 0 && used[top.Item1 - 1][top.Item2] > count && map[top.Item1 - 1][(int)top.Item2] == '.')
            {
                que.Enqueue(new Tuple<long, long>(top.Item1 - 1, top.Item2));
                que2.Enqueue(new Tuple<long, long>(top.Item1 - 1, top.Item2));
                used[top.Item1 - 1][top.Item2] = count;
            }

            if (top.Item2 - 1 >= 0 && used[top.Item1][top.Item2 - 1] > count && map[top.Item1][(int)top.Item2 - 1] == '.')
            {
                que.Enqueue(new Tuple<long, long>(top.Item1, top.Item2 - 1));
                que2.Enqueue(new Tuple<long, long>(top.Item1, top.Item2 - 1));
                used[top.Item1][top.Item2 - 1] = count;
            }
        }

        var next2 = new Queue<Tuple<long, long>>(que2);
        while (que2.Count() > 0)
        {
            var top = que2.Dequeue();

            for (int i = -2; i <= 2; i++)
            {
                if (IsOk(top.Item1 + 2, top.Item2 + i) && used[top.Item1 + 2][top.Item2 + i] > count + 1 && map[top.Item1 + 2][(int)top.Item2 + i] == '.')
                {
                    used[top.Item1 + 2][top.Item2 + i] = count + 1;
                    next2.Enqueue(new Tuple<long, long>(top.Item1 + 2, top.Item2 + i));
                }
            }

            for (int i = -2; i <= 2; i++)
            {
                if (IsOk(top.Item1 - 2, top.Item2 + i) && used[top.Item1 - 2][top.Item2 + i] > count + 1 && map[top.Item1 - 2][(int)top.Item2 + i] == '.')
                {
                    used[top.Item1 - 2][top.Item2 + i] = count + 1;
                    next2.Enqueue(new Tuple<long, long>(top.Item1 - 2, top.Item2 + i));
                }
            }

            for (int i = -2; i <= 2; i++)
            {
                if (IsOk(top.Item1 + i, top.Item2 - 2) && used[top.Item1 + i][top.Item2 - 2] > count + 1 && map[top.Item1 + i][(int)top.Item2 - 2] == '.')
                {
                    used[top.Item1 + i][top.Item2 - 2] = count + 1;
                    next2.Enqueue(new Tuple<long, long>(top.Item1 + i, top.Item2 - 2));
                }
            }

            for (int i = -2; i <= 2; i++)
            {
                if (IsOk(top.Item1 + i, top.Item2 + 2) && used[top.Item1 + i][top.Item2 + 2] > count + 1 && map[top.Item1 + i][(int)top.Item2 + 2] == '.')
                {
                    used[top.Item1 + i][top.Item2 + 2] = count + 1;
                    next2.Enqueue(new Tuple<long, long>(top.Item1 + i, top.Item2 + 2));
                }
            }
        }
        if (next2.Count() > 0) WFS(next2, count + 1);

    }


    static bool IsOk(long p, long q)
    {
        if (p >= H || q >= W)
        {
            return false;
        }

        if (p < 0 || q < 0)
        {
            return false;
        }

        return true;
    }



}


public class PriorityQueue<T> : IEnumerable<T>
{
    private readonly List<T> _data = new List<T>();
    private readonly IComparer<T> _comparer;
    private readonly bool _isDescending;

    public PriorityQueue(IComparer<T> comparer, bool isDescending = true)
    {
        _comparer = comparer;
        _isDescending = isDescending;
    }

    public PriorityQueue(Comparison<T> comparison, bool isDescending = true)
        : this(Comparer<T>.Create(comparison), isDescending)
    {
    }

    public PriorityQueue(bool isDescending = true)
        : this(Comparer<T>.Default, isDescending)
    {
    }

    public void Enqueue(T item)
    {
        _data.Add(item);
        var childIndex = _data.Count - 1;
        while (childIndex > 0)
        {
            var parentIndex = (childIndex - 1) / 2;
            if (Compare(_data[childIndex], _data[parentIndex]) >= 0)
                break;
            Swap(childIndex, parentIndex);
            childIndex = parentIndex;
        }
    }

    public T Dequeue()
    {
        var lastIndex = _data.Count - 1;
        var firstItem = _data[0];
        _data[0] = _data[lastIndex];
        _data.RemoveAt(lastIndex--);
        var parentIndex = 0;
        while (true)
        {
            var childIndex = parentIndex * 2 + 1;
            if (childIndex > lastIndex)
                break;
            var rightChild = childIndex + 1;
            if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0)
                childIndex = rightChild;
            if (Compare(_data[parentIndex], _data[childIndex]) <= 0)
                break;
            Swap(parentIndex, childIndex);
            parentIndex = childIndex;
        }
        return firstItem;
    }

    public T Peek()
    {
        return _data[0];
    }

    private void Swap(int a, int b)
    {
        var tmp = _data[a];
        _data[a] = _data[b];
        _data[b] = tmp;
    }

    private int Compare(T a, T b)
    {
        return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b);
    }

    public int Count => _data.Count;

    public IEnumerator<T> GetEnumerator()
    {
        return _data.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}