#include <bits/stdc++.h>
int r, c, q, E;
int to[200005 * 4], first[200005 * 2], next[200005 * 4];
int scx[200005 * 2];
inline void addedge(int u, int v) {
  to[++E] = v;
  next[E] = first[u];
  first[u] = E;
  to[++E] = u;
  next[E] = first[v];
  first[v] = E;
}
void dfs(int x) {
  int i, y;
  scx[x] = 1;
  for (i = first[x]; i; i = next[i])
    if (!scx[y = to[i]]) dfs(y);
}
int main() {
  int i, u, v, ans = 0;
  scanf("%d%d%d", &r, &c, &q);
  for (i = 1; i <= q; ++i) {
    scanf("%d%d", &u, &v);
    addedge(u, v + r);
  }
  for (i = 1; i <= r + c; ++i)
    if (!scx[i]) dfs(i), ++ans;
  printf("%d\n", --ans);
  return 0;
}
