#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

h,w = inp
c = inp
d = inp
c = [c[0]-1,c[1]-1]
d = [d[0]-1, d[1]-1]
map = na2(h,w,false)
h.times do |i|
  s = gets.chomp
  s.split("").each.with_index do |c ,j|
    map[i][j] = (c == "#")
  end
end
deque = [[c[0],c[1],0]]
mem = na2(h,w,false)
while deque.size > 0
  #p deque.size
  y,x,c = deque.shift

  next if y < 0
  next if x < 0
  next if y >= h
  next if x >= w
  if(y == d[0] and x == d[1])
    puts c
    exit
  end
  next if mem[y][x]
  next if map[y][x]
  mem[y][x] = true
  deque.unshift([y+1,x,c])
  deque.unshift([y-1,x,c])
  deque.unshift([y,x+1,c])
  deque.unshift([y,x-1,c])
  deque.push([y+1,x+1,c+1])
  deque.push([y+1,x+2,c+1])
  deque.push([y+1,x-1,c+1])
  deque.push([y+1,x-2,c+1])
  deque.push([y-1,x+1,c+1])
  deque.push([y-1,x+2,c+1])
  deque.push([y-1,x-1,c+1])
  deque.push([y-1,x-2,c+1])
  deque.push([y+2,x+1,c+1])
deque.push([y+2,x+2,c+1])
deque.push([y+2,x-1,c+1])
deque.push([y+2,x-2,c+1])
deque.push([y-2,x+1,c+1])
deque.push([y-2,x+2,c+1])
deque.push([y-2,x-1,c+1])
deque.push([y-2,x-2,c+1])
end
p -1