#include <bits/stdc++.h>
// #include "ane.cpp"

const int INF  = 1e9;
const long long INFLL = 1e18;
const int NMAX = 1005;
const int MMAX = 100005;
const int KMAX = 1005;
const int MOD  = 1e9 + 7;
using namespace std;

// comment to disable debug functions
#define DEBUG

// frequently used macros

#if __cplusplus >= 201103L
#define ALL(v) begin(v),end(v)
#define SORT(v) sort(begin(v), end(v))
#define FIND(v,x) find(begin(v), end(v), (x))
#else
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(v.begin(), v.end())
#define FIND(v,x) find(v.begin(), v.end(), (x))
#endif

#define MEMNEXT(from, to) do{ memmove((to), (from), sizeof(from)); \
memset((from), 0, sizeof(from)); } while(0)
#ifdef DEBUG
#define DUMP(x) do{ std::cerr << (#x) << ": " << x << std::endl; }while(0)
#else
#define DUMP(x) do{}while(0)
#endif

// frequent used aliases
typedef long long ll;
typedef pair<int, int> p;
typedef pair<ll, int> lp;
typedef pair<ll, ll> llp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<vec> mat;
typedef vector<vecll> matll;

// frequently used constants
static const int di[] = {1, 0, -1, 0};
static const int dj[] = {0, 1, 0, -1};

// frequently used structs
struct edge{
  int to,cost;
};

// printf for debug
#ifndef DEBUG
void debug(const char* format, ...){}
#else
void debug(const char* format, ...){
  va_list arg;
  va_start(arg, format);
  vprintf(format, arg);
  va_end(arg);
}
#endif

// dump vector
#ifdef DEBUG
#define DUMPV(v, c) do{       \
printf("%s: ", #v);         \
for (int i = 0; i < (c); ++i) \
  {                           \
    cout << (v)[i] << " ";      \
  }                           \
  cout << endl;               \
} while(0)
#else
#define DUMPV(v,c)
#endif

// std::fill of multi dimensions
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
  std::fill( (T*)array, (T*)(array+N), val );
}

// binary search
ll BSearch(ll _begin, ll _end, bool (*f)(int)){
  ll mid;
  while(_end - _begin > 1LL) {
    mid = (_begin + _end) / 2LL;
    if(f(mid)) {
      debug("BSearch: f(%d) == true\n", mid);
      _end = mid;
    }
    else
    {
      debug("BSearch: f(%d) == false\n", mid);
      _begin = mid;
    }
  }
  return _end;
}


ll W,H,N,M,K,A,B,C,D,E;
ll X1[NMAX], X2[NMAX], Y1[NMAX], Y2[NMAX];
int dat[NMAX * 2][NMAX * 2] = {};
bool used[NMAX * 2][NMAX * 2] = {};
ll ans;
vector<ll> xs,ys;

void compress(vector<ll> &xs, map<ll, ll> &mp){
  SORT(xs);
  xs.erase(unique(ALL(xs)), xs.end());
  for (int i = 0; i < xs.size(); ++i)
  {
    mp[xs[i]] = i;
  }
}
void bfs(int x, int y, const ll HH, const ll WW){
  queue<p> que;
  que.push(p{x,y});
  while(!que.empty()) {
    int xx = que.front().first;
    int yy = que.front().second;
    que.pop();
    if(used[yy][xx]) continue;
    used[yy][xx] = true;
    for (int k = 0; k < 4; ++k)
    {
      if (yy + di[k] >= 0 && xx + dj[k] >= 0 &&
          yy + di[k] < HH && xx + dj[k] < WW &&
        dat[yy+di[k]][xx+dj[k]] == 0 && !used[yy+di[k]][xx+dj[k]])
      {
        que.push(p{xx+dj[k], yy + di[k]});
      }
    }
  }
}
void solve(){
  // main algorithm
  // coordinate compression
  // chrono::steady_clock::time_point
  //   start, after_compress, after_tape, after_bfs;

  // start = chrono::steady_clock::now();
  map<ll, ll> x_map, y_map;
  compress(xs, x_map);
  compress(ys, y_map);

  // after_compress = chrono::steady_clock::now();

  // debug("x_map:\n");
  // for(auto&& p : x_map) {
  //   debug("%lld -> %lld\n", p.first, p.second);
  // }
  // debug("y_map:\n");
  // for(auto&& p : y_map) {
  //   debug("%lld -> %lld\n", p.first, p.second);
  // }

  // fill tape
  #define X(x) (x_map[x])
  #define Y(y) (y_map[y])
  Fill(dat, 0);
  for (int i = 0; i < N; ++i)
  {
    ll x1 = X(X1[i]), x2 = X(X2[i]),
    y1 = Y(Y1[i]), y2 = Y(Y2[i]);
    for (int y = y1; y < y2; ++y)
    {
      dat[y][x1] += 1;
      dat[y][x2] -= 1;
    }
  }
  for (int y = 0; y < Y(H); ++y)
  {
    for (int x = 0; x < X(W); ++x)
    {
      dat[y][x] = (x == 0 ? 0 : dat[y][x-1]) + dat[y][x];
    }
  }

  // after_tape = chrono::steady_clock::now();

  // debug("dat:\n");
  // for (int y = Y(H) - 1; y >= 0; --y)
  // {
  //   for (int x = 0; x < X(W); ++x)
  //   {
  //     debug("%c", (dat[y][x] == 0 ? '.' : 'X'));
  //   }
  //   debug("\n");
  // }

  // count space
  ans = 0;
  Fill(used, false);
  for (int y = 0; y < Y(H); ++y)
  {
    for (int x = 0; x < X(W); ++x)
    {
      if (dat[y][x] == 0 && !used[y][x])
      {
        ans++;
        // debug("ans++: dat[%d][%d] == 0\n", y, x);
        bfs(x, y, Y(H), X(W));
      }
    }
  }

  // after_bfs = chrono::steady_clock::now();

  // printf("compress: %d ms\n",
  //   chrono::duration_cast<chrono::milliseconds>(after_compress - start).count());
  // printf("tape: %d ms\n",
  //   chrono::duration_cast<chrono::milliseconds>(after_tape - after_compress).count());
  // printf("bfs: %d ms\n",
  //   chrono::duration_cast<chrono::milliseconds>(after_bfs - after_tape).count());

  #undef X
  #undef Y
}
void debug(){
  // output debug information
}
void answer(){
  // output answer
  cout << ans << endl;
}
int main(int argc, char const *argv[])
{
  auto start = chrono::steady_clock::now();
  decltype(start) end;
  // operate inputs

  // Fill(dp, -1);
  xs.reserve(2 * NMAX);
  ys.reserve(2 * NMAX);
  while(true){
    cin >> W >> H;
    if(!(W | H)) break;
    cin >> N;
    xs.clear();
    ys.clear();
    xs.push_back(0);
    ys.push_back(0);
    for (int i = 0; i < N; ++i)
    {
      scanf("%lld%lld%lld%lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
      xs.push_back(X1[i]);
      ys.push_back(Y1[i]);
      xs.push_back(X2[i]);
      ys.push_back(Y2[i]);
    }
    xs.push_back(W);
    ys.push_back(H);
    solve();
  #ifdef DEBUG
    debug();
  #endif
    answer();
  }
  // end = chrono::steady_clock::now();
  // printf("overall: %d ms\n",
  //   chrono::duration_cast<chrono::milliseconds>(end - start).count());
  return 0;
}

Measured memory_bytes: 23044