#include <bits/stdc++.h>
using namespace std;
int n, m, q, i, j, k, l, r, c, rn, cn, p;
vector<int> ri, ci;
vector<vector<int> > rx, cx;
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (r = 0; r < n; r++) ri.push_back(-1);
  for (c = 0; c < m; c++) ci.push_back(-1);
  for (i = 0; i < q; i++) {
    scanf("%d%d", &r, &c);
    r--;
    c--;
    if (ri[r] == -1 && ci[c] == -1) {
      j = rx.size();
      ri[r] = j;
      ci[c] = j;
      rx.push_back(vector<int>());
      rx[j].push_back(r);
      cx.push_back(vector<int>());
      cx[j].push_back(c);
    } else if (ri[r] == -1) {
      j = ci[c];
      ri[r] = j;
      rx[j].push_back(r);
    } else if (ci[c] == -1) {
      j = ri[r];
      ci[c] = j;
      cx[j].push_back(c);
    } else {
      j = ri[r];
      k = ci[c];
      if (k == j) continue;
      rn = rx[j].size() + cx[j].size();
      cn = rx[k].size() + cx[k].size();
      if (rn > cn) {
        l = k;
        k = j;
        j = l;
      }
      rx[k].insert(rx[k].end(), rx[j].begin(), rx[j].end());
      cx[k].insert(cx[k].end(), cx[j].begin(), cx[j].end());
      for (r = 0; r < rx[j].size(); r++) ri[rx[j][r]] = k;
      for (c = 0; c < cx[j].size(); c++) ci[cx[j][c]] = k;
      rx[j].clear();
      cx[j].clear();
    }
  }
  p = -1;
  for (i = 0; i < rx.size(); i++) p += rx[i].size() > 0;
  for (r = 0; r < n; r++) p += ri[r] == -1;
  for (c = 0; c < m; c++) p += ci[c] == -1;
  printf("%d\n", p);
}
