#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int Maxn = 202020;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
struct pos {
  int first, second, third;
};
int N, M, Q;
bool vis[2 * Maxn];
vector<int> conn[2 * Maxn];
bool dfs(int a) {
  if (vis[a]) return false;
  vis[a] = true;
  for (int(i) = 0; (i) < (int)(conn[a].size()); i++) dfs(conn[a][i]);
  return true;
}
int main() {
  scanf("%d%d%d", &N, &M, &Q);
  for (int(i) = 0; (i) < (int)(Q); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u += Maxn;
    conn[u].push_back(v);
    conn[v].push_back(u);
  }
  int cnt = -1;
  for (int(i) = 1; (i) <= (int)(N); i++)
    if (dfs(Maxn + i)) cnt++;
  for (int(i) = 1; (i) <= (int)(M); i++)
    if (dfs(i)) cnt++;
  printf("%d\n", cnt);
  return 0;
}
