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 H int
var W int
var Ch int
var Cw int
var Dh int
var Dw int
var S [][]byte
var m [][]int

func main() {
	H = nextInt()
	W = nextInt()
	Ch = nextInt() - 1
	Cw = nextInt() - 1
	Dh = nextInt() - 1
	Dw = nextInt() - 1

	S = make([][]byte, H)
	m = make([][]int, H)
	for i := 0; i < H; i++ {
		S[i] = nextBytes()
		m[i] = make([]int, W)

		for j := 0; j < W; j++ {
			m[i][j] = H * W
		}
	}
	m[Ch][Cw] = 0

	queue := pqNew()
	queue.Push(P{Ch, Cw, 0})

	dir := []P{
		P{-1, -1, 0},
		P{-1, +1, 0},
		P{+1, +1, 0},
		P{+1, -1, 0},
	}

	for !queue.Empty() {
		p := queue.Pop()
		if p.h == Dh && p.w == Dw {
			fmt.Println(p.c)
			return
		}
		nexts := make([]P, 0)
		// A type
		for i := 0; i < len(dir); i++ {
			d := dir[i]
			nexts = append(nexts, P{p.h + d.h, p.w + d.w, p.c})
		}

		// B type
		for h := -2; h <= +2; h++ {
			for w := -2; w <= +2; w++ {
				nexts = append(nexts, P{p.h + h, p.w + w, p.c + 1})
			}
		}

		for i := 0; i < len(nexts); i++ {
			n := nexts[i]
			if out(n.h, n.w) {
				continue
			}
			if m[n.h][n.w] <= n.c {
				continue
			}
			m[n.h][n.w] = n.c
			queue.Push(n)
		}
	}

	fmt.Println(-1)
}

func out(h, w int) bool {
	return h < 0 || w < 0 || h >= H || w >= W || S[h][w] == '#'
}

// PriorityQueue は優先度付きキューを表す
type PriorityQueue struct {
	queue []P
	size  int
}

func pqNew() *PriorityQueue {
	pq := &PriorityQueue{queue: make([]P, 0), size: 0}

	return pq
}
func (pq *PriorityQueue) less(i, j int) bool {
	pi := pq.queue[i]
	pj := pq.queue[j]

	if pi.c != pj.c {
		return pi.c < pj.c
	}
	if pi.h != pj.h {
		return pi.h < pj.h
	}
	return pi.w < pj.w
}

// Push は優先度付きキューに要素を一つ追加します。
func (pq *PriorityQueue) Push(value P) {
	if len(pq.queue) == 0 || len(pq.queue)-1 == pq.size {
		pq.queue = append(pq.queue, value)
	} else {
		pq.queue[pq.size] = value
	}

	i := pq.size
	parent := pq.parent(i)

	for i > 0 && pq.less(parent, i) {
		pq.swap(parent, i)
		i := parent
		parent = pq.parent(i)
	}

	pq.size++
}

// Pop は優先度付きキューから要素を一つ取り出します。
func (pq *PriorityQueue) Pop() P {
	if pq.size == 0 {
		panic("Priority Queue is Empty")
	}
	ret := pq.queue[0]
	pq.size--
	pq.queue[0] = pq.queue[pq.size]

	i := 0
	left := pq.left(i)
	right := pq.right(i)

	for right <= pq.size {
		l := pq.less(i, left)
		r := pq.less(i, right)
		if l && (!r || pq.less(right, left)) {
			pq.swap(i, left)
			i = left
		} else if r {
			pq.swap(i, right)
			i = right
		} else {
			break
		}
		left = pq.left(i)
		right = pq.right(i)
	}

	return ret
}

// Empty は優先度付きキューが空かどうかを判断します。
func (pq *PriorityQueue) Empty() bool {
	return pq.size == 0
}

func (pq *PriorityQueue) swap(i, j int) {
	pq.queue[i], pq.queue[j] = pq.queue[j], pq.queue[i]
}
func (pq *PriorityQueue) parent(i int) int {
	return (i - 1) / 2
}
func (pq *PriorityQueue) left(i int) int {
	return i*2 + 1
}
func (pq *PriorityQueue) right(i int) int {
	return i*2 + 2
}

type P struct {
	h int
	w int
	c int
}

var stdin = initStdin()

func initStdin() *bufio.Scanner {
	bufsize := 1 * 1024 * 1024 // 1 MB
	var stdin = bufio.NewScanner(os.Stdin)
	stdin.Buffer(make([]byte, bufsize), bufsize)
	stdin.Split(bufio.ScanWords)
	return stdin
}

func nextString() string {
	stdin.Scan()
	return stdin.Text()
}

func nextBytes() []byte {
	stdin.Scan()
	return stdin.Bytes()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextString())
	return i
}

func nextInt64() int64 {
	i, _ := strconv.ParseInt(nextString(), 10, 64)
	return i
}

func debug(args ...interface{}) {
	fmt.Fprintln(os.Stderr, args)
}