n,m=gets.split.map &:to_i
a=(1..m).map{gets}
c=40
(0...m).any?{|x|(0...n).any?{|y|a[x][y]>?-&&(a[x][y]=?!
c.times{m.times{|x|n.times{|y|m.times{|z|n.times{|w|(x-z).abs+(y-w).abs<2&&a[x][y]<?"&&a[z][w]>?-&&(a[z][w]=?!)}}}}})}}
m.times{|x|n.times{|y|m.times{|z|n.times{|w|a[x][y]+a[z][w]==".!"&&c=[c,(x-z).abs+(y-w).abs-1].min}}}}
p c