def get_parent x
    @parent[x] = get_parent(@parent[x]) if @parent[x] != x
    @parent[x]
end

while true
    w, h = gets.chomp.split.map(&:to_i)
    break if [w, h] == [0, 0]
    map = []
    h.times{map << gets.chomp.split.map(&:to_i)}
    @parent = (0...(w * h)).to_a
    for y in 0...h
        for x in 0...w
            if map[y][x] == 1
                [[y - 1, x - 1], [y - 1, x], [y - 1, x + 1], [y, x - 1]].each{|y2, x2|
                    next if y2 < 0 || x2 < 0 || x2 >= w
                    next if map[y2][x2] == 0
                    p1 = get_parent(y * w + x)
                    p2 = get_parent(y2 * w + x2)
                    min, max = [p1, p2].minmax
                    @parent[max] = min
                }
            else
                @parent[y * w + x] = nil
            end
        end
    end
    count = 0
    @parent.each_with_index{|x, i| count += 1 if x == i}
    puts count
end

