loop do
  w, h = gets.chomp.split(' ').map(&:to_i)
  exit(0) if w == 0 && h == 0
  c = []
  count = 0
  h.times do
    c << gets.chomp.split(' ').map(&:to_i)
  end

  while c.any? { |line| line.include?(1) }
    stack = []

    c.each_with_index do |line, i|
      next unless line.include?(1)
      stack = [[i, line.index(1)]]
      count += 1
      break
    end

    until stack.empty?
      i, j = stack.pop
      next if c[i][j] == 0

      c[i][j] = 0

      stack << [i + 1, j] if i + 1 < h && c[i + 1][j] == 1
      stack << [i - 1, j] if i - 1 >= 0 && c[i - 1][j] == 1
      stack << [i, j + 1] if j + 1 < w && c[i][j + 1] == 1
      stack << [i, j - 1] if j - 1 >= 0 && c[i][j - 1] == 1

      stack << [i + 1, j - 1] if i + 1 < h && j - 1 >= 0 && c[i + 1][j - 1] == 1
      stack << [i - 1, j - 1] if i - 1 >= 0 && j - 1 >= 0 && c[i - 1][j - 1] == 1
      stack << [i + 1, j + 1] if i + 1 < h && j + 1 < w && c[i + 1][j + 1] == 1
      stack << [i - 1, j + 1] if i - 1 >= 0 && j + 1 < w && c[i - 1][j + 1] == 1
    end
  end
  puts count
end
