class MapChecker
    def initialize(width, height)
        @checker = Array.new(height) do
            Array.new(width) do false end
        end
    end
    
    def check(position)
        @checker[position[:y]][position[:x]] = true
    end
    
    def checked?(position)
        @checker[position[:y]][position[:x]]
    end
end

class Field
    def initialize(map, width, height)
        @map = map
        @width = width
        @height = height
        @map_checker = MapChecker.new(width, height)
    end
    
    def next_lands(position)
        width = @map[0].length
        height = @map.length
        lands = []
        [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]].each do |dif|
            new_x = position[:x] + dif[0]
            new_y = position[:y] + dif[1]
            new_position = {x: new_x, y: new_y}
            next if !land?(new_x, new_y)
            next if @map_checker.checked?(new_position)
            @map_checker.check(new_position)
            lands.push(new_position)
        end
        lands
    end
    
    def start_point
        point = nil
        @height.times do |y|
            @width.times do |x|
                if @map[y][x] == 1 && !@map_checker.checked?({x: x, y: y})
                    point = {x: x, y: y}
                    break
                end
            end
            break if !point.nil?
        end
        return nil if point.nil?
        @map_checker.check(point)
        point
    end
    
    private
    
    def land?(x, y)
        return false if x >= @width || y >= @height
        return false if x < 0 || y < 0
        return false if @map[y][x] == 0
        true
    end
end


while true
    width, height = gets.split(' ').map(&:to_i)
    break if width == 0 && height == 0
    map = []
    height.times do
        row = gets.split(' ').map(&:to_i)
        map.push(row)
    end
    
    field = Field.new(map, width, height)
    lands = 0
    while true
        position_queue = []
        start = field.start_point
        break if start.nil?
        position_queue.push(start)
        lands += 1
        while true
            new_positions = position_queue.map do |position|
                field.next_lands(position)
            end.flatten
            break if new_positions.length == 0
            position_queue = new_positions
        end
    end
    puts lands
end