main = interact $ unlines . map (show . red_black) . read' . lines
 where
  read' ("0 0":_) = []
  read' (wh:xs) = ((w,h), filter is_nonred (zip [0.. ] (concat tss))) : read' xs'
   where
    [w,h] = map read (words wh)
    (tss,xs') = splitAt h xs
    is_nonred (_,c) = c /= '#'

red_black ((w,h),ts) = length (filter is_scanned (until_fix scan ts))
 where
  is_scanned (_,c) = c == '@'
  is_nonreach (_,c) = c == '.'
  replace (i,'.') = (i,'@')
  scan ts' = map paint ts'
   where
    paint t = if is_nonreach t && is_adjacent t then replace t else t
    is_adjacent (i,_) = (not.null) (filter is_scanned neighbors)
     where
      neighbors = filter (\(i',_) -> elem i' [i-1,i+1,i-w,i+w] && is_inborder i') ts'
      is_inborder i' = div i' w == div i w || abs (i' - i) == w

until_fix f x = if f x == x then x else until_fix f (f x)