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