DXYS = [[0, 1], [0, -1], [-1, 0], [1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1]]

def initial_position(tiles)
  tiles.size.times do |j|
    tiles[0].size.times do |i|
      return [j, i] if tiles[j][i] == 1
    end
  end
  return []

end

def dfs(tiles, j, i)
  return if j<0 || tiles.size <= j
  return if i<0 || tiles[0].size <= i
  return if tiles[j][i] != 1

  tiles[j][i] = 2

  DXYS.map do |dx,dy|
    dfs(tiles, j+dy, i+dx)
  end
end

loop do
  width, height = gets.split.map(&:to_i)
  break if width == 0 && height == 0

  #?????????
  tiles = []
  height.times {tiles << gets.split.map(&:to_i)}
  island_count = 0

  while (position=initial_position(tiles)) != []
    island_count += 1
    dfs(tiles, position[0], position[1])
  end

  p island_count
end