## subroutines

def compress(x0, x1, n, w)
  xs = []

  for i in (0...n)
    for d in [-1, 0, 1]
      tx0 = x0[i] + d
      xs << tx0 if tx0 >= 0 && tx0 < w
      tx1 = x1[i] + d
      xs << tx1 if tx1 >= 0 && tx1 < w
    end
  end

  xs.sort!
  xs.uniq!
  w0 = xs.length

  xmap = {}
  for i in (0...w0)
    xmap[xs[i]] = i
  end

  for i in (0...n)
    x0[i] = xmap[x0[i]]
    x1[i] = xmap[x1[i]]
  end

  w0
end

def paint(brd, x0, y0, w, h)
  brd[y0][x0] = 1
  q = [[x0, y0]]

  while ! q.empty?
    x, y = q.shift

    if x > 0 && brd[y][x - 1] == 0
      brd[y][x - 1] = 1
      q << [x - 1, y]
    end
    if x < w - 1 && brd[y][x + 1] == 0
      brd[y][x + 1] = 1
      q << [x + 1, y]
    end
    if y > 0 && brd[y - 1][x] == 0
      brd[y - 1][x] = 1
      q << [x, y - 1]
    end
    if y < h - 1 && brd[y + 1][x] == 0
      brd[y + 1][x] = 1
      q << [x, y + 1]
    end
  end
end

### main

loop do
  w, h = gets.strip.split(' ').map{|s| s.to_i}
  break if (w | h) == 0

  n = gets.strip.to_i

  xs0 = []
  ys0 = []
  xs1 = []
  ys1 = []

  for i in (0...n)
    xs0[i], ys0[i], xs1[i], ys1[i] = gets.strip.split(' ').map{|s| s.to_i}
    xs1[i] -= 1
    ys1[i] -= 1
  end

  w0 = compress(xs0, xs1, n, w)
  h0 = compress(ys0, ys1, n, h)
  #p [xs0, xs1, ys0, ys1]
  #p [w0, h0]

  brd = h0.times.map{w0.times.map{0}}

  for i in (0...n)
    for y in (ys0[i]..ys1[i])
      for x in (xs0[i]..xs1[i])
        brd[y][x] = 1
      end
    end
  end
  #p brd

  cl = 0

  for y in (0...h0)
    for x in (0...w0)
      if brd[y][x] == 0
        cl += 1
        paint(brd, x, y, w0, h0)
      end
    end
  end

  puts cl
end