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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	finv, inv, fac [MAX]int
)

func COM(n, k int) int {
	MOD := int(MOD)
	if n < k {
		return 0
	}
	if n < 0 || k < 0 {
		return 0
	}
	return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
}

func init() {
	MOD := int(MOD)
	fac[0] = 1
	fac[1] = 1
	finv[0] = 1
	finv[1] = 1
	inv[1] = 1
	for i := 2; i < MAX; i++ {
		fac[i] = fac[i-1] * i % MOD
		inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD
		finv[i] = finv[i-1] * inv[i] % MOD
	}
	//sc.Split(bufio.ScanLines)
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 500100)
}

const (
	MAX = 510000
)

var MOD int64 = 1000000007

func ModAdd(a, b int64) int64 {
	return (a + b) % MOD
}

func ModMul(a, b int64) int64 {
	ans := a * b % MOD
	if ans < 0 {
		ans += MOD
	}
	return ans
}

func ModSub(a, b int64) int64 {
	r := (a%MOD - b%MOD) % MOD
	if r < 0 {
		r += MOD
	}
	return r
}

func ModDiv(a, b int64) int64 {
	return a % MOD * ModInv(b) % MOD
}

func ModPow(a, n int64) int64 {
	var r int64 = 1
	for n > 0 {
		if n&1 == 1 {
			r = r * a % MOD
		}
		a = a * a % MOD
		n >>= 1
	}
	return r
}

func ModInv(a int64) int64 {
	var b, u, v int64 = MOD, 1, 0
	for b > 0 {
		t := a / b
		a -= t * b
		a, b = b, a
		u -= t * v
		u, v = v, u
	}
	u %= MOD
	if u < 0 {
		u += MOD
	}
	return u
}

type Q struct {
	value int
	index int
	K     int
}

type QS []*Q

func (p QS) Len() int           { return len(p) }
func (p QS) Less(i, j int) bool { return p[i].value > p[j].value }
func (pq QS) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *QS) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Q)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *QS) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func GCD(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return GCD(b, a%b)
}

func LCM(a, b int64) int64 {
	return a * b / GCD(a, b)
}

type UnionFind struct {
	par  []int
	size []int
}

func NewUnionFind(size int) *UnionFind {
	return new(UnionFind).init(size)
}

func (uf *UnionFind) init(size int) *UnionFind {
	uf.par = make([]int, size)
	uf.size = make([]int, size)

	for i := 0; i < size; i++ {
		uf.par[i] = i
		uf.size[i] = 1
	}
	return uf
}

func (uf *UnionFind) Unite(p int, q int) {
	rp := uf.Root(p)
	rq := uf.Root(q)
	if rp == rq {
		return
	}
	if uf.size[rp] < uf.size[rq] {
		uf.par[rp] = uf.par[rq]
		uf.size[rq] += uf.size[rp]
	} else {
		uf.par[rq] = uf.par[rp]
		uf.size[rp] += uf.size[rq]
	}
}

func (uf *UnionFind) Root(p int) int {
	for uf.par[p] == p {
		return p
	}
	uf.par[p] = uf.Root(uf.par[p])
	return uf.par[p]
}

func (uf *UnionFind) Size(p int) int {
	return uf.size[uf.Root(p)]
}

func (uf *UnionFind) Same(p int, q int) bool {
	return uf.Root(p) == uf.Root(q)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	n, _ := strconv.Atoi(getStr())
	return n
}

func getStr() string {
	sc.Scan()
	return sc.Text()
}

func factor(result map[int]int, num, pnum int) map[int]int {
	if pnum*pnum > num {
		if num != 1 {
			result[num] += 1
		}
		return result
	}

	if num%pnum == 0 {
		num /= pnum
		result[pnum]++
	} else {
		pnum++
	}
	return factor(result, num, pnum)
}

type P struct {
	x, y, score, times int
}

func main() {
	H, W := getInt(), getInt()
	CH, CW := getInt()-1, getInt()-1
	DH, DW := getInt()-1, getInt()-1
	D := [1010][1010]string{}
	for i := 0; i < H; i++ {
		S := []rune(getStr())
		for j := 0; j < W; j++ {
			D[i][j] = string(S[j])
		}
	}
	filled := map[string]bool{
		fmt.Sprintf("%d/%d", CH, CW): true,
	}
	queue := []*P{
		{
			x: CW, y: CH,
		},
	}
	times := -1
	for len(queue) > 0 {
		q := queue[0]
		//fmt.Println(filled, "POS", q.x, q.y)
		queue = queue[1:]
		if q.x == DW && q.y == DH {
			times = q.times
			break
		}
		for _, p := range []*P{
			{
				x: q.x + 1, y: q.y, score: q.score, times: q.times,
			},
			{
				x: q.x - 1, y: q.y, score: q.score, times: q.times,
			},
			{
				x: q.x, y: q.y + 1, score: q.score, times: q.times,
			},
			{
				x: q.x, y: q.y - 1, score: q.score, times: q.times,
			},
		} {
			if p.x >= W || p.x < 0 {
				continue
			}
			if p.y >= H || p.y < 0 {
				continue
			}
			if filled[fmt.Sprintf("%d/%d", p.x, p.y)] {
				continue
			}
			if D[p.y][p.x] == "." {
				filled[fmt.Sprintf("%d/%d", p.x, p.y)] = true
				queue = append(queue, &P{
					x: p.x, y: p.y, score: p.score + 1, times: p.times,
				})
			} else {
				//fmt.Println("CUR", p.x, p.y)
				for _, p := range []*P{
					{
						x: q.x + 2, y: q.y - 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 2, y: q.y - 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 2, y: q.y, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 2, y: q.y + 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 2, y: q.y + 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 1, y: q.y - 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 1, y: q.y - 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 1, y: q.y, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 1, y: q.y + 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x + 1, y: q.y + 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x, y: q.y - 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x, y: q.y - 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x, y: q.y, score: p.score, times: p.times + 1,
					},
					{
						x: q.x, y: q.y + 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x, y: q.y + 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 1, y: q.y - 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 1, y: q.y - 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 1, y: q.y, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 1, y: q.y + 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 1, y: q.y + 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 2, y: q.y - 2, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 2, y: q.y - 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 2, y: q.y, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 2, y: q.y + 1, score: p.score, times: p.times + 1,
					},
					{
						x: q.x - 2, y: q.y + 2, score: p.score, times: p.times + 1,
					},
				} {
					if p.x >= W || p.x < 0 {
						continue
					}
					if p.y >= H || p.y < 0 {
						continue
					}
					if filled[fmt.Sprintf("%d/%d", p.x, p.y)] {
						continue
					}
					//fmt.Println(p.x, p.y)
					if D[p.y][p.x] == "." {
						filled[fmt.Sprintf("%d/%d", p.x, p.y)] = true
						queue = append(queue, &P{
							x: p.x, y: p.y, score: p.score + 1, times: p.times,
						})
					}
				}
			}
		}

	}
	fmt.Println(times)
}

type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}