#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;

typedef long long int64;
const int inf = (int)1e9;

struct rect {
	int x1, x2, y1, y2;
	rect() : x1(0), y1(0), x2(0), y2(0) {}
	rect(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

int main()
{
	const int dx[] = {-1, 0, 0, 1};
	const int dy[] = {0, -1, 1, 0};
	
	while(true) {
		
		int w, h, n;
		vector<rect> sheet;
		vector<int> x, y;
		
		scanf("%d%d", &w, &h);
		if(w == 0)
			break;
		scanf("%d", &n);
		sheet.resize(n);
		x.resize(n * 2 + 2);
		y.resize(n * 2 + 2);
		for(int i = 0; i < n; ++i) {
			scanf("%d%d%d%d", &sheet[i].x1, &sheet[i].y1, &sheet[i].x2, &sheet[i].y2);
		}
		for(int i = 0; i < n; ++i) {
			x[i * 2 + 0] = sheet[i].x1, x[i * 2 + 1] = sheet[i].x2;
			y[i * 2 + 0] = sheet[i].y1, y[i * 2 + 1] = sheet[i].y2;
		}
		x[n * 2 + 0] = 0, x[n * 2 + 1] = w;
		y[n * 2 + 0] = 0, y[n * 2 + 1] = h;
		sort(x.begin(), x.end());
		sort(y.begin(), y.end());
		x.erase(unique(x.begin(), x.end()), x.end());
		y.erase(unique(y.begin(), y.end()), y.end());
		
		for(int i = 0; i < n; ++i) {
			sheet[i].x1 = lower_bound(x.begin(), x.end(), sheet[i].x1) - x.begin();
			sheet[i].x2 = lower_bound(x.begin(), x.end(), sheet[i].x2) - x.begin();
			sheet[i].y1 = lower_bound(y.begin(), y.end(), sheet[i].y1) - y.begin();
			sheet[i].y2 = lower_bound(y.begin(), y.end(), sheet[i].y2) - y.begin();
		}
		
		static int imos[1050][1050];
		
		fill(imos[0], imos[1050], 0);
		
		
		for(int i = 0; i < n; ++i) {
			rect &r = sheet[i];
			imos[r.x1][r.y1] += 1;
			imos[r.x2][r.y2] += 1;
			imos[r.x2][r.y1] -= 1;
			imos[r.x1][r.y2] -= 1;
		}
		
		for(int itx = 1; itx < 1050; ++itx) {
			for(int ity = 0; ity < 1050; ++ity)
				imos[itx][ity] += imos[itx - 1][ity];
		}
		
		for(int itx = 0; itx < 1050; ++itx) {
			for(int ity = 1; ity < 1050; ++ity)
				imos[itx][ity] += imos[itx][ity - 1];
		}
		
		int ans = 0;
		
		for(int itx = 0; itx < x.size() - 1; ++itx) {
			for(int ity = 0; ity < y.size() - 1; ++ity) {
				
				if(imos[itx][ity] == 0) {
				
					/*if(ans == 0) {
						printf("[debug] %d\n", ans);
						for(int iy = 0; iy < y.size(); ++iy) {
							for(int ix = 0; ix < x.size(); ++ix)
								printf("%c", imos[ix][iy] == 0 ? ' ' : imos[ix][iy] > 0 ? 'X' : '.');
							printf("\n");
						}
					}*/
					
					ans += 1;
					
					queue<pair<int, int> > q;
					
					q.push(make_pair(itx, ity));
					
					while(!q.empty()) {
						
						int cx = q.front().first;
						int cy = q.front().second;
						q.pop();
						
						for(int i = 0; i < 4; ++i) {
							
							int nx = cx + dx[i];
							int ny = cy + dy[i];
							
							if(nx < 0 || ny < 0 || nx >= x.size() - 1 || ny >= y.size() - 1)
								continue;
							if(imos[nx][ny] != 0)
								continue;
							
							imos[nx][ny] = -1;
							q.push(make_pair(nx, ny));
						}
					}
					
					/*printf("[debug] %d\n", ans);
					for(int iy = 0; iy < y.size(); ++iy) {
						for(int ix = 0; ix < x.size(); ++ix)
							printf("%c", imos[ix][iy] == 0 ? ' ' : imos[ix][iy] > 0 ? 'X' : '.');
						printf("\n");
					}*/
				}
			}
		}
		
		printf("%d\n", ans);
	}
	
	return 0;
}

/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */

Measured memory_bytes: 5532