#include <bits/stdc++.h>
using namespace std;
void dfs(int root, vector<vector<int> > &graph, vector<bool> &vis) {
  vis[root] = true;
  for (int i = 0; i < graph[root].size(); i++) {
    if (!vis[graph[root][i]]) {
      dfs(graph[root][i], graph, vis);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, q;
  cin >> n >> m >> q;
  vector<vector<int> > graph(n + m + 1);
  int r, c;
  while (q--) {
    cin >> r >> c;
    graph[r].push_back(c + n);
    graph[c + n].push_back(r);
  }
  int connectedComp = 0;
  vector<bool> vis(n + m + 1, false);
  for (int i = 1; i <= n + m; i++) {
    if (!vis[i]) {
      dfs(i, graph, vis);
      connectedComp++;
    }
  }
  cout << connectedComp - 1 << "\n";
  return 0;
}
