#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}


template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
  auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
  initializer_list<int>{(void(append(ts)),0)...};
  return bs;
}


template<typename T>
vector<T> shift(vector<T> vs,vector<T> as){
  assert(vs.size()==as.size());
  for(int i=0;i<(int)vs.size();i++) vs[i]+=as[i];
  return vs;
}
template<typename T>
vector<T> shift(vector<T> vs,T a){
  return shift(vs,vector<T>(vs.size(),a));
}

template<typename T, typename ...Ts>
vector<T> near(vector<T> vs,Ts... ts){
  vector<T> rs;
  rs.reserve(vs.size()*sizeof...(ts));
  auto append=[&](auto a){
    auto ws=shift(vs,a);
    for(auto w:ws) rs.emplace_back(w);
  };
  initializer_list<int>{(void(append(ts)),0)...};
  return rs;
}


template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int w,h;
  while(cin>>w>>h,w){
    int k;
    cin>>k;
    vector<int> x1(k),y1(k),x2(k),y2(k);
    for(int i=0;i<k;i++) cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];

    auto vx=fusion(near(x1,-1,0,1),near(x2,-1,0,1));
    auto vy=fusion(near(y1,-1,0,1),near(y2,-1,0,1));
    vx.emplace_back(0);
    vx.emplace_back(w);
    vy.emplace_back(0);
    vy.emplace_back(h);

    vx=compress(vx);
    vy=compress(vy);
    while(vx.front()<0) vx.erase(vx.begin());
    while(vy.front()<0) vy.erase(vy.begin());
    while(vx.back() >w) vx.pop_back();
    while(vy.back() >h) vy.pop_back();

    auto dx=dict(vx);
    auto dy=dict(vy);

    int n=vy.size();
    int m=vx.size();

    auto dp=make_v<int>(n,m);
    fill_v<int>(dp,0);

    for(int i=0;i<k;i++){
      dp[dy[y1[i]]][dx[x1[i]]]++;
      dp[dy[y1[i]]][dx[x2[i]]]--;
      dp[dy[y2[i]]][dx[x1[i]]]--;
      dp[dy[y2[i]]][dx[x2[i]]]++;
    }

    for(int i=0;i<n;i++)
      for(int j=0;j+1<m;j++)
        dp[i][j+1]+=dp[i][j];
    for(int i=0;i+1<n;i++)
      for(int j=0;j<m;j++)
        dp[i+1][j]+=dp[i][j];

    int ans=0;
    auto bfs=[&](int sy,int sx){
      ans++;
      using P = pair<int, int>;
      queue<P> que;
      dp[sy][sx]=1;
      que.emplace(sy,sx);

      int dy[]={0,0,1,-1};
      int dx[]={1,-1,0,0};
      auto in=[&](int y,int x){
        return 0<=y and y+1<n and 0<=x and x+1<m;
      };

      while(!que.empty()){
        int y,x;
        tie(y,x)=que.front();que.pop();
        for(int k=0;k<4;k++){
          int ny=y+dy[k],nx=x+dx[k];
          if(!in(ny,nx)) continue;
          if(dp[ny][nx]) continue;
          dp[ny][nx]=1;
          que.emplace(ny,nx);
        }
      }
    };

    for(int i=0;i+1<n;i++)
      for(int j=0;j+1<m;j++)
        if(!dp[i][j]) bfs(i,j);

    cout<<ans<<newl;
  }
  return 0;
}



Measured memory_bytes: 10044