#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (T e : v) {
    os << e << ' ';
  }
  return os;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, q;
  cin >> n >> m;
  cin >> q;
  vector<int> L[n], R[m];
  vector<bool> Lv(n), Rv(m);
  for (int i = 0; i < q; ++i) {
    int r, c;
    cin >> r >> c;
    L[r - 1].emplace_back(c - 1);
    R[c - 1].emplace_back(r - 1);
  }
  function<bool(int, bool)> dfs = [&](int u, bool left) {
    auto& edges = left ? L[u] : R[u];
    auto& visited = left ? Lv : Rv;
    if (visited[u]) {
      return false;
    }
    visited[u] = true;
    for (int v : edges) {
      dfs(v, !left);
    }
    return true;
  };
  int cx = 0;
  for (int i = 0; i < n; ++i) {
    if (dfs(i, true)) {
      cx++;
    }
  }
  for (int i = 0; i < m; ++i) {
    if (dfs(i, false)) {
      cx++;
    }
  }
  cout << cx - 1 << endl;
  return 0;
}
