#include <iostream>
#include <list>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

//以下、長方形クラスRectの定義
class Rect{
public:
  long left,bottom,right,top;
  vector<long> nbrs;
  long color;
  Rect(istream *is);
  Rect(long l, long b, long r, long t);
  //void print(ostream *os);
};
Rect::Rect(istream *is){
  cin >> left >> bottom >> right >> top;
}
Rect::Rect(long l, long b, long r, long t){
  this -> left =l;
  this -> bottom = b;
  this -> right = r;
  this -> top = t;
}

/*
void Rect::print(ostream *os){
  *os << "(" << left << "," << bottom << ") (" << right << "," << top << ")";
}
ostream &operator<<(ostream &os, Rect &r){
  r.print(&os);
  return os;
}
 */


class LessRect{
public:
  bool operator()(const Rect& a, const Rect& b){
    return (a.left < b.left) || ((a.left == b.left)&&(a.bottom < b.bottom));
  }
};

//以下、各問題例を解くpaint()関数。返値は彩色数
long paint(long width, long height, int n){
  vector<Rect> rects;
  vector<Rect> *frags, *add, *tmp;
  Rect rectAll(0,0,width,height);

  rects.reserve(n);
  for(int i=0; i<n;i++){
    rects.push_back(Rect(&cin));
  }
  frags = new vector<Rect>;
  frags->reserve(n);
  add = new vector<Rect>;
  add->reserve(n);
  frags -> push_back(rectAll);


  Rect *rect,*frag;
  for(int i= 0;i<rects.size();i++){
    rect = &(rects.at(i));
    for(int j=0; j<frags->size();j++){
      frag = &(frags->at(j));
      if((rect->bottom >= frag->top) || (rect->top <= frag->bottom)){
        add->push_back(*frag);
        continue;
      }
      if(rect->left > frag->left){
        add->push_back(Rect(frag->left,frag->bottom,
                            min(rect->left,frag->right),frag->top));
      }      
      if(rect->bottom > frag->bottom){
        if((rect->left < frag->right) && (rect->right > frag->left)){
          add->push_back(Rect(max(rect->left,frag->left),frag->bottom,
                              min(rect->right,frag->right),rect->bottom));
        }
      }
      if(rect->top < frag->top){
        if((rect->left < frag->right) && (rect->right > frag->left)){
          add->push_back(Rect(max(rect->left,frag->left),rect->top,
                              min(rect->right,frag->right),frag->top));
        }
      }      
      if(rect->right < frag->right){
        add->push_back(Rect(max(rect->right,frag->left),frag->bottom,
                            frag->right,frag->top));
      }
    }
    frags->clear();
    tmp = frags;
    frags = add;
    add = tmp;
  }

  Rect *f1,*f2;
  sort(frags->begin(),frags->end(),LessRect());
  for(int i=0; i < frags->size(); i++){
    f1 = &(frags->at(i));
    //cout << *f1 << endl;
    f1 -> color = 0; //次ステップ（彩色）のための初期化
    for(int j=i+1; j < frags->size(); j++){
      f2 = &(frags->at(j));
      if(f2->left < f1->right) continue;
      if(f2->left > f1->right) break;
      if(f2->bottom >= f1->top) break;
      if(f2->top <= f1 -> bottom) continue;
      (f1->nbrs).push_back(j);
      (f2->nbrs).push_back(i);
      //cout << *f1 << "->" << *f2 << endl;
    }
  }
  //深さ優先探索による彩色
  stack<long> st;
  long color = 0;  
  for(int i=0; i < frags->size(); i++){
    f1 = &(frags->at(i));
    if(f1 -> color != 0) continue;
    (f1 -> color) = ++color;
    for(int j=0; j< (f1->nbrs).size();j++) st.push((f1->nbrs).at(j));
    while(!st.empty()){
      f2 = &(frags->at(st.top())); st.pop();
      if(f2->color != 0) continue;
      f2->color = color;
      for(int j=0; j< (f2->nbrs).size();j++) st.push((f2->nbrs).at(j));
    }
  }  
  delete frags;
  delete add;  
  return color;
}
//以下メイン関数
int main(){
  long width,height, res;
  int n;
  
  cin >> width >> height >> n;
  while((width != 0) || (height != 0)){
    res = paint(width,height,n);
    cout << res << "\n";
    cin >> width >> height >> n;
  }
  return 0;
}

Measured memory_bytes: 1604