def solve(s,buf)
  h,w=s.split.map &:to_i
  c=0
  (nq=[0i]).each{|z|
    next if ?-==t=buf[z.imag][z.real]
    c+=1
    (cq=[z]).each{|u|
      next if buf[u.imag][u.real]==?-
      buf[u.imag][u.real]=?-
      [1,1i,-1,-1i].each{|d|
        v=u+d
        next unless (0...w)===v.real&&(0...h)===v.imag
        case buf[v.imag][v.real]
        when ?-; next
        when t; cq.push(v)
        else; nq.push(v)
        end
      }
    }
  }
  c
end
nparam,*buf=""
while s=gets
  if s=~/^\d/
    p solve(nparam,buf) if !buf.empty?
    break if s=~/^0/
    buf.clear
    nparam=s
  else
    buf.push(s)
  end
end