#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
const vector<long long int> dx = {1, -1, 0, 0};
const vector<long long int> dy = {0, 0, 1, -1};
vector<long long int> ga(long long int n, bool oneIndexed = false) {
  vector<long long int> a;
  if (oneIndexed) a.push_back(0ll);
  for (long long int i = 0; i < n; i++) {
    long long int p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
vector<string> gas(unsigned long long int n, bool oneIndexed = false) {
  vector<string> a;
  if (oneIndexed) a.push_back("");
  for (unsigned long long int i = 0; i < n; i++) {
    string p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a, long long int begin = 0,
        long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
template <typename T, typename A>
void papair(vector<T, A> const &a, long long int begin = 0,
            long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i].first << " " << a[i].second << "\n";
  }
}
vector<long long int> visitedX((long long int)3e5, 0);
vector<long long int> visitedY((long long int)3e5, 0);
vector<vector<long long int> > adjListX(2e5 + 1);
vector<vector<long long int> > adjListY(2e5 + 1);
long long int edgeCount = 0;
void dfs(long long int curr, long long int prev, bool isX) {
  if (isX) {
    visitedX[curr] = 1;
  } else {
    visitedY[curr] = 1;
  }
  const vector<long long int> &neighs = (isX ? adjListX[curr] : adjListY[curr]);
  for (auto neigh : neighs) {
    if (neigh == prev) continue;
    edgeCount++;
    if ((isX ? !visitedY[neigh] : !visitedX[neigh])) {
      dfs(neigh, curr, !isX);
    }
  }
}
void solve() {
  long long int n, m, q;
  cin >> n >> m >> q;
  for (long long int i = 0; i < q; i++) {
    long long int x, y;
    cin >> x >> y;
    adjListX[x].push_back(y);
    adjListY[y].push_back(x);
  }
  long long int x = 1, c = 0;
  while (x <= n) {
    if (!visitedX[x]) {
      dfs(x, -1, true);
      c++;
    }
    x++;
  }
  long long int y = 1;
  while (y <= m) {
    if (!visitedY[y]) c++;
    y++;
  }
  cout << c - 1 << "\n";
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
