#include <iostream>
#include <algorithm>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <queue>
#define MAX_N 1010

using namespace std;

static int X1[1010], Y1[1010], X2[1010], Y2[1010], fld[MAX_N * 2][MAX_N * 2], 
            dx[4] = { -1, 0, 0, 1 }, dy[4] = { 0, -1, 1, 0 };

static int compress(int *const, int *const, const int, const int);
static int bfs(const int, const int);
void imos(const int, const int, const int);

int main(void)
{
    int W, H, N;

    //freopen("D:\\input.txt", "r", stdin);
    while (1)
    {
        scanf("%d%d", &W, &H);
        if (W == 0 && H == 0)
            break;
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
        {
            scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
        }
        W = compress(X1, X2, W, N);
        H = compress(Y1, Y2, H, N);

        imos(W, H, N);
        cout << bfs(W, H) << endl;
    }
    return EXIT_SUCCESS;
}

static int compress(int *const s1, int *const s2, const int W, const int N)
{
    //???????????£???
    vector<int>xs;

    for (int i = 0; i < N; i++)
    {
        if (0 < s1[i] && s1[i] < W) xs.push_back(s1[i]);
        if (0 < s2[i] && s2[i] < W) xs.push_back(s2[i]);
    }
    xs.push_back(0);
    xs.push_back(W);//?????????????????¶
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());

    for (int i = 0; i < N; i++)
    {
        s1[i] = find(xs.begin(), xs.end(), s1[i]) - xs.begin();
        s2[i] = find(xs.begin(), xs.end(), s2[i]) - xs.begin();
    }
    return xs.size() - 1;//??¨????????????????????????????????¶??????size??????2?????????1??????????\????????????£?????°?????????
}

static int bfs(const int W, const int H)
{
    int ans = 0;
    for (int i = 0; i < W; i++)
        for (int j = 0; j < H; j++)
        {
            if (fld[i][j])continue;//????´¢?????????????????????
            ans++;

            queue<pair<int, int> > que;
            que.push(make_pair(i, j));
            while (!que.empty())
            {
                int tx = que.front().first, ty = que.front().second;
                que.pop();
                for (int i = 0; i < 4; i++)
                {
                    int ttx = tx + dx[i], tty = ty + dy[i];
                    if (0 <= ttx && ttx <= W
                        && 0 <= tty && tty <= H
                        && !fld[ttx][tty])
                    {
                        que.push(make_pair(ttx, tty));
                        fld[ttx][tty] = 1;
                    }
                }
            }
        }
    return ans;
}

void imos(const int W, const int H, const int N)
{
    //imos??????????????´
    memset(fld, 0, sizeof(fld));

    for (int i = 0; i < N; i++)//????????±??????
    {
        fld[X1[i]][Y1[i]]++;
        fld[X1[i]][Y2[i]]--;
        fld[X2[i]][Y1[i]]--;
        fld[X2[i]][Y2[i]]++;
    }
    for (int i = 0; i < W; i++)//?´?????¨????
        for (int j = 1; j < H; j++)
            fld[i][j] += fld[i][j - 1];

    for (int j = 0; j < H; j++)//?´??????????
        for (int i = 1; i < W; i++)
            fld[i][j] += fld[i - 1][j];
    //?????¶??¨????°±????????????????????????
}

Measured memory_bytes: 19176