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;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Tasks
{
    public class D
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (H, W) = Scanner.Scan<int, int>();
            var (CH, CW) = Scanner.Scan<int, int>();
            CH--; CW--;
            var (DH, DW) = Scanner.Scan<int, int>();
            DH--; DW--;
            var S = new string[H];
            var used = new bool[H][].Select(x => new bool[W]).ToArray();
            used[CH][CW] = true;
            for (var i = 0; i < H; i++)
            {
                S[i] = Console.ReadLine();
                for (var j = 0; j < S[i].Length; j++)
                {
                    if (S[i][j] == '#') used[i][j] = true;
                }
            }
            if (S[DH][DW] == '#')
            {
                Console.WriteLine(-1);
                return;
            }

            var inf = (int)1e9;
            var dx = new int[] { -1, 0, 1, 0 };
            var dy = new int[] { 0, 1, 0, -1 };
            var costs = new int[H][].Select(x => Enumerable.Repeat(inf, W).ToArray()).ToArray();
            costs[CH][CW] = 0;
            void Dfs(int h, int w, int num)
            {
                if (costs[h][w] < num) return;
                costs[h][w] = num;
                // Console.WriteLine($"{h} {w} {num}");
                if (h == DH && w == DW) return;

                var none = true;
                for (var i = 0; i < 4; i++)
                {
                    var x = w + dx[i];
                    var y = h + dy[i];
                    if (x < 0 || x >= W) continue;
                    if (y < 0 || y >= H) continue;
                    if (!used[y][x])
                    {
                        none = false;
                        used[y][x] = true;
                        Dfs(y, x, costs[h][w]);
                        used[y][x] = false;
                    }
                }

                if (none)
                {
                    for (var i = -2; i <= 2; i++)
                    {
                        for (var j = -2; j <= 2; j++)
                        {
                            if (i == 0 && j == 0) continue;
                            var x = w + j;
                            var y = h + i;
                            if (x < 0 || x >= W) continue;
                            if (y < 0 || y >= H) continue;
                            if (!used[y][x])
                            {
                                none = false;
                                used[y][x] = true;
                                Dfs(y, x, costs[h][w] + 1);
                                // used[y][x] = false;
                            }
                        }
                    }
                }
            }

            Dfs(CH, CW, 0);
            if (costs[DH][DW] == inf) costs[DH][DW] = -1;

            Console.WriteLine(costs[DH][DW]);

        }

        public static class Scanner
        {
            private static Queue<string> queue = new Queue<string>();
            public static T Next<T>()
            {
                if (!queue.Any()) foreach (var item in Console.ReadLine().Trim().Split(" ")) queue.Enqueue(item);
                return (T)Convert.ChangeType(queue.Dequeue(), typeof(T));
            }
            public static T Scan<T>() => Next<T>();
            public static (T1, T2) Scan<T1, T2>() => (Next<T1>(), Next<T2>());
            public static (T1, T2, T3) Scan<T1, T2, T3>() => (Next<T1>(), Next<T2>(), Next<T3>());
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>());
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>(), Next<T5>());
        }
    }
}