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

//以下、長方形クラスRectの定義
class Rect{
public:
  int left,bottom,right,top;
  vector<int> nbrs;
  int color;
  Rect();
  Rect(int l, int b, int r, int t);
};
Rect::Rect(){
  scanf("%d%d%d%d",&left,&bottom,&right,&top);
}
Rect::Rect(int l, int b, int r, int t){
  this -> left =l;
  this -> bottom = b;
  this -> right = r;
  this -> top = t;
}

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()関数。返値は彩色数
int paint(int width, int 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());
  }
  frags = new vector<Rect>;
  add = new vector<Rect>;
  add->reserve(2*n);
  frags->reserve(2*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));
    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);
    }
  }
  //深さ優先探索による彩色
  stack<int> st;
  int 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(){
  int width,height,res,n;
  scanf("%d%d%d",&width,&height,&n);
  while((width != 0) && (height !=0)){
    res = paint(width,height,n);
    printf("%d\n",res);
    scanf("%d%d%d",&width,&height,&n);
  }
  return 0;
}

Measured memory_bytes: 1440