Y = [-1,-1,0,1,1,1,0,-1]
X = [0,-1,-1,-1,0,1,1,1]
rounds = []
round_hw = []

ans = []
loop do
  w, h = gets.split.map(&:to_i)
  break if w == 0 && h == 0
  round_hw << [h, w]
  rounds << []
  rounds[-1] << Array.new(w+2, 0)
  h.times do
    rounds[-1] << [0] + gets.split.map(&:to_i) + [0]
  end
  rounds[-1] << Array.new(w+2, 0)
end

# rounds.each do |round|
#   round.each do |area|
#     p area
#   end
# end

rounds.each_with_index do |areas, index|
  check = []
  stack = []
  count = 0 # 陸毎にマーキングする値
  1.upto(round_hw[index][0]) do |y|
    1.upto(round_hw[index][1]) do |x|

      if areas[y][x] == 1
        stack.push([y, x])
        count += -1
        while !stack.empty?
          now = stack.pop
          areas[now[0]][now[1]] = count # 現在のマスをチェック
          8.times do |k|
            nexy = now[0] + Y[k]
            nexx = now[1] + X[k]
            if areas[nexy][nexx] == 1
              stack.push([nexy, nexx])
            end
          end
        end
        check << count
      end

    end
  end
  ans << check.size
end

ans.each do |an|
  puts an
end
