require 'set'

h,w=gets.chomp.split(" ").map(&:to_i)

matrix=[]

left_top = [0,0]
right_top = [0,w-1]
left_bottom=[h-1,0]
right_bottom=[h-1,w-1]
xy=[left_top,left_bottom,right_top,right_bottom]

(0..h-1).each{|v|
  row=gets.chomp
  offset=0
  loop do
    index=row.index("#",offset)
    break if index.nil?

    offset=index+1
    xy.push([v,index])
  end
}

kyori=1000000
(0..xy.length-2).each{|i|
  x1,y1=xy[i]
  x2,y2=xy[i+1]
  
  k=(y2-y1).abs+(x2-x1).abs
  kyori=k if kyori >k
}

print kyori
