#include <bits/stdc++.h>
using namespace std;
long long y = 200005;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void mad(long long &a, long long b) {
  a = (a + b) % 1000000007;
  if (a < 0) a += 1000000007;
}
long long gcd(long long a, long long b) {
  if (a > b) swap(a, b);
  if (!a) return b;
  return gcd(b % a, a);
}
vector<long long int> ed[400010];
long long cx, cy = 1;
long long n, m, q;
int vis[400005];
void dfs(int i) {
  if (vis[i]) return;
  vis[i] = 1;
  for (int j : ed[i]) dfs(j);
}
void solve() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;
    ed[u].push_back(y + v);
    ed[y + v].push_back(u);
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    dfs(i);
    cx++;
  }
  for (int j = 1; j <= m; j++)
    if (!vis[y + j]) cy++;
  cout << cx + cy - 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
}
