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

while line = gets
  w,h = line.split.map(&:to_i)
  break if w.zero? && h.zero?

  c = Array.new(h){ gets.split.map(&:to_i) }
  seen = Array.new(h){ Array.new(w,false) }

  inside = -> y, x { 0 <= y && y < h && 0 <= x && x < w }

  dfs = -> x,y do
    return if seen[y][x]
    seen[y][x] = true
    8.times do |i|
      nx = DX[i] + x
      ny = DY[i] + y
      next unless inside[ny, nx]
      next if seen[ny][nx]
      next if c[ny][nx].zero?
      dfs[nx,ny]
    end
  end
  
  ans = 0
  h.times do |y|
    w.times do |x|
      next if seen[y][x]
      next if c[y][x].zero?
      ans += 1
      dfs[x,y]
    end
  end
  p ans
end


