#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = {
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};


typedef pair<pii, pii> Block;

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) { //????????\??£??????????????¨ y?????\??£?????????????????????
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { //x??¨y????????????????????\??£??????????????????????????????
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	bool isroot(int x) {
		return data[x] < 0;
	}
	int size(int x) {
		return -data[root(x)];
	}
};

signed main() {
	int w, h, n;

	while (scanf("%d %d", &w, &h)){
		if (!w && !h)
			break;
	scanf("%d", &n);
	auto fair = [](Block& block) {
		if (block.first.first > block.second.first) {
			swap(block.first.first, block.second.first);
		}
		if (block.first.second > block.second.second) {
			swap(block.first.second, block.second.second);
		}
	};

	vector<Block> bs(n);
	vector<int> ax(n * 2), ay(n * 2);

	REP(i, n) {
		scanf("%d %d", &ax[i * 2], &ay[i * 2]);
		bs[i].first = { ax[i * 2],ay[i * 2] };
		scanf("%d %d", &ax[i * 2 + 1], &ay[i * 2 + 1]);
		bs[i].second = { ax[i * 2 + 1],ay[i * 2 + 1] };
		fair(bs[i]);//??´??¢
	}
	ax.push_back(0);
	ay.push_back(0);
	ax.push_back(w);
	ay.push_back(h);
	sort(ALL(bs));
	sort(ALL(ax));
	sort(ALL(ay));
	ax.erase(unique(ALL(ax)), ax.end());
	ay.erase(unique(ALL(ay)), ay.end());
	vector<vector<int>> isryt((int) ax.size() - 1, vector<int>((int) ay.size() - 1, -1));
	UnionFind uf((ax.size() - 1)*(ay.size() - 1) + 1);


	REP(i, (int) ax.size() - 1) {
		REP(j, (int) ay.size() - 1) {
			Block nb = { {ax[i],ay[j]},{ ax[i + 1],ay[j + 1] } };
			fair(nb);
			//auto itr = lower_bound(ALL(bs), nb);
			bool f = 0;
			REP(c, n) {
				if (bs[c].first.first <= nb.first.first &&
					bs[c].first.second <= nb.first.second &&
					bs[c].second.first >= nb.second.first &&
					bs[c].second.second >= nb.second.second) {
					f = 1;
					break;
				}
			}
			if (!f) {
				isryt[i][j] = 0;
				REP(k, 4) {
					pii nex = { i + Dir[k].first,j + Dir[k].second };
					if (RANGE(nex.first, 0, ax.size() - 2) && RANGE(nex.second, 0, ay.size() - 2)) {
						if (!isryt[nex.first][nex.second])
							uf.unionSet(j*(ax.size() - 1) + i, nex.second*(ax.size() - 1) + nex.first);
					}
				}
			}
			else {
				isryt[i][j] = 1;
				uf.unionSet(j*(ax.size() - 1) + i, (ax.size() - 1)*(ay.size() - 1));
			}
		}
	}

	int ans = 0;
	REP(i, uf.data.size()) {
		if (uf.isroot(i)) {
			ans++;
		}
	}
	printf("%d\n", ans - 1);
}
	return 0;
}

Measured memory_bytes: 5280