#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
vector<int> adj[400005];
bool vis[400005];
void dfs(int i) {
  vis[i] = 1;
  for (auto v : adj[i]) {
    if (!vis[v]) dfs(v);
  }
}
void solve() {
  int n, m, q;
  cin >> n >> m >> q;
  while (q--) {
    int r, c;
    cin >> r >> c;
    r--;
    c += (200000 - 1);
    adj[r].push_back(c);
    adj[c].push_back(r);
  }
  int c = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i]) c++, dfs(i);
  for (int i = 0; i < m; i++)
    if (!vis[i + 200000]) c++, dfs(i);
  cout << c - 1;
}
int main() {
  fastio();
  solve();
  return 0;
}
