/*?????*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
	int len = bes.length();
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	cout << "Debug out:";
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void debug (const T &a, string bes = "") {
	int len = bes.length();
	cout << "Debug out:";
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

void IO_Init() {
	ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
	return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;

/*?????*/

int W, H, N;
int X1[MAXN], X2[MAXN],Y1[MAXN], Y2[MAXN];
bool fld[MAXN * 6][MAXN * 6];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

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.pb(tx1);
            if(1 <= tx2 && tx2 <= w) xs.pb(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);

    mem(fld, false);

    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;
            }
        }
    }
    int ans = 0;

    for(int y = 0;y < H;y ++){
        for(int x = 0;x < W;x ++){
            if(fld[y][x]) continue;
            ans ++;
            fld[y][x] = true;
            queue<PII> que;
            que.push(mp(x, y));
            while(!que.empty()){
                int sx = que.front().first, sy = que.front().second;
                que.pop();
                for(int i = 0;i < 4;i ++){
                    int nx = sx + dx[i];
                    int ny = sy + dy[i];
                    if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
                    if(fld[ny][nx]) continue;
                    que.push(mp(nx, ny));
                    fld[ny][nx] = true;
                }
            }
        }
    }

    printf("%d\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	//FIN;
	//FOUT;
#endif

	while(~scanf("%d%d", &W, &H), W && H){
        scanf("%d", &N);
        for(int i = 0;i < N;i ++){
            scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
            X1[i] ++, Y1[i] ++;
        }
        solve();
	}
	return 0;
}

Measured memory_bytes: 38800