#include <bits/stdc++.h>
using namespace std;
vector<int> a[200005], b[200005];
int f[200005];
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
void uni(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  f[fx] = fy;
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
    b[y].push_back(x);
  }
  if (n == 1 || m == 1) {
    long long ans = n;
    ans *= m;
    ans -= q;
    cout << ans << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) f[i] = i;
  long long ans = 0;
  for (int i = 1; i <= m; i++) {
    if (b[i].size() == 0) {
      ans++;
    } else {
      int x = b[i][0];
      for (int j = 1; j < b[i].size(); j++) {
        uni(b[i][j], x);
      }
    }
  }
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(find(i));
  ans += (int)s.size() - 1;
  cout << ans << endl;
}
