str1 = gets.split
H,W = str1[0].to_i, str1[1].to_i

a,b = "0","0"
(0...W).each do
  a << "0"
  b << "1"
end
a << "0"
b << "0"
X,Y = "",""
X << a
Y << a

(0...H).each do
  X << "0"
  X << gets.chomp
  Y << b
  X << "0"
end

X.gsub!('#', '1')
X.gsub!('.', '0')

X << a
Y << a

X1 = X.to_i(2)
Y1 = Y.to_i(2)

i = 0
while X1 != Y1
  X1 = ( X1 | X1 << 1 | X1 << (W+2) | X1 >> 1 | X1 >> (W+2) ) & Y1
  i += 1
end
puts i