#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
#define UNIQUE(v) { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); }

int main () {

    ll w, h;
    while (cin >> w >> h, w) {

        ll n; cin >> n;
        vector<ll> x1(n), y1(n);
        vector<ll> x2(n), y2(n);
        for(ll i = 0; i < n; i++) {
            cin >> x1[i] >> y1[i];
            cin >> x2[i] >> y2[i];
        }

        vector<ll> unqx = x1;
        vector<ll> unqy = y1;
        for (ll &xi : x2) unqx.push_back(xi);
        for (ll &yi : y2) unqy.push_back(yi);
        unqx.push_back(0);
        unqx.push_back(w);
        unqy.push_back(0);
        unqy.push_back(h);
        UNIQUE(unqx);
        UNIQUE(unqy);
        map<ll, ll> sahzx, sahzy;
        for (ll i = unqx.size(); i--; ) sahzx[unqx[i]] = i;
        for (ll i = unqy.size(); i--; ) sahzy[unqy[i]] = i;

        ll wu = unqx.size();
        ll hu = unqy.size();
        vector<vector<int>> imosee(wu+2, vector<int>(hu+2, 0));
        for (ll i = n; i--; ) {
            imosee[sahzx[x1[i]]+1][sahzy[y1[i]]+1]++;
            imosee[sahzx[x1[i]]+1][sahzy[y2[i]]+1]--;
            imosee[sahzx[x2[i]]+1][sahzy[y1[i]]+1]--;
            imosee[sahzx[x2[i]]+1][sahzy[y2[i]]+1]++;
        }
        for (ll x = 1; x <= wu; x++) {
            for (ll y = 1; y <= hu; y++) {
                imosee[x][y+1] += imosee[x][y];
            }
        }
        for (ll y = 1; y <= hu; y++) {
            for (ll x = 1; x <= wu; x++) {
                imosee[x+1][y] += imosee[x][y];
            }
        }
        for (ll y = 1; y <= hu; y++) {
            for (ll x = 1; x <= wu; x++) {
                imosee[x][y] = !!imosee[x][y];
            }
        }
        for (ll x = 0; x <= wu+1; x++) {
            imosee[x][0] = true;
            imosee[x][hu] = true;
            imosee[x][hu+1] = true;
        }
        for (ll y = 0; y <= hu+1; y++) {
            imosee[0][y] = true;
            imosee[wu][y] = true;
            imosee[wu+1][y] = true;
        }

        ll ans = 0;
        vector<vector<bool>> chk(wu+2, vector<bool>(hu+2, false));
        for (ll x = 1; x <= wu; x++) {
            for (ll y = 1; y <= hu; y++) {
                if (chk[x][y] || imosee[x][y]) continue;
                deque<pair<ll, ll>> q = {{x, y}};
                chk[x][y] = true;
                ans++;
                while (q.size()) {
                    pair<ll, ll> v = q.front(); q.pop_front();
                    ll xv = v.first;
                    ll yv = v.second;
                    
                    if (!imosee[xv-1][yv] && !chk[xv-1][yv]) {
                        q.emplace_back(xv-1, yv);
                        chk[xv-1][yv] = true;
                    }
                    if (!imosee[xv+1][yv] && !chk[xv+1][yv]) {
                        q.emplace_back(xv+1, yv);
                        chk[xv+1][yv] = true;
                    }
                    if (!imosee[xv][yv-1] && !chk[xv][yv-1]) {
                        q.emplace_back(xv, yv-1);
                        chk[xv][yv-1] = true;
                    }
                    if (!imosee[xv][yv+1] && !chk[xv][yv+1]) {
                        q.emplace_back(xv, yv+1);
                        chk[xv][yv+1] = true;
                    }
                }
            }
        }

    cout << ans << endl;

    
    }

}


Measured memory_bytes: 4324