def countup(map, index, w)
    return if index < 0 || map.length <= index || map[index] != 1
    map[index] = 2
    if index % w != 0
        countup(map, index-1, w)
        countup(map, index-w-1, w)
        countup(map, index+w-1, w)
    end
    countup(map, index-w, w)
    countup(map, index+w, w)
    if index % w != w-1
        countup(map, index+1, w)
        countup(map, index-w+1, w)
        countup(map, index+w+1, w)
    end
end

while true do
    w, h = gets.split(' ').map(&:to_i)
    break if w == 0 && h == 0
    break unless h
    map = []
    h.times {
        map.push(gets.split(' ').map(&:to_i))
    }
    map_flat = map.flatten
    
    
    count = 0
    while true do
        break unless map_flat.include?(1)
        countup(map_flat, map_flat.index(1), w)
        map_flat.map{ |i| i == 2 ? 0 : i }
        count += 1
    end
    p count
end