#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();
  Rect(istream *is);
  Rect(long l, long b, long r, long t);
  void print(ostream *os);
};
Rect::Rect(istream *is){
  cin >> (this -> left);
  cin >> (this -> bottom);
  cin >> (this -> right);
  cin >> (this -> 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;
  vector<Rect>::iterator rect,frag,next;  
  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(2*n);
  add = new vector<Rect>;
  add->reserve(2*n);
  frags -> push_back(rectAll);
  for(rect = rects.begin();rect!=rects.end();rect++){
    for(frag = frags->begin(); frag != frags->end();frag++){
      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;
  }

  sort(frags->begin(),frags->end(),LessRect());
  
  Rect *f1,*f2;
  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->right <= f1->right) continue;
      if(f2->left > f1->right) continue;
      if(f2->bottom >= f1->top) continue;
      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; cin >> height;
  while((width != 0) || (height != 0)){
    cin >> n;
    res = paint(width,height,n);
    cout << res << "\n";
    cin >> width; cin >> height;
  }
  return 0;
}

Measured memory_bytes: 1620