#include <bits/stdc++.h>
using namespace std;
const int MAXN = 201010;
const int MAXM = 201010;
int n, m;
int cnt;
int id[MAXN + MAXM];
int sz[MAXN + MAXM];
inline int getRow(int r) { return r; }
inline int getCol(int c) { return n + c; }
void setup(int total) {
  cnt = total;
  for (int i = 0; i < total; ++i) {
    id[i] = i;
    sz[i] = 1;
  }
}
int find(int x) {
  if (id[x] == x) return x;
  return id[x] = find(id[x]);
}
void merge(int x, int y) {
  int i = find(x);
  int j = find(y);
  if (i == j) return;
  if (sz[i] > sz[j]) swap(i, j);
  id[i] = j;
  sz[j] += sz[i];
  --cnt;
}
int main() {
  ios_base ::sync_with_stdio(false);
  int q;
  cin >> n >> m >> q;
  setup(n + m);
  for (int r, c, i = 0; i < q; i++) {
    cin >> r >> c;
    merge(getRow(r), getCol(c));
  }
  cout << cnt - 1 << "\n";
  return 0;
}
