#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int x[200010], y[200010];
vector<int> row[200010], col[200010];
int parent[200010];
int sz[200100];
char used[200010];
void make_set(int v) {
  parent[v] = v;
  sz[v] = 1;
}
int find_set(int v) {
  if (parent[v] == v) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[a] < sz[b]) swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}
set<int> rows[200010], cols[200010];
int r[200010], c[200010];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> q;
  if (q == 0) {
    cout << n + m - 1 << endl;
    exit(0);
  }
  for (int i = 1; i <= q; i++) {
    cin >> x[i] >> y[i];
    row[x[i]].push_back(i);
    col[y[i]].push_back(i);
    r[x[i]]++;
    c[y[i]]++;
    make_set(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)row[i].size() - 1; j++) {
      union_sets(row[i][j], row[i][j + 1]);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < (int)col[i].size() - 1; j++) {
      union_sets(col[i][j], col[i][j + 1]);
    }
  }
  int cnt = 0;
  for (int i = 1; i <= q; i++) {
    int p = find_set(i);
    if (!used[p]) {
      cnt++;
      used[p] = true;
    }
  }
  for (int i = 1; i <= n; i++)
    if (!r[i]) cnt++;
  for (int j = 1; j <= m; j++)
    if (!c[j]) cnt++;
  cout << cnt - 1 << endl;
}
