#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = (((1 << 30) - 1) << 1) + 1;
const int nINF = 1 << 31;
const int N = 2e5 + 1;
int n, m, q, visr[N], visc[N], r[N], c[N];
vector<int> gr[N], gc[N];
void dfs(int row, int col, int fr) {
  visr[row] = 1;
  visc[col] = 1;
  if (fr != 1) {
    for (int x : gr[row]) {
      if (!visc[x]) dfs(row, x, 1);
    }
  }
  if (fr != 2) {
    for (int x : gc[col]) {
      if (!visr[x]) dfs(x, col, 2);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  memset(visr, 0, sizeof(visr));
  memset(visc, 0, sizeof(visc));
  cin >> n >> m >> q;
  for (int i = 0; i < (q); i++) {
    cin >> r[i] >> c[i];
    gr[r[i]].push_back(c[i]);
    gc[c[i]].push_back(r[i]);
  }
  int ans = 0, g = 0;
  for (int i = 0; i < (q); i++) {
    if (!visr[r[i]] || !visc[c[i]]) {
      dfs(r[i], c[i], 0);
      ++g;
    }
  }
  ans = g - 1;
  for (int i = (1); i <= (n); i++)
    if (!visr[i]) ++ans;
  for (int i = (1); i <= (m); i++)
    if (!visc[i]) ++ans;
  cout << ans << endl;
  return 0;
}
