H,W,*a = $<.take(3).join.split.map &:to_i
S = (1..H).map{ gets }
a.map!{_1-1}
A = [p] * ((H+1)<<10)
B = (1<<10) - 1
C = A.dup
D = (a[2]<<10) + a[3]
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|
      next  if (r+=h)<0 || H<=r || (c+=w)<0 || W<=c
      a = (r<<10) + c
      (A[a] = n; Q << (r<<10)+c)  if !A[a] && S[r][c]==?.
    }
  }
  while Q[0]
    Q.pop(H*W).each {|q|
      S[h=q>>10][w=q&B] = ?*
      N.each {|r,c|
        next  if (r+=h)<0 || H<=r || (c+=w)<0 || W<=c
        s = S[r][c]
        a = (r<<10) + c
        if s == ?.
          (Q << a; A[a] = n) unless A[a]
        elsif s == ?#
          (R << q; C[q] = n) unless C[q]
        end
      }
    }
  end
  break n if A[D]
  break -1 if !R[0]
}