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: Accepted)

using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.IO;
using System.Text;
using N = System.Int32;

static class Program
{
    static public void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var solver = new Solver();
        solver.Solve();
        Console.Out.Flush();
    }
}

public class Solver
{
    N h, w, ch, cw, dh, dw;
    bool[,] moveflag;
    bool[,] warpflag;
    List<N[]> stack = new List<N[]>();
    public void Solve()
    {
        h = rn;
        w = rn;
        ch = rn - 1;
        cw = rn - 1;
        dh = rn - 1;
        dw = rn - 1;//index用に1減算
        string s = "";
        moveflag = new bool[h, w];
        warpflag = new bool[h, w];
        for (int x = 0; x < h; x++) {
            s = rs;
            for (int y = 0; y < w; y++)
            {
                moveflag[x, y] = (s[y] == '#');
                warpflag[x, y] = (s[y] == '#');
            }
        }
        N count = 0;
        stack.Add(new N[] { ch, cw });
        var stackcopy = new List<N[]>();
        while (stack.Count>0)
        {
            stackcopy = new List<N[]>(stack);
            stack = new List<N[]>();
            while (stackcopy.Count > 0)
            {
                HelperMove(stackcopy[0][0], stackcopy[0][1]);
                HelperWarp(stackcopy[0][0], stackcopy[0][1]);
                stackcopy.RemoveAt(0);
            }
            if (moveflag[dh, dw])
            {
                Write(count);
                return;
            }
            count++;
        }
        Write(-1);
    }

    public void HelperMove(N x, N y) //塗り
    {
        moveflag[x, y] = true;
        if (x > 0 && !moveflag[x - 1,y]) { HelperMove(x - 1, y); }
        if (x < h - 1 && !moveflag[x + 1, y]) { HelperMove(x + 1, y); }
        if (y > 0 && !moveflag[x, y - 1]) { HelperMove(x, y - 1); }
        if (y < w - 1 && !moveflag[x, y + 1]) { HelperMove(x, y + 1); }
    }

    public void HelperWarp(N x, N y) //ワープスタック
    {
        if (warpflag[x,y]) return;
        warpflag[x, y] = true;
        for(int xx = -2; xx <= 2; xx++)
        {
            for (int yy = -2; yy <= 2; yy++)
            {
                if (x + xx >= 0 && x + xx < h && y + yy >= 0 && y + yy < w && !moveflag[x + xx, y + yy])
                {
                    stack.Add(new N[] { x + xx, y + yy });
                }
            }
        }
        if (x > 0 && !warpflag[x - 1, y]) { HelperWarp(x - 1, y); }
        if (x < h - 1 && !warpflag[x + 1, y]) { HelperWarp(x + 1, y); }
        if (y > 0 && !warpflag[x, y - 1]) { HelperWarp(x, y - 1); }
        if (y < w - 1 && !warpflag[x, y + 1]) { HelperWarp(x, y + 1); }
    }


    N rn { get { return sc.Integer(); } }
    double rd { get { return sc.Double(); } }
    string rs { get { return sc.Scan(); } }
    public StreamScanner sc = new StreamScanner(Console.OpenStandardInput());

    void Write<T>(T t) { Console.WriteLine(t.ToString()); }
    void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); }
    void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); }
    void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); }
}



public class StreamScanner
{
    public StreamScanner(Stream stream) { str = stream; }
    private readonly Stream str;
    private readonly byte[] buf = new byte[1024];
    private int len, ptr;
    public bool isEof = false;
    public bool IsEndOfStream { get { return isEof; } }
    private byte read()
    {
        if (isEof) throw new EndOfStreamException();
        if (ptr >= len)
        {
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) <= 0)
            {
                isEof = true;
                return 0;
            }
        }
        return buf[ptr++];
    }
    public char Char()
    {
        byte b = 0;
        do b = read();
        while (b < 33 || 126 < b);
        return (char)b;
    }
    public string Scan()
    {
        var sb = new StringBuilder();
        for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
            sb.Append(b);
        return sb.ToString();
    }
    public N Integer()
    {
        N ret = 0; byte b = 0; var ng = false;
        do b = read();
        while (b != '-' && (b < '0' || '9' < b));
        if (b == '-') { ng = true; b = read(); }
        for (; true; b = read())
        {
            if (b < '0' || '9' < b)
                return ng ? -ret : ret;
            else ret = ret * 10 + b - '0';
        }
    }
    public double Double() { return double.Parse(Scan()); }
}