ans = []

@di = [-1, -1, -1, 0, 0, 1, 1, 1]
@dj = [-1, 0, 1, -1, 1, -1, 0, 1]

def dfs(i, j)
  @field[i][j] = 0

  8.times do |dir|
    ni = i + @di[dir]
    nj = j + @dj[dir]

    next if ni.negative? || nj.negative?
    next if ni >= @h || nj >= @w
    next if @field[ni][nj].zero?

    dfs(ni, nj)
  end
end

loop do
  @w, @h = gets.split.map(&:to_i)
  break if @w.zero? && @h.zero?

  @field = @h.times.map { gets.split.map(&:to_i) }
  count = 0

  @h.times do |i|
    @w.times do |j|
      next if @field[i][j].zero?

      dfs(i, j)
      count += 1
    end
  end

  ans << count
end

ans.each do |a|
  p a
end

