H,W,*a = $<.take(3).join.split.map &:to_i
S = (1..H).map{ gets }
a.map!{_1-1}
B = (1<<10) - 1
D = a[3]
L = S[a[2]]
h = [*-2..2].repeated_permutation(2).group_by{_1.abs+_2.abs}
N = h[1]
P = h[2]+h[3]+h[4]
Q = [(a[0]<<10)+a[1]]
R = []
p (0..).each {|n|
  R.pop(H*W).each {
    h,w = _1>>10, _1&B
    P.each {|r,c|
      (S[r][c]=?*; Q << (r<<10)+c) if 0<=(r+=h) && r<H && 0<=(c+=w) && c<W && S[r][c]==?.
    }
  }
  while Q[0]
    Q.pop(H*W).each {|q|
      h,w,j = q>>10, q&B
      N.each {|r,c|
        next  if (r+=h)<0 || H<=r || (c+=w)<0 || W<=c
        s = S[r][c]
        if s == ?#
          j = 1
        elsif s == ?.
          S[r][c] = ?+
          Q << (r<<10)+c
        end
      }
      R << q  if j
    }
  end
  break n if L[D] != ?.
  break -1 if !R[0]
}