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 Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

module Main where
import qualified Data.ByteString.Char8         as BS
import qualified Data.ByteString.Lazy.Char8    as BSL
import qualified Data.ByteString.Builder       as BSB
import           Data.Char                      ( isSpace )
import           Data.List                      ( unfoldr )
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import qualified Data.Vector.Generic           as VG
import qualified Data.Vector.Generic.Mutable   as VGM
import           Data.Maybe
import           Control.Monad

main :: IO ()
main = do
  [h, w] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  [ch, cw] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  [dh, dw] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  mat <- VU.unfoldrN (h*w) (BS.uncons . BS.dropWhile isSpace) <$> BS.getContents
  let mat' = VU.map toMaze mat
  let from2d = mkFrom2d w h
  let to2d = mkTo2d w h
  let inBound = mkInBound w h
  let start = (cw-1, ch-1)
  let goal = (dw-1, dh-1)
  let dfs maze (i, j) v = do
        VUM.write maze (from2d i j) v
        forM_ (filter (uncurry inBound) [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]) $ \(i', j') -> do
            c <- VUM.read maze (from2d i' j')
            case c of
              999999 -> return ()
              -1 -> dfs maze (i', j') v
              w -> if v >= w then return () else dfs maze (i', j') v
        forM_ (filter (uncurry inBound) [(i+2,j), (i-2,j), (i,j+2), (i,j-2), (i+2,j+2), (i-2,j-2), (i-2,j+2), (i+2,j-2), (i+2,j+1), (i-2,j-1), (i-2,j+1), (i+2,j-1), (i+1,j+2), (i-1,j-2), (i-1,j+2), (i+1,j-2)]) $ \(i', j') -> do
            c <- VUM.read maze (from2d i' j')
            case c of
              999999 -> return ()
              -1 -> dfs maze (i', j') (v+1)
              w -> if v >= w then return () else dfs maze (i', j') (v+1)
  mat'' <- VU.thaw mat'
  ans <- dfs mat'' start 0
  v <- VUM.read mat'' (uncurry from2d goal)
  -- debug <- VU.freeze mat''
  -- print2d w h debug
  print $ case v of
    999999 -> -1
    v -> v

toMaze :: Char -> Int
toMaze '#' = 999999
toMaze '.' = -1


-- usage:
-- do
--   [x, y] <- getSizeAction 
--   let from2d = mkFrom2d x y
--   let to2d = mkTo2d x y
--   let inBound = mkInBound x y
--   let v ! i j = mk2dIndexing x y
--   let v !? i j = mk2dIndexingMaybe x y
mkFrom2d :: Int -> Int -> Int -> Int -> Int
mkFrom2d x y i j = i + x * j

mkTo2d :: Int -> Int -> Int -> (Int, Int)
mkTo2d x y idx = (idx `mod` x, idx `div` x)

mkInBound :: Int -> Int -> Int -> Int -> Bool
mkInBound x y i j = 0 <= i && i < x && 0 <= j && j < y

mk2dIndexing :: VG.Vector v a => Int -> Int -> v a -> Int -> Int -> a
mk2dIndexing x y v i j = v VG.! mkFrom2d x y i j

mk2dIndexingMaybe :: VG.Vector v a => Int -> Int -> v a -> Int -> Int -> Maybe a
mk2dIndexingMaybe x y v i j
  | 0 <= i && i < x && 0 <= j && j < y = Just $ mk2dIndexing x y v i j
  | otherwise                          = Nothing

putYN :: Bool -> IO ()
putYN True  = putStrLn "Yes"
putYN False = putStrLn "No"

print2d :: (Show (v a), Foldable v, VG.Vector v a) => Int -> Int -> v a -> IO ()
print2d x y v = mapM_ print $ chunksOf x v
  where
    chunksOf k = go
      where
        go t = case VG.splitAt k t of
                (a,b) | null a    -> []
                      | otherwise -> a : go b