def dfs x, y
  if !@lines[y] || @lines[y][x] == 0
    # p [x, y, false]
    return
  end
  # p [x, y, true]
  @lines[y][x] = 0

  dfs x + 1, y
  dfs x + 1, y + 1
  dfs x + 1, y - 1
  dfs x, y + 1
  dfs x, y - 1
  dfs x - 1, y
  dfs x - 1, y + 1
  dfs x - 1, y - 1
end

loop do
  w, h = gets.split.map(&:to_i)
  break if w == 0
  @lines = [[0] * (w+2)]
  h.times do
    @lines << [0, *gets.split.map(&:to_i), 0]
  end
  @lines << [0] * (w+2)

  ans = 0
  visited = []
  @lines.each_with_index do |l, y|
    next if y == 0 || y == h + 1
    l.each_with_index do |c, x|
      next if x == 0 || x == w + 1
      if @lines[y][x] == 1
        dfs x, y
        ans += 1
      end
    end
  end

  puts ans
end