/*
AOJ 0531 Paint Color
Difficult: 4
*/

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <functional>
#include <string.h>
#include <cmath>

using namespace std;

const int MAXN = 1010;

int H, W;
int N;
int rx1[MAXN], rx2[MAXN], ry1[MAXN], ry2[MAXN];

bool read()
{
    scanf("%d%d", &W, &H);
    if(W==0 && H==0) return false;

    scanf("%d", &N);
    int x1,y1,x2,y2;
    for(int i=0; i<N; i++){
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        x2--, y2--;
        rx1[i] = x1, ry1[i] = y1;
        rx2[i] = x2, ry2[i] = y2;
    }
    return true;
}

map<int, int> mpx;
map<int, int> mpy;

void makeMap(){
    mpx.clear(), mpy.clear();
    int x[MAXN*2], y[MAXN*2];
    for(int i=0; i<N; i++){
        x[i] = rx1[i], x[i+N] = rx2[i];
        y[i] = ry1[i], y[i+N] = ry2[i];
    }
    x[N*2] = 0, x[N*2 + 1] = W-1;
    y[N*2] = 0, y[N*2 + 1] = H-1;
    sort(x, x+N*2+2);
    sort(y, y+N*2+2);
    int xn = unique(x, x+N*2+2) - x;
    int yn = unique(y, y+N*2+2) - y;

    mpx[x[0]] = 1;
    int idx = 1;
    for(int i=1; i<xn; i++){
        if(x[i] - x[i-1]!=1) idx++;
        mpx[x[i]] = ++idx;
    }
    W = idx;

    mpy[y[0]] = 1;
    idx = 1;
    for(int i=1; i<yn; i++){
        if(y[i] - y[i-1]!=1) idx++;
        mpy[y[i]] = ++idx;
    }
    H = idx;
}


struct BITTREE{
    int* x;
    int n;
    static int lowbit(int v){return v&(-v);}

    void set(int *_x, int _n){
        x = _x, n = _n;
        memset(x, 0, sizeof(int)*(1+n));
    }
    void add(int pos, int v){
        while(pos<=n){
            x[pos] += v;
            pos += lowbit(pos);
        }
    }
    int sum(int pos){
        int ans = 0;
        while(pos > 0){
            ans += x[pos];
            pos -= lowbit(pos);
        }
        return ans;
    }
};


int bitArray[4010][4010];
BITTREE bit[4010];
bool visited[4010][4010];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void bfs(int i, int j)
{
    queue<pair<int,int> >q;
    q.push(make_pair(i, j));
    visited[i][j] = true;
    while(!q.empty()){
        pair<int,int> p = q.front(); q.pop();
        int x = p.first, y = p.second;
        for(int d=0; d<4; d++){
            int cx = x + dx[d], cy = y + dy[d];
            if(cx>=1 && cx<=W && cy>=1 && cy<=H)
            if(!visited[cx][cy]) {
                q.push(make_pair(cx, cy));
                visited[cx][cy] = true;
            }
        }
    }
}

void solve()
{
    makeMap();
    for(int i=1; i<=W; i++)
        bit[i].set(bitArray[i], H);

    for(int i=0; i<N; i++)
    {
        int xs = mpx[rx1[i]], xe = mpx[rx2[i]];
        int ys = mpy[ry1[i]], ye = mpy[ry2[i]];
        for(int j=xs; j<=xe; j++){
            bit[j].add(ys,1);
            if(ye!=H) bit[j].add(ye+1, -1);
        }
    }

    for(int i=1; i<=W; i++){
    for(int j=1; j<=H; j++){
        visited[i][j] = (bit[i].sum(j) > 0);
        //printf("%d", visited[i][j]);
    }
        //printf("\n");
    }

    int blank = 0;
    for(int i=1; i<=W; i++)
    for(int j=1; j<=H; j++)
        if(!visited[i][j]){
            bfs(i,j); blank++;
        }
    printf("%d\n", blank);
}

int main()
{
    while(read())
        solve();
}

Measured memory_bytes: 23824