#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};


void dfs(int i, int j, vector<vector<int>> &grid){
    grid[i][j] = 1;
    int w = grid.size();
    int h = grid[0].size();
    for(int itr = 0; itr < 4; itr++){
        int ni = i + di[itr];
        int nj = j + dj[itr];
        if(ni < 0 or w <= ni or nj < 0 or h <= nj) continue;
        if(grid[ni][nj] > 0) continue;
        dfs(ni, nj, grid);
    }
}


signed main(){

    while(1){
    vector<int> X;
    vector<int> Y;
    int w, h; cin >> w >> h;
    if(w == 0) break;
    int n; cin >> n;
    vector<int> sx(n);
    vector<int> sy(n);
    vector<int> tx(n);
    vector<int> ty(n);
    for(int i = 0; i < n; i++){
        cin >> sx[i] >> sy[i] >> tx[i] >> ty[i];
        X.push_back(sx[i]);
        X.push_back(tx[i]);
        Y.push_back(sy[i]);
        Y.push_back(ty[i]);
        X.push_back(sx[i] - 1);
        X.push_back(tx[i] + 1);
        Y.push_back(sy[i] - 1);
        Y.push_back(ty[i] + 1);
    }

    X.push_back(0);
    X.push_back(w);
    Y.push_back(0);
    Y.push_back(h);
    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());

    X.erase(unique(X.begin(), X.end()), X.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());
    
    /*
    cout << "X" << endl;
    for(int i = 0; i < X.size(); i++){
        cout << X[i] << endl;
    }

    cout << "Y" << endl;
    for(int i = 0; i < Y.size(); i++){
        cout << Y[i] << endl;
    }
    */
    
    int mh = lower_bound(Y.begin(), Y.end(), h) - Y.begin();
    int mw = lower_bound(X.begin(), X.end(), w) - X.begin();
    
    vector<int> msx(n);
    vector<int> msy(n);
    vector<int> mtx(n);
    vector<int> mty(n);
    for(int i = 0; i < n; i++){
        msx[i] = lower_bound(X.begin(), X.end(), sx[i]) - X.begin();
        mtx[i] = lower_bound(X.begin(), X.end(), tx[i]) - X.begin();
        
        msy[i] = lower_bound(Y.begin(), Y.end(), sy[i]) - Y.begin();
        mty[i] = lower_bound(Y.begin(), Y.end(), ty[i]) - Y.begin();
    }
    /*
    cout << mw << " " << mh << endl;
    for(int i = 0; i < n; i++){
        cout << msx[i] << " " << msy[i] << " " << msy[i] << " " << mty[i] << endl;
    }
    */
    
    // 1 : wall
    vector<vector<int>> grid(mw + 1, vector<int> (mh + 1, 0));
    for(int i = 0; i < n; i++){
       
        grid[msx[i]][msy[i]]++;
        grid[mtx[i]][mty[i]]++;
        grid[msx[i]][mty[i]]--;
        grid[mtx[i]][msy[i]]--;
        
        //cout << msx[i] << " " << msy[i] << " " << msy[i] << " " << mty[i] << endl;
    }
    
    /*
    cerr << "grid imosmaeyade" << endl;
    for(int i = 0; i < mw; i++){
        for(int j = 0; j < mh; j++){
            cerr << grid[i][j] << " ";
        }
        cout << endl;
    }
    */


    for(int i = 0; i <= mh; i++){
        for(int j = 0; j < mw; j++) grid[j + 1][i] += grid[j][i];
    }

    for(int j = 0; j <= mw; j++){
        for(int i = 0; i < mh; i++){
            grid[j][i + 1] += grid[j][i];
        }
    }
    
    for(int i = 0; i <= mw; i++){
        grid[i][0] = 1;
        grid[i][mh] = 1;
    }

    for(int j = 0; j <= mh; j++){
        grid[0][j] = grid[mw][j] = 1;
    }

    /*
    cerr << "grid maeyade" << endl;
    for(int i = 0; i < mw; i++){
        for(int j = 0; j < mh; j++){
            cerr << grid[i][j] << " ";
        }
        cout << endl;
    }
    */

    int ans = 0;
    for(int i = 0; i < mw; i++){
        for(int j = 0; j < mh; j++){
            if(grid[i][j] > 0) continue;
            ans++;
            dfs(i, j, grid);
        }
    }
    /*
    cerr << "grid " << endl;
    for(int i = 0; i < mw; i++){
        for(int j = 0; j < mh; j++){
            cerr << grid[i][j] << " ";
        }
        cout << endl;
    }*/

    cout << ans << endl;
    }
    return 0;
}



Measured memory_bytes: 40580