import Data.List
import Data.Char

main = interact $ unlines . map (show . how_many_island) . read' . lines
 where
  read' ("0 0":_) = []
  read' (wh:xs) = cs : read' xs'
   where
    [w,h] = map read (words wh)
    (is,xs') = splitAt h xs
    cs = map fst (filter ((=='1') . snd) atlas)
    atlas = zip [(x,y)| x<-[0..h-1], y<-[0..w-1]] (filter isDigit (concat is))

how_many_island [] = 0
how_many_island cs = 1 + how_many_island (explore cs)

explore [] = []
explore (c:cs) = advance [c] [] cs

advance ok ng []
 | any (`neighbor_of` ng) ok = advance ok [] ng
 | otherwise = ng
advance ok ng (c:cs)
 | c `neighbor_of` ok = advance (c:ok) ng cs
 | otherwise = advance ok (c:ng) cs

neighbor_of (x,y) cs = (not.null) (intersect neighbor cs)
 where neighbor = [(x',y') | x'<-[x-1..x+1], y'<-[y-1..y+1], (x',y') /= (x,y)]