#include <iostream>
#include <cstdio>                                               //scanf,printf
#include <algorithm>                                            //max,min
#include <string>                                               //string
#include <cstring>                                              //strlen,strcat
#include <cctype>                                               //isalpha,tolower
#include <cmath>                                                //sqrt,sin
#include <stack>                                                //top,push
#include <queue>                                                //front,push
#include <vector>                                               //push_back,sort
#include <set>                                                  //insert,begin
#include <map>                                                  //insert,find
#include <list>                                                 //push_front,begin
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>                                               //pow,powf
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define repi(s,n) for(int i=s; i<n; i++)
#define repj(s,n) for(int j=s; j<n; j++)
#define repk(s,n) for(int k=s; k<n; k++)
#define repl(s,n) for(int l=s; l<n; l++)

#define MAX_N 1000


int W,H,N;
int X1[MAX_N],X2[MAX_N],Y1[MAX_N],Y2[MAX_N];

bool fld[MAX_N*6][MAX_N*6];

int compress(int *x1,int *x2,int w){
  vector<int> xs;
  for(int i=0;i<N;i++){
    for(int d=-1;d<=1;d++){
      int tx1=x1[i]+d,tx2=x2[i]+d;
      if(1<=tx1&&tx1<=w)xs.push_back(tx1);
      if(1<=tx2&&tx2<=w)xs.push_back(tx2);
    }
  }
  sort(xs.begin(),xs.end());
  xs.erase(unique(xs.begin(),xs.end()),xs.end());
  
  for(int i=0;i<N;i++){
    x1[i]=find(xs.begin(),xs.end(),x1[i])-xs.begin();
    x2[i]=find(xs.begin(),xs.end(),x2[i])-xs.begin();
  }
  
  return xs.size();
  
}

void solve(){
  
  
  W = compress(X1,X2,W);
  H = compress(Y1,Y2,H);
  
//  cout<<"w: "<<W<<en
//  cout<<"h: "<<H<<en
  
  
  memset(fld,0,sizeof(fld));
  
  for(int i=0;i<N;i++){
    for(int y=Y1[i];y<=Y2[i];y++){
      for(int x=X1[i];x<=X2[i];x++){
        fld[y][x]=true;
      }
    }
  }
  /*
   for(int i=0;i<H;i++){
   for(int j=0;j<W;j++){
   cout<<fld[i][j];
   }
   cout<<endl;
   }
   */
  int ans=0;
  for(int y=0;y<H;y++){
    for(int x=0;x<W;x++){
      if(fld[y][x])continue;
      ans++;
      queue< P > que;
      que.push(P(x,y));
      while(!que.empty()){
        int sx=que.front().first;
        int sy=que.front().second;
        que.pop();
        for(int i=0;i<4;i++){
          int tx=sx+dx[i],ty=sy+dy[i];
          if(tx<0||W<=tx)continue;
          if(ty<0||H<=ty)continue;
          if(fld[ty][tx])continue;
          que.push(P(tx,ty));
          fld[ty][tx]=true;
        }
      }
    }
  }
  cout<<ans<<endl;
}

int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0)break;
    cin>>N;
    for(int i=0;i<N;i++){
      cin>>X1[i]>>Y1[i]>>X2[i]>>Y2[i];
      X1[i]++;
      Y1[i]++;
    }
    solve();
  }
  return 0;
}



Measured memory_bytes: 38304