main = interact $ unlines . map (show . red_black) . read' . lines
 where
  read' ("0 0":_) = []
  read' (wh:xs) = (w, 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,ts) = length (filter is_scanned (until_fix scan ts))
 where
  is_scanned (_,c) = c == '@'
  scan ts' = map paint ts'
   where
    paint (i,c) = if c == '.' && is_adjacent i then (i,'@') else (i,c)
    is_adjacent i = any (\i' -> lookup i' ts' == Just '@') neighbors
     where
      neighbors = filter is_inborder [i-1,i+1,i-w,i+w]
      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)