Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <cmath>
#define INF 10000000
#define REP(i, n) for(int i = 0;i < (n);i++)
#define all(n) n.begin(),n.end()

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

const int MAX_N = 1000;

int w, h, n;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int fld[6*MAX_N][6*MAX_N];

int compress(int *x1, int *x2, int W){
	vector<int> xs;
	
	REP(i, n){
		for(int d = -1;d <= 1;d++){
			int tx1 = x1[i]+d, tx2 = x2[i]+d;
			if(0 <= tx1 && tx1 <= W*2) xs.push_back(tx1);
			if(0 <= tx2 && tx2 <= W*2) xs.push_back(tx2);
		}
	}

	sort(all(xs));
	//xs.erase(unique(all(xs)), xs.end());

	REP(i, n){
		x1[i] = find(all(xs), x1[i]) - xs.begin();
		x2[i] = find(all(xs), x2[i]) - xs.begin();
	}
	return xs.size();
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void dfs(int x, int y){
	if(fld[x][y]) return;

	fld[x][y] = true;

	REP(i, 4){
		//int tx = x + dx[i];
		//int ty = y + dy[i];
		if(0 <= x+dx[i] && x+dx[i] < w && 0 <= y+dy[i] && y+dy[i] < h && !fld[x+dx[i]][y+dy[i]]) dfs(x+dx[i], y+dy[i]);
	}
}

int main(void){
	while(cin >> w >> h && w && h){
		cin >> n;
		REP(i, n){
			cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
			X1[i] = X1[i] * 2;
			X2[i] = X2[i] * 2;
			Y1[i] = Y1[i] * 2;
			Y2[i] = Y2[i] * 2;
		}

		REP(i, MAX_N*6){
			fill(fld[i], fld[i]+(MAX_N*6), false);
		}

		w = compress(X1, X2, w);
		h = compress(Y1, Y2, h);

		REP(k, n){
			for(int i = X1[k];i <= X2[k];i++){
				for(int j = Y1[k];j <= Y2[k];j++){
					fld[i][j] = true;
				}
			}
		}

		int res = 0;
		REP(i, w){
			REP(j, h){
				if(fld[i][j]) continue;

				res++;
//				dfs(i, j);
				queue<PII> que;
				que.push(PII(i, j));
				while(!que.empty()){
					PII p = que.front(); que.pop();
					REP(k, 4){
						int tx = p.first+dx[k], ty = p.second + dy[i];
						if(tx < 0 || w <= tx || ty < 0 || h <= ty || fld[tx][ty]) continue;
						fld[tx][ty] = true;
						que.push(PII(tx, ty));
					}
				}
			}
		}

		cout << res << endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:91:39: error: 'PII' was not declared in this scope
   91 |                                 queue<PII> que;
      |                                       ^~~
a.cc:91:42: error: template argument 1 is invalid
   91 |                                 queue<PII> que;
      |                                          ^
a.cc:91:42: error: template argument 2 is invalid
a.cc:92:37: error: request for member 'push' in 'que', which is of non-class type 'int'
   92 |                                 que.push(PII(i, j));
      |                                     ^~~~
a.cc:93:44: error: request for member 'empty' in 'que', which is of non-class type 'int'
   93 |                                 while(!que.empty()){
      |                                            ^~~~~
a.cc:94:44: error: expected ';' before 'p'
   94 |                                         PII p = que.front(); que.pop();
      |                                            ^~
      |                                            ;
a.cc:94:66: error: request for member 'pop' in 'que', which is of non-class type 'int'
   94 |                                         PII p = que.front(); que.pop();
      |                                                                  ^~~
a.cc:96:58: error: 'p' was not declared in this scope
   96 |                                                 int tx = p.first+dx[k], ty = p.second + dy[i];
      |                                                          ^
a.cc:97:73: error: 'ty' was not declared in this scope; did you mean 'tx'?
   97 |                                                 if(tx < 0 || w <= tx || ty < 0 || h <= ty || fld[tx][ty]) continue;
      |                                                                         ^~
      |                                                                         tx
a.cc:98:57: error: 'ty' was not declared in this scope; did you mean 'tx'?
   98 |                                                 fld[tx][ty] = true;
      |                                                         ^~
      |                                                         tx
a.cc:99:53: error: request for member 'push' in 'que', which is of non-class type 'int'
   99 |                                                 que.push(PII(tx, ty));
      |                                                     ^~~~