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

const int N = 1e3 + 5;
int m, n, k;
int b[N][N]; 
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
const int INF = 1e9 + 5;

void loang(int x, int y, int val) {
    queue<pair<int, int>> q;
    q.push({x, y});
    b[x][y] = INF; // Mark the cell as visited
    while (!q.empty()) {
        auto [cx, cy] = q.front(); q.pop();
        for (int d = 0; d < 4; ++d) {
            int nx = cx + dx[d];
            int ny = cy + dy[d];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && b[nx][ny] == val) {
                b[nx][ny] = INF; // Mark as visited
                q.push({nx, ny});
            }
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("cake.inp", "r", stdin);
    freopen("cake.out", "w", stdout);
    
    while (true) {
        cin >> m >> n;
        if (m == 0 && n == 0) break;

        cin >> k;
        memset(b, 0, sizeof(b)); // Initialize the grid to 0

        for (int i = 1; i <= k; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            // Fill the rectangle
            for (int x = x1; x < x2; ++x) {
                for (int y = y1; y < y2; ++y) {
                    b[x][y] += (1 << (i - 1)); // Use i-1 to set the correct bit
                }
            }
        }
        
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (b[i][j] != INF && b[i][j] != 0) { // Ensure it's not visited and not empty
                    ans++;
                    loang(i, j, b[i][j]);
                }
            }
        }
        cout << ans << "\n"; // Output the count of distinct regions
    }

    return 0;
}
