#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
int fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i <= n + m; i++) fa[i] = i;
  for (int i = 1; i <= q; i++) {
    int r, c;
    cin >> r >> c;
    int t1 = find(r), t2 = find(c + n);
    fa[t1] = t2;
  }
  int ans = 0;
  for (int i = 1; i <= n + 1 + m; i++) {
    if (find(i) == i) ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
