#if 1
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <fstream>

std::ifstream ifs("0531-input.txt");
#define in std::cin//ifs

int32_t W, H, n;

int32_t pos1x[1001];
int32_t pos1y[1001];
int32_t pos2x[1001];
int32_t pos2y[1001];
int16_t map[2010][2010];//x,y

int32_t pr(int32_t* v1, int32_t* v2, int32_t N)
{
	static int32_t memo[2002];
	auto iter = memo;
	for (int32_t i = 0; i < N; ++i) {
		*iter++ = v1[i];
		*iter++ = v2[i];
	}
	std::sort(memo, iter);
	iter = std::unique(memo, iter);
	for (int32_t i = 0; i < N; ++i) {
		v1[i] = std::lower_bound(memo, iter, v1[i]) - memo;
		v2[i] = std::lower_bound(memo, iter, v2[i]) - memo;
	}
	return iter - memo;
}

int main()
{
	in.sync_with_stdio(false);
	std::cout.sync_with_stdio(false);

	in >> W >> H;

	while (W != 0 && H != 0)
	{
		for (auto& ar : map)for (auto& i : ar)i = 0;

		in >> n;
		for (int i = 0; i < n; ++i) {
			in >> pos1x[i] >> pos1y[i] >> pos2x[i] >> pos2y[i];
		}
		pos1x[n] = pos1y[n] = 0;
		pos2x[n] = W; pos2y[n] = H;

#if 0
		std::cout << "------------------------------------\n";
		for (int i = 0; i < n; ++i) {
			std::cout << pos1x[i] << ' ' << pos1y[i] << ' ' << pos2x[i] << ' ' << pos2y[i] << '\n';
		}
#endif

		W = pr(pos1x, pos2x, n+1)-1;
		H = pr(pos1y, pos2y, n+1)-1;

#if 0
		std::cout << "------------------------------------\n";
		for (int i = 0; i < n; ++i) {
			std::cout << pos1x[i] << ' ' << pos1y[i] << ' ' << pos2x[i] << ' ' << pos2y[i] << '\n';
		}
#endif


		//????????????????????£??????????????¶???
		for (int i = 0; i < n; ++i) {
			++map[pos1x[i]][pos1y[i]];
			--map[pos1x[i]][pos2y[i]];
			--map[pos2x[i]][pos1y[i]];
			++map[pos2x[i]][pos2y[i]];
		}
		//?¨??????????
		for (int x = 1; x < W; ++x) {
			for (int y = 0; y < H; ++y) {
				map[x][y] += map[x - 1][y];
			}
		}
		//???
		for (int x = 0; x < W; ++x) {
			for (int y = 1; y < H; ++y) {
				map[x][y] += map[x][y - 1];
			}
		}

#if 0
		std::cout << "------------------------------------\n";
		for (signed y = H - 1; y >= 0; --y) {
			for (int x = 0; x < W; ++x) {
				std::cout << map[x][y];
			}
			std::cout << '\n';
		}
#endif

		//???????????¢???
		constexpr int32_t diff[4][2] = {
			{ 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 },
		};
		int32_t res = 0;
		std::queue<uint32_t> que;
		for (int x = 0; x < W; ++x) {
			for (int y = 0; y < H; ++y) {
				if (0 < map[x][y]) { continue; }
				++res;
				map[x][y] = 1;
				que.push((x << 16) | y);
				while (!que.empty())
				{
					auto raw = que.front(); que.pop();
					int32_t X_ = raw >> 16;
					int32_t Y_ = raw & 0xFFFF;
					for (auto& d : diff) {
						auto nx = X_ + d[0], ny = Y_ + d[1];
						if (
							[](int32_t v) {return 0 <= v&&v<W; }(nx) &&
							[](int32_t v) {return 0 <= v&&v<H; }(ny) &&
							(map[nx][ny] <= 0)
							) {
							map[nx][ny] = 1;
							que.push(((uint32_t)(nx) << 16) | (uint32_t)(ny));
						}
					}
				}

#if 0
				std::cout << "------------------------------------\n";
				for (signed y = H - 1; y >= 0; --y) {
					for (int x = 0; x < W; ++x) {
						std::cout << map[x][y];
					}
					std::cout << '\n';
				}
#endif

			}
		}//for
		std::cout << res << std::endl;

#if 0
		std::cout << "------------------------------------\n";
#endif

		in >> W >> H;
	}
}
#endif

Measured memory_bytes: 11016