#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int D = (int)2e5 + 9;
vector<int> d1[D], d2[D];
bool v1[D], v2[D];
int o = 0;
void dfs(int node, int l) {
  if (l == 0) {
    if (v1[node] == true) return;
    v1[node] = true;
    o = 1;
    for (auto x : d1[node]) {
      dfs(x, 1);
    }
  } else {
    if (v2[node] == true) return;
    v2[node] = true;
    o = 1;
    for (auto x : d2[node]) {
      dfs(x, 0);
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n, m, q;
  cin >> n >> m >> q;
  int r, c;
  for (int i = 0; i < q; i++) {
    cin >> r >> c;
    d1[r].push_back(c);
    d2[c].push_back(r);
  }
  int co = 0;
  for (int i = 1; i <= n; i++) {
    o = 0;
    dfs(i, 0);
    co += o;
  }
  for (int i = 1; i <= m; i++) {
    o = 0;
    dfs(i, 1);
    co += o;
  }
  cout << co - 1;
  return 0;
}
