#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int parent[maxn];
int find_parent(int x) {
  if (parent[x] == x) return x;
  return parent[x] = find_parent(parent[x]);
}
int main() {
  int n, m, q, r, c, x, y;
  scanf("%d%d%d", &n, &m, &q);
  int i;
  for (i = 1; i <= n + m; i++) parent[i] = i;
  while (q--) {
    scanf("%d%d", &r, &c);
    x = find_parent(parent[r]);
    y = find_parent(parent[c + n]);
    if (x < y)
      parent[y] = parent[x];
    else if (y < x)
      parent[x] = parent[y];
  }
  int ans = 0;
  for (i = 1; i <= n + m; i++) {
    if ((parent[i] = find_parent(parent[i])) == i) ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
