#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> l2r(200000);
vector<vector<int>> r2l(200000);
int cnt = 0;
bool lused[200000] = {};
bool rused[200000] = {};
void dfs(short direction, int v) {
  if (direction == 0) {
    lused[v] = true;
    for (int x : l2r[v])
      if (!rused[x]) dfs(1, x);
  } else {
    rused[v] = true;
    for (int x : r2l[v])
      if (!lused[x]) dfs(0, x);
  }
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> col(n), row(m);
  int x, y;
  for (int i = 0; i < q; ++i) {
    cin >> x >> y;
    x--;
    y--;
    l2r[x].push_back(y);
    r2l[y].push_back(x);
  }
  for (int i = 0; i < n; ++i)
    if (!lused[i]) {
      cnt++;
      dfs(0, i);
    }
  for (int i = 0; i < m; ++i)
    if (!rused[i]) {
      cnt++;
      dfs(1, i);
    }
  cout << cnt - 1 << endl;
  return 0;
}
