#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#define BIT(nr) (1ULL << (nr))
//#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	//ない環境用
#define LL_HALFMAX 9223372036854775807 / 2	//ない環境用
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }


using namespace std;

//座標圧縮

class compress {
public:
	//map<int, int> zip;
	int unzip[3010];
	int size = 0;

	int zip(int x) {
		return lower_bound(unzip, unzip + size, x) - unzip;
	}

	compress(vector<int> x)
	{
		sort(x.begin(), x.end());
		x.erase(unique(x.begin(), x.end()), x.end());
		for (int i = 0; i < x.size(); i++) {
			unzip[i] = x[i];
			size++;
		}
	}
};

int mapData[3010][3010];
int dx[4] = { 0, 1, 0,-1 }; // x軸方向への変位
int dy[4] = { 1, 0,-1, 0 }; // y軸方向への変位

void solve(int W, int H) {
	rep(i, 3010)rep(j, 3010) mapData[i][j] = 0;

	int n;
	cin >> n;

	vector<int> x1(n), y1(n), x2(n), y2(n), x, y;
	int maxX = -1, maxY = -1;
	rep(i, n) {
		cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
		chmax(maxX, x1[i]);
		chmax(maxX, x2[i]);
		chmax(maxY, y1[i]);
		chmax(maxY, y2[i]);

	}

	rep(i, n) {
		for (int d = -1; d <= 1; d++) {
			int tx1 = x1[i] + d;
			int tx2 = x2[i] + d;
			int ty1 = y1[i] + d;
			int ty2 = y2[i] + d;
			if (tx1 >= 0 && tx1 <= W)x.push_back(tx1);
			if (tx2 >= 0 && tx2 <= W)x.push_back(tx2);
			if (ty1 >= 0 && ty1 <= H)y.push_back(ty1);
			if (ty2 >= 0 && ty2 <= H)y.push_back(ty2);
		}
	}
	x.push_back(W);
	y.push_back(H);

	compress cx(x), cy(y);

	rep(i, n) {
		int cx1 = cx.zip(x1[i]);
		int cx2 = cx.zip(x2[i]);
		int cy1 = cy.zip(y1[i]);
		int cy2 = cy.zip(y2[i]);
		for (int y = cy1; y < cy2; y++) {
			for (int x = cx1; x < cx2; x++) {
				mapData[y][x] = 1;
			}
		}
	}

	// bfs
	int ans = 0;
	int nh = cy.zip(H) - 1;
	int nw = cx.zip(W) - 1;

	rep(y, nh) {
		rep(x, nw) {
			if (mapData[y][x] == 1) continue;
			ans++;
			queue<pair<int, int>> q;
			q.push(mp(x, y));

			while (!q.empty()) {
				int sx = q.front().first, sy = q.front().second;
				q.pop();
				rep(i, 4) {
					int tx = sx + dx[i], ty = sy + dy[i];
					if (tx < 0 || nw < tx || ty < 0 || nh < ty) continue;
					if (mapData[ty][tx]) continue;
					q.push(mp(tx, ty));
					mapData[ty][tx] = 1;
				}
			}
		}
	}

	cout << ans << "\n";

}

signed main() {

	while (1) {
		int W, H;
		cin >> W >> H;
		if (W == 0 && H == 0) {
			break;
		}
		solve(W, H);
	}

	return 0;
}



Measured memory_bytes: 38488