while(n,m=gets.split.map &:to_i)[0]>0
a=(b=[?.*(n+2)])+(1..m).map{?.+gets.tr(?\n,?.)}+b
q=[[c=0,0]]
r=[]
(q[0]||(q=r;r=[];c+=1)
x,y=q.shift
[[x,y+1],[x,y-1],[x+1,y],[x-1,y]].map{|i,j|d=a[i][j];d==?.?(a[i][j]=?a;q<<[i,j]):d==?#?(r<<[i,j]):d==?&?(q=r=[];break):0})while q[0]||r[0]
p c
end