/*
 * hujx.cpp
 *
 *  Created on: 2015年5月21日
 *      Author: dell
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
#include<set>
#include<bitset>
//#define ONLINE_JUDGE
#define eps 1e-6
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll long long
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
//#pragma comment(linker,"/STACK:1024000000,1024000000")
int n,m;
#define N 210
#define M 1000010
#define Mod 1000000000
#define p(x,y) make_pair(x,y)
#define pr pair<ll,int>
int w,h;
int x_1[1010],y_1[1010],x_2[1010],y_2[1010];
int vis[2010][2010];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int compress(int *x1,int *x2,int val){
	vector<int> v;
	for(int i=0;i<n;i++){
		int tmp1 = x1[i];
		int tmp2 = x2[i];
		if(tmp1>=1 && tmp1<val) v.push_back(tmp1);
		if(tmp2>=1 && tmp2<val) v.push_back(tmp2);
	}
	v.push_back(0);
	v.push_back(val);
	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	for(int i=0;i<n;i++){
		x1[i] = find(v.begin(),v.end(),x1[i])-v.begin();
		x2[i] = find(v.begin(),v.end(),x2[i])-v.begin();
	}
	return v.size()-1;
}
bool check(int x,int y){
	if(x<0 || x>h || y<0 || y>w || vis[x][y]>0)
		return false;
	return true;
}
int bfs(){
	int ans=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(vis[i][j]) continue;
			ans++;
			queue<pair<int,int> > q;
			q.push(p(j,i));
			while(!q.empty()){
				int x = q.front().first;
				int y = q.front().second;
				q.pop();
				for(int k=0;k<4;k++){
					int xx = x+dir[k][0];
					int yy = y+dir[k][1];
					if(!check(yy,xx)) continue;
					vis[yy][xx] = 1;
					q.push(p(xx,yy));
				}
			}
		}
	}
	return ans;
}
int main(){
    while(sfd(w,h) != EOF && w+h){
    	sf(n);
    	for(int i=0;i<n;i++){
    		scanf("%d%d%d%d",&x_1[i],&y_1[i],&x_2[i],&y_2[i]);
    	}
    	w = compress(x_1,x_2,w);
    	h = compress(y_1,y_2,h);
    	memset(vis,0,sizeof vis);
    	for(int i=0;i<n;i++){
    		vis[y_1[i]][x_1[i]]++;
    		vis[y_1[i]][x_2[i]]--;
    		vis[y_2[i]][x_1[i]]--;
    		vis[y_2[i]][x_2[i]]++;
    	}
    	for(int i=0;i<h;i++){
    		for(int j=1;j<w;j++){
    			vis[i][j] += vis[i][j-1];
    		}
    	}
    	for(int i=1;i<h;i++){
    		for(int j=0;j<w;j++){
    			vis[i][j] += vis[i-1][j];
    		}
    	}
    	int ans = bfs();
    	pf(ans);
    }
    return 0;
}

Measured memory_bytes: 17020