# coding: utf-8
require 'pp'
require 'set'

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

#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

  warp = 0

  smat = query.map { | sl |
    sl.chars
  }

  queue = Set.new([[ch, cw]])

  c = 0
  
  while queue.size > 0
#    c = c + 1
#    if (c > 3) then
#         break
#    end

    ppp "Q=",queue
    ppp "mat=", smat

    flag = true
    while queue.size > 0
      queue = queue.to_a
      
      ch,cw = queue.shift

      queue = queue.to_set

      if (smat[ch][cw] == ".") then
        smat[ch][cw] = "*"
        pairs = [[ch-1,cw],
                 [ch,cw-1],
                 [ch+1,cw],
                 [ch,cw+1]]
        pairs = pairs.select { | x , y | x >= 0 && x < h && y >= 0 && y < w}
        queue = queue + pairs

        flag = false
      end
    end
    ppp "mat2=", smat

    if flag then
      return [-1]
    end
    
    if (smat[dh][dw] == "*") then
      return [warp]
    end

    smat.each_index { | ch |
      smat[ch].each_index { | cw |
        if (smat[ch][cw] == "*") then
          queue = queue + Set.new([[ch,cw]])
          smat[ch][cw] == "E"

          wsize = 2
        
          cchs = (ch-wsize).upto(ch+wsize).select { | x | x >= 0 && x < h}
          ccws = (cw-wsize).upto(cw+wsize).select { | x | x >= 0 && x < w}
          
          cchs.each { | h |
            ccws.each { | w |
              queue = queue + Set.new([[h,w]])
            }
          }
        end
      }
    }
    warp = warp + 1
  end
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
