DY = [-1, 0, 1, 0, -1, -1, 1, 1]
DX = [0, 1, 0, -1, -1, 1, 1, -1]

def lake_count(y, x, map, checked)
  return if checked[y][x]
  checked[y][x] = true

  8.times do |i|
    ny = y + DY[i]
    nx = x + DX[i]

    next if ny < 0 || nx < 0 || $h <= ny || $w <= nx
    next if map[ny][nx] == 0
    lake_count(ny, nx, map, checked)
  end
end

loop do
  $w, $h = gets.chomp.split.map(&:to_i)
  break if $w == 0 && $h == 0
  map = []

  $h.times do
    map << gets.chomp.split.map(&:to_i)
  end

  ans = 0
  checked = Array.new($h) { Array.new($w, false) }

  $h.times do |y|
    $w.times do |x|
      next if map[y][x] == 0 || checked[y][x]
      ans += 1
      lake_count(y, x, map, checked)
    end
  end

  puts ans
end

