n,m=gets.split.map &:to_i a=(1..m).map{gets} (0...m).any?{|x|(0...n).any?{|y|a[x][y]>?-&&(q=[x,y] (x=q.shift;y=q.shift;[[x+1,y],[x,y+1],[x-1,y],[x,y-1]].map{|z,w|a[z][w]>?-&&(a[z][w]=?!;q+=[z,w])})while[]!=q;0)}} c=99 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