#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = int;
using vll = vector<ll>;
using pll = pair<ll, ll>;
#define rep(i,n) for(ll i(0);(i)<(n);(i)++)

ll compress(vll &x1, vll &x2, ll w){
    vll xs;
    ll N = x1.size();
    //* detect important index into xs
    rep(i, N){
        for(ll d=-1; d <= 1; d++){ //* before(-1)/at(0)/after(+1)
            ll tx1 = x1[i]+d, tx2 = x2[i]+d;
            if(0<= tx1 && tx1 < w)
                xs.push_back(tx1);
            if(0<= tx2 && tx2 < w)
                xs.push_back(tx2);
        }
    }
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    //* rewrite x1, x2 using index in xs
    rep(i, N){
        x1[i] = lower_bound(xs.begin(), xs.end(), x1[i])-xs.begin();
        x2[i] = lower_bound(xs.begin(), xs.end(), x2[i])-xs.begin();
    }
    return xs.size();
}

int main(){
    while(1){
        ll W, H, N;
        cin >> W >> H;
        if(W==0 && H == 0)
            break;
        cin >> N;
        vll x1(N), x2(N), y1(N), y2(N);
        rep(i, N){
            scanf("%d", &x1[i]);
            scanf("%d", &y1[i]);
            scanf("%d", &x2[i]);
            scanf("%d", &y2[i]);

            x2[i]--; y2[i]--;
        }

        //* compress coordinates
        W = compress(x1, x2, W);
        H = compress(y1, y2, H);

        //* build the board and fill-in the mass
        vector<vll>  bd(W+1, vll(H+1, 0));

        rep(i, N){
            bd[x1[i]][y1[i]]     ++;
            bd[x2[i]+1][y2[i]+1] ++;
            bd[x1[i]][y2[i]+1] --;
            bd[x2[i]+1][y1[i]] --;
        }
        rep(x, W){
            rep(y, H){
                bd[x][y+1] += bd[x][y];
            }
        }

        rep(y, H){
            rep(x, W){
                bd[x+1][y] += bd[x][y];
            }
        }

        //* BFS on new board
        ll cnt = 0;
        vll dx{0, 1, 0, -1}, dy{1, 0, -1, 0};
        rep(x, W){
            rep(y, H){
                if(bd[x][y]!=0)
                    continue;
                //* found one blank mass
                cnt++;
                queue<pll> que;
                que.push(make_pair(x, y));
                bd[x][y] = 1;
                while(!que.empty()){
                    ll sx = que.front().first, sy = que.front().second;
                    que.pop();
                    rep(i, 4){
                        ll tx = sx + dx[i], ty = sy + dy[i];
                        if(tx < 0 || tx >= W || ty < 0 || ty >= H)
                            continue;
                        if(bd[tx][ty]!=0)
                            continue;
                        que.push(make_pair(tx, ty));
                        bd[tx][ty] = 1;
                    }
                }

            }
        }

        cout << cnt << endl;
    }
    return 0;
}


Measured memory_bytes: 9760