while true
  w,h = gets.chomp.split.map(&:to_i)

  break if w.zero? && h.zero?

  c = []
  h.times do |i|
    c[i] = gets.chomp.split.map(&:to_i)
  end

  reached = Array.new(h) { Array.new(w, false) }

  ans = 0
  h.times do |hh|
    w.times do |ww|
      next if c[hh][ww] == 0 || reached[hh][ww]
      ans += 1
      stack = [[hh,ww]]
      while !stack.empty?
        hhh,www = stack.pop
        reached[hhh][www] = true
        [-1,0,1].each do |hi|
          next if hi+hhh < 0
          next if hi+hhh > h-1
          [-1,0,1].each do |wi|
            next if wi+www < 0
            next if wi+www > w-1
            next if c[hi+hhh][wi+www] == 0 || reached[hi+hhh][wi+www]
            stack.push([hi+hhh,wi+www])
          end
        end
      end
    end
  end
  p ans
end

