#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m, q;
pair<int, int> arr[N];
int h_sum[N], v_sum[N];
vector<int> h[N], v[N];
bool used[N];
bool h_used[N], v_used[N];
void dfs(int u) {
  used[u] = true;
  pair<int, int>& p = arr[u];
  if (!h_used[p.first]) {
    h_used[p.first] = true;
    for (int i = 0; i < h[p.first].size(); ++i)
      if (!used[h[p.first][i]]) dfs(h[p.first][i]);
  }
  if (!v_used[p.second]) {
    v_used[p.second] = true;
    for (int i = 0; i < v[p.second].size(); ++i)
      if (!used[v[p.second][i]]) dfs(v[p.second][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m >> q;
  if (q == 0) {
    cout << n + m - 1 << endl;
    return 0;
  }
  for (int i = 0; i < q; ++i) {
    int r, c;
    cin >> r >> c;
    --r;
    --c;
    ++h_sum[r];
    ++v_sum[c];
    h[r].push_back(i);
    v[c].push_back(i);
    arr[i] = make_pair(r, c);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (h_sum[i] == 0) ++ans;
  for (int i = 0; i < m; ++i)
    if (v_sum[i] == 0) ++ans;
  for (int i = 0; i < q; ++i) {
    if (used[i]) continue;
    ++ans;
    dfs(i);
  }
  --ans;
  cout << ans << endl;
}
