#include<bits/stdc++.h>
#define P pair<int,int>
using namespace std;

P x[1000], y[1000];
bool S[2005][2005];
int dx[4] = { -1,0,0,1 }, dy[4] = { 0,-1,1,0 };
int main() {
	int a, b;
	while (cin >> a >> b, a | b) {
		set<int>c[2];
		c[0].insert(0); c[0].insert(a);
		c[1].insert(0); c[0].insert(b);
		int d; cin >> d;
		for (int e = 0; e < d; e++) {
			int f, g, h, i; scanf("%d %d %d %d", &f, &g, &h, &i);
			c[0].insert(f); c[0].insert(h);
			c[1].insert(g); c[1].insert(i);
			x[e] = P(f, g);
			y[e] = P(h, i);
		}
		memset(S, 0, sizeof(S));
		int taiou[2][2005];
		unordered_map<int, int>U[2];
		auto e = c[0].begin();
		for (int f = 0; f < c[0].size(); e++, f++) {
			taiou[0][f] = *e;
			U[0][*e] = f;
		}
		e = c[1].begin();
		for (int f = 0; f < c[1].size(); e++, f++) {
			taiou[1][f] = *e;
			U[1][*e] = f;
		}
		for (int g = 0; g < d; g++) {
			for (int l = U[0][x[g].first]; l < U[0][y[g].first]; l++) {
				for (int r = U[1][x[g].second]; r < U[1][y[g].second]; r++) {
					S[l][r] = true;
				}
			}
		}
		int ans = 0;
		for (int i = 0; i+1 < c[0].size(); i++) {
			for (int j = 0; j+1 < c[1].size(); j++) {
				if (!S[i][j]) {
					ans++;
					queue<P>Q;
					Q.push(P(i,j));
					while (Q.size()) {
						P t = Q.front(); Q.pop();
						S[i][j] = true;
						for (int w = 0; w < 4; w++) {
							int n = t.first + dx[w], m = t.second + dy[w];
							if (n >= 0 && n+1 < c[0].size() && m >= 0 && m+1 < c[1].size() && !S[n][m]) {
								S[n][m] = true;
								Q.push(P(n, m));
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}