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.Data;
using System.Linq;

namespace AtCoderContest
{
    class Program
    {
        static void Main()
        {
            var hw = Console.ReadLine().Split(" ").Select(int.Parse).ToList();
            var (h, w) = (hw[0], hw[1]);

            var cPair = Console.ReadLine().Split(" ").Select(int.Parse).ToList();
            var startCell = (cPair[0], cPair[1]);

            var dPair = Console.ReadLine().Split(" ").Select(int.Parse).ToList();
            var destCell = (dPair[0], dPair[1]);

            var fieldWithCheckedFlag = new Dictionary<(int h, int w), (bool isRoad, bool isChecked)>();
            for (var i = 1; i <= h; i++)
            {
                var line = Console.ReadLine();
                for (var j = 1; j <= w; j++)
                {
                    fieldWithCheckedFlag[(i, j)] = (line[j - 1] == '.', false);
                }
            }

            // devide closed space
            var closedSpaces = new List<HashSet<(int, int)>>();
            for (var hInd = 1; hInd <= h; hInd++)
            {
                for (var wInd = 1; wInd <= w; wInd++)
                {
                    var closedSpace = GetClosedSpaceAndFieldCheck((hInd, wInd), fieldWithCheckedFlag);
                    closedSpaces.Add(closedSpace);

                    HashSet<(int, int)> GetClosedSpaceAndFieldCheck((int h, int w) cell, Dictionary<(int h, int w), (bool isRoad, bool isChecked)> fieldWithCheckedFlag)
                    {
                        var closedSpace = new HashSet<(int, int)>();

                        if (cell.h <= 0 || cell.h > h || cell.w <= 0 || cell.w > w)
                        {
                            return closedSpace;
                        }

                        var fieldCell = fieldWithCheckedFlag[cell];
                        if (fieldCell.isChecked || !fieldCell.isRoad)
                        {
                            return closedSpace;
                        }

                        closedSpace.Add(cell);
                        fieldWithCheckedFlag[cell] = (true, true);

                        closedSpace.UnionWith(GetClosedSpaceAndFieldCheck((cell.h - 1, cell.w), fieldWithCheckedFlag));
                        closedSpace.UnionWith(GetClosedSpaceAndFieldCheck((cell.h + 1, cell.w), fieldWithCheckedFlag));
                        closedSpace.UnionWith(GetClosedSpaceAndFieldCheck((cell.h, cell.w - 1), fieldWithCheckedFlag));
                        closedSpace.UnionWith(GetClosedSpaceAndFieldCheck((cell.h, cell.w + 1), fieldWithCheckedFlag));

                        return closedSpace;
                    }
                }
            }
            closedSpaces.RemoveAll(c => c.Count == 0);

            return;

            var startSpace = closedSpaces.First(s => s.Contains(startCell));
            var destSpace = closedSpaces.First(s => s.Contains(destCell));

            var answer = Solve(closedSpaces, startSpace, destSpace);
            Console.WriteLine(answer);
        }

        public static int Solve(List<HashSet<(int, int)>> remainSpaces, HashSet<(int, int)> startSpace, HashSet<(int, int)> destSpace)
        {
            if (startSpace == destSpace)
            {
                return 0;
            }

            var exceptStartSpace = new List<HashSet<(int, int)>>(remainSpaces);
            exceptStartSpace.Remove(startSpace);

            var warpableSpaces = exceptStartSpace.FindAll(s => CanWarp(startSpace, s));
            if (warpableSpaces.Count == 0)
            {
                return -1;
            }
            else
            {
                var scores = warpableSpaces.Select(adjacentSpace => Solve(exceptStartSpace, adjacentSpace, destSpace));
                if (scores.Max() == -1)
                {
                    return -1;
                }
                else
                {
                    return scores.Min() + 1;
                }
            }
        }

        public static bool CanWarp(HashSet<(int, int)> one, HashSet<(int, int)> other)
        {
            return one.Any(cell =>
            {
                var (h, w) = cell;
                for (var i = -2; i <= 2; i++)
                {
                    for (var j = -2; j <= 2; j++)
                    {
                        if (other.Contains((h + i, w + j)))
                        {
                            return true;
                        }
                    }
                }

                return false;
            });
        }
    }
}