#include <bits/stdc++.h>
using namespace std;
class unionfind {
 private:
  vector<int> rank;
  vector<int> parent;

 public:
  unionfind(int count_element);
  int findInst(int e);
  void unionInst(int e1, int e2);
};
bool check[(400005)];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M, Q;
  cin >> N >> M >> Q;
  unionfind uf(N + M);
  for (int i = 0; i < (400005); i++) check[i] = false;
  for (int q = 0; q < Q; q++) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    uf.unionInst(r, N + c);
  }
  int count = 0;
  for (int i = 0; i < N + M; i++) {
    int parent = uf.findInst(i);
    if (!check[parent]) {
      count++;
      check[parent] = true;
    }
  }
  cout << count - 1;
}
void unionfind::unionInst(int e1, int e2) {
  int p1 = findInst(e1);
  int p2 = findInst(e2);
  if (p1 == p2) return;
  if (rank[p1] < rank[p2])
    parent[p2] = p1;
  else {
    parent[p1] = p2;
    if (rank[p1] == rank[p2]) rank[p2]++;
  }
}
int unionfind::findInst(int e) {
  if (e != parent[e]) parent[e] = findInst(parent[e]);
  return parent[e];
}
unionfind::unionfind(int count_element) {
  rank.assign(count_element, int(0));
  parent.assign(count_element, int(0));
  for (int i = 0; i < count_element; i++) {
    rank[i] = 0;
    parent[i] = i;
  }
}
