$island_num = 0
 
def check(x, y)
    return false if x<0 || y<0 || $w<=x || $h<=y || $chart[y][x] == 0
     
    if $history[y][x]
        return false
    else
        $history[y][x] = true
    end
 
    # y → x　の順に push
    $next.push(y)
    $next.push(x)
end
 
loop do
    $w, $h = gets.split.map(&:to_i)
     
    # EOF
    exit if $w == 0 && $h == 0
     
    # 島の個数の初期化
    $island_num = 0
 
    # 海図の作成
    $chart = []
    $h.times do
        $chart << gets.split.map(&:to_i)
    end
     
    # 探索済み地域
    $history = Array.new($h).map{Array.new($w, false)}
 
    $h.times do |hh|
        $w.times do |ww|
            next if $chart[hh][ww] == 0 || $history[hh][ww] == true
         
            $list = [hh, ww]
            $next = []
     
            while (! $list.empty?) do
                while (! $list.empty?) do
                    xx = $list.pop
                    yy = $list.pop
                     
                    check(xx-1, yy-1)
                    check(xx, yy-1)
                    check(xx+1, yy-1)
                    check(xx-1, yy)
                    check(xx+1, yy)
                    check(xx-1, yy+1)
                    check(xx, yy+1)
                    check(xx+1, yy+1)
                end
                 
                $list = [*$next]
                $next.clear
            end
             
            $island_num += 1
        end
    end
     
    puts $island_num
end
