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 Ruby (2.7.1) (judge verdict: Time Limit Exceeded)

# coding: utf-8
require 'pp'

###################

#inputType = :singleLine
# inputType = :2Lines
inputType = :nLines
  
def main(query)
  l1 = query.shift
  l2 = query.shift
  l3 = query.shift
  h,w = tois(l1.split(" "))
  ch, cw = tois(l2.split(" "))
  ch = ch - 1
  cw = cw - 1
  dh, dw = tois(l3.split(" "))
  dh = dh - 1
  dw = dw - 1

  smat = query.map { | sl |
    sl.chars.map { | x |
      { :type => x,
        :w => 0,
        :status => :unexpanded
      }
    }
  }

  smat[ch][cw][:status] = :expanding
  

  ### 最小のWを迷路から探す
  c = 0
  
  while true
    ppp "SMAT=", smat

    c = c + 1

#    if (c > 13) then
#      break
    #    end
    
    
    minw = 1000000000000000000
    minch = -1
    mincw = -1
    expanding = false 
    smat.each_index { | ch |
      smat[ch].each_index { | cw |
        if smat[ch][cw][:status] == :expanding && smat[ch][cw][:type] == "." then
          if smat[ch][cw][:w] < minw then
            minw = smat[ch][cw][:w]
            minch = ch
            mincw = cw
            expanding = true
          end
        end
      }
    }

    ppp "minch,mincw=", [minch, mincw]    

    if (! expanding) then
      if (smat[dh][dw][:status] == :unexpanded) then
        return [-1]
      else
        return [smat[dh][dw][:w]]
      end
    end

    if (minw >= smat[dh][dw][:w] && smat[dh][dw][:status] != :unexpanded) then
      ppp "END",[minw,dh,dw,smat[dh][dw]]
      return [smat[dh][dw][:w]]
    end

    pairs = [[minch-1,mincw],
             [minch,mincw-1],
             [minch+1,mincw],
             [minch,mincw+1]]

    pairs = pairs.select { | x , y | x >= 0 && x < h && y >= 0 && y < w}
    
    pairs.each { | cch,ccw |
      if smat[cch][ccw][:type] == "." then
        if smat[cch][ccw][:w] > minw || smat[cch][ccw][:status] == :unexpanded then
          smat[cch][ccw][:w] = minw
          smat[cch][ccw][:status] = :expanding
        end
      end
    }

    wsize = 2
    cchs = (minch-wsize).upto(minch+wsize).select { | x | x >= 0 && x < h}
    ccws = (mincw-wsize).upto(mincw+wsize).select { | x | x >= 0 && x < w}
    
    cchs.each { | cch |
      ccws.each { | ccw |
        if smat[cch][ccw][:type] == "." then
          if smat[cch][ccw][:w] > (minw + 1) || smat[cch][ccw][:status] == :unexpanded then
            smat[cch][ccw][:w] = minw + 1
            smat[cch][ccw][:status] = :expanding
          end
        end
      }
    }
    smat[minch][mincw][:status] = :e
    
    
  end

  return [1,1,1]
end

###################

TEST=false

def tois(l)
  l.map { | x | x.to_i}
end

def ppp(msg, val)
  if (TEST) then
    print msg
    pp val
  end
end

target = nil
if (ARGV.size == 0) then
  f = STDIN
end
if (ARGV.size > 0) then
  f = open(ARGV[0])
end
if (ARGV.size > 1) then
  target = ARGV[1].to_i
end

t = 1
ng = []
while true
  query = []
  if (inputType == :singleLine) then
    a = f.gets
    if ! a then
      exit
    end
    a.chomp!
    query.push a
  elsif (inputType == :l2Lines) then
    a = f.gets
    if ! a then
      exit
    end
    a.chomp!
    query.push a
    b = f.gets.chomp
    query.push b
  elsif (inputType == :l3Lines) then
    a = f.gets
    if ! a then
      exit
    end
    a.chomp!
    query.push a
    b = f.gets.chomp
    query.push b
    c = f.gets.chomp
    query.push c
  elsif (inputType == :nLines) then
    a = f.gets
    if ! a then
      exit
    end
    a.chomp!
    query.push a

    h = a.split(" ")[0].to_i

    b = f.gets.chomp
    query.push b
    c = f.gets.chomp
    query.push c
    
    h.times { | i |
      query.push f.gets.chomp
    }
  end
  
  aa = f.gets
  if ! aa then
    r = main(query) 
    r.each { | x | puts x }
    exit
  elsif ( aa.chomp == "---") then
    oks = []
    while true
      s = f.gets.chomp
      if (s == "===") then
        break
      end
      oks.push s
    end

    if (target == nil || t == target) then
      if target then
        TEST = true
      end

      puts "--- " + t.to_s + " ---"
      r = main(query)    

      if r.join('\n') == oks.join('\n') then
        puts "RESULT:"
        r.each { | x | puts x }
        puts "OK"
      else
        puts "RESULT:NG"
        r.each { | x | puts x }
        puts "NG!!!!!!!!!"
        ng.push t
      end
    end
  end
  t = t + 1
end
puts "---"
puts "NG targes = " + ng.to_s