#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<stack>

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(n) n.begin(),n.end()
#define SIZE 2048

struct P{
  int x,y;
  P(int x1, int y1){x=x1,y=y1;}
};

int compress(vector<int> &x1, vector<int> &x2, int W){
  vector<int> xs;
  unsigned int size = x1.size();
  for(int i=0; i<size; i++){
    for(int d=-1; d<=1; d++){
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if(0<=tx1 && tx1 <=W) xs.push_back(tx1);
      if(0<=tx2 && tx2 <=W) xs.push_back(tx2);
    }
  }
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());
  int end = 0;

  for(int i=0; i<size; i++){
    x1[i] = find(all(xs), x1[i]) - xs.begin();
    x2[i] = find(all(xs), x2[i]) - xs.begin();
    if(end<x2[i])
      end = x2[i];
  }
  return xs.size()-1;
}

void dfs(bool Map[SIZE][SIZE], P start, int W, int H){
  static const int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
  stack<P> st;
  st.push(start);
  while(st.size()){
    P s = st.top();
    st.pop();
    rep(i, 4){
      P t = P(s.x+dx[i], s.y+dy[i]);
      if(t.y>=0 && t.x>=0 && t.y<H && t.x<W && Map[t.y][t.x]==false){
	Map[t.y][t.x]=true;
	st.push(t);
      }
    }
  }
}

bool board[SIZE][SIZE];

int main(){
  int W,H,N;
  while(cin>>W>>H, W|H){
    cin>>N;    
    vector<int> x1, x2, y1, y2;
    rep(i,N){
      int a, b, c, d;
      cin>>a>>b>>c>>d;
      x1.push_back(a);y1.push_back(b);
      x2.push_back(c);y2.push_back(d);
    }

    W = compress(x1, x2, W);
    H = compress(y1, y2, H);
    memset(board, 0, sizeof(board));
    rep(i, N){
      for(int y=y1[i]; y<y2[i]; y++){
	memset(board[y]+x1[i], 1, x2[i]-x1[i]);
      }
    }

    int count = 0;
    rep(y, H){
      rep(x, W){
	if(board[y][x]==false){
	  count++;
	  dfs(board, P(x, y), W, H);
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}