#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 2;
int n, m, q, ans, p[N], sz[N];
pair<int, int> a[N];
int f_s(int v) { return (p[v] == v) ? v : p[v] = f_s(p[v]); }
void u_s(int a, int b) {
  a = f_s(a);
  b = f_s(b);
  if (a != b) {
    if (sz[a] < sz[b]) swap(a, b);
    p[b] = a;
    sz[a] += sz[b];
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n + m; i++) p[i] = i, sz[i] = 1;
  for (int i = 1; i <= q; i++) {
    cin >> a[i].first >> a[i].second;
    a[i].second += n;
    u_s(a[i].first, a[i].second);
  }
  for (int i = 1; i <= n + m; i++) {
    if (p[i] == i) ans++;
  }
  cout << ans - 1 << endl;
}
