#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
class DSU {
 public:
  vector<int> p;
  int n;
  int find(int x) {
    if (p[x] != x) {
      p[x] = find(p[x]);
      return p[x];
    } else
      return x;
  }
  void join(int x, int y) {
    int a = find(x), b = find(y);
    if (a == b) return;
    p[b] = p[a];
  }
  int count() {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      ret += p[i] == i;
    }
    return ret;
  }
  DSU(int n) {
    this->n = n;
    p.resize(n);
    for (int i = 0; i < n; i++) {
      p[i] = i;
    }
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  DSU *row = new DSU(n), *col = new DSU(m);
  vector<set<int>> r(n), c(m);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    r[x].insert(y);
    c[y].insert(x);
  }
  int blank = 0, blank2 = 0;
  for (int i = 0; i < n; i++) blank += r[i].empty();
  for (int i = 0; i < m; i++) blank2 += c[i].empty();
  if (blank < blank2) swap(blank, blank2);
  for (int i = 0; i < n; i++) {
    if (r[i].size() < 2) continue;
    auto p = *r[i].begin();
    for (auto it : r[i]) {
      col->join(it, p);
    }
  }
  for (int i = 0; i < m; i++) {
    if (c[i].size() < 2) continue;
    auto p = *c[i].begin();
    for (auto it : c[i]) {
      row->join(it, p);
    }
  }
  long long x = row->count() - 1, y = col->count() - 1;
  long long ret = blank;
  long long p = blank - blank2;
  for (int i = 0; i < ret - blank2; i++) {
    if (x > y)
      x--;
    else
      y--;
  }
  cout << blank + max(x, y) << "\n";
  return 0;
}
