#ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
#endif
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;
using PII = pair<ll, ll>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())
#define MP make_pair

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

const ll N_MAX = 3100;
ll M[N_MAX][N_MAX];
void reset(){
  rep(i, N_MAX){
    rep(j, N_MAX){
      M[i][j] = 0;
    }
  }
}

int dx[4] = {-1, 0, 1,  0};
int dy[4] = { 0, 1, 0, -1};

void flood_fill(ll y, ll x, ll H, ll W){
  queue<PII> que;
  que.push(MP(y, x));

  while(!que.empty()){
    auto pa = que.front(); que.pop();
    y = pa.first;
    x = pa.second;
    rep(i, 4){
      ll ny = y + dy[i];
      ll nx = x + dx[i];
      if(0<=ny && ny<H && 0<=nx && nx<W && M[ny][nx]==0){
        // in range
        que.push(MP(ny, nx));
        M[ny][nx] = 1;
      }
    }  
  }
}

// 座標圧縮
// 変換テーブルを返す
VI make_compress_table(vector<ll>& A){  
  // 変換表
  auto B = A;
  sort(ALL(B));
  auto it = unique(ALL(B));
  B.erase(it, B.end());
  return B;
}

ll compress_by_table(VI& T, ll v){
  return lower_bound(ALL(T), v) - T.begin();
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll W,H;
    
    while(true){
      cin>>W>>H;
      if(W==0) return 0;

      reset();

      ll n;cin>>n;
      VI X1(n);
      VI X2(n);
      VI Y1(n);
      VI Y2(n);
      rep(i, n){
        ll x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;
        X1[i] = x1;
        X2[i] = x2;
        Y1[i] = y1;
        Y2[i] = y2;
      }

      VI X;
      for(ll x : X1){
        X.push_back(x);
        if(x-1>=0) X.push_back(x-1);
        if(x+1<W) X.push_back(x+1);
      }
      for(ll x : X2){
        x--; // 1ずれてるので
        // あとは同じ
        X.push_back(x);
        if(x-1>=0) X.push_back(x-1);
        if(x+1<W) X.push_back(x+1);
      }

      VI Y;
      for(ll y : Y1){
        Y.push_back(y);
        if(y-1>=0) Y.push_back(y-1);
        if(y+1<H) X.push_back(y+1);
      }
      for(ll y : Y2){
        y--; // 1ずれてるので
        // あとは同じ
        Y.push_back(y);
        if(y-1>=0) Y.push_back(y-1);
        if(y+1<H) X.push_back(y+1);
      }

      VI TX = make_compress_table(X);
      VI TY = make_compress_table(Y);
      
      rep(i, n){
        ll a1 = compress_by_table(TX, X1[i]);
        ll a2 = compress_by_table(TX, X2[i]);
        ll b1 = compress_by_table(TY, Y1[i]);
        ll b2 = compress_by_table(TY, Y2[i]);

        // imos
        FOR(y, b1, b2){
          // x方向はimos
          M[y][a1] += 1;
          M[y][a2] -= 1;
        }
      }

      // imosで塗る
      rep(i, H){
        rep(j, W-1){
          M[i][j+1] += M[i][j];
        }
      }

      // 圧縮された世界でのW, H
      ll W2 = SZ(TX);
      ll H2 = SZ(TY);

      // debug 塗り状態表示
      // rep(i, H2){
      //   rep(j, W2){
      //     cout << M[i][j] << ' ';
      //   }
      //   cout << endl;
      // }

      ll cnt=0;
      rep(i, H2){
        rep(j, W2){
          if(M[i][j]==0){
            flood_fill(i, j, H2, W2);
            cnt++;
          }
        }
      }
      p(cnt);
    }
    
    return 0;
}


Measured memory_bytes: 78396