while true
  w, h = gets.split.map(&:to_i)
  break if w == 0
  xa = {}; ya = {}
  xa[0] = 0; xa[w] = 0
  ya[0] = 0; ya[h] = 0
  points = []
  gets.to_i.times do
    p = gets.split.map(&:to_i)
    points << p
    xa[p[0]] ||= 0; xa[p[2]] ||= 0
    ya[p[1]] ||= 0; ya[p[3]] ||= 0
  end
  tmp_x = xa.keys.sort
  xa.keys.each{ |k| xa[k] = tmp_x.index(k) + 1 }
  tmp_y = ya.keys.sort
  ya.keys.each{ |k| ya[k] = tmp_y.index(k) + 1 }
  xs = xa.size; ys = ya.size
  fld = Array.new(ys+1){ Array.new(xs+1, true) }
  fld[0].fill(false); fld[ys].fill(false)
  fld.map{ |arr| arr[0] = arr[xs] = false }
  for p in points
    x1, y1, x2, y2 = p
    for y in (ya[y1]...ya[y2])
      for x in (xa[x1]...xa[x2])
        fld[y][x] = false
      end
    end
  end
  ret = 0
  for y in (1...ys)
    for x in (1...xs)
      next unless fld[y][x]
      arr = []
      arr << [y, x]
      until arr.empty?
        ny, nx = arr.shift
        [[-1,0],[0,-1],[1,0],[0,1]].each do |dy, dx|
          px = nx+dx; py = ny+dy
          if fld[py][px]
            arr << [py, px] 
            fld[py][px] = false
          end
        end
      end
      ret += 1
    end
  end
  p ret
end