#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using ul = uint64_t;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pi>;
static constexpr int inf = (int)1e9 + 5;
static constexpr ll infl = (ll)1e18 + 5;
static mt19937 rng((uint32_t)chrono::duration_cast<chrono::nanoseconds>(
                       chrono::high_resolution_clock::now().time_since_epoch())
                       .count());
vi vis;
vvi adj;
void dfs(int v) {
  for (auto& e : adj[v]) {
    if (!vis[e]) vis[e] = true, dfs(e);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  int n, m, q;
  cin >> n >> m >> q;
  vpi s(q);
  for (auto& si : s) cin >> si.first >> si.second;
  if (n == 1 || m == 1) {
    cout << n * m - q << '\n';
    return 0;
  }
  adj.resize(n + m);
  vis.resize(n + m);
  for (auto& si : s)
    adj[si.first - 1].push_back(si.second - 1 + n),
        adj[si.second + n - 1].push_back(si.first - 1);
  int cnt = 0;
  for (int i = 0; i < (n + m); ++i)
    if (!vis[i]) vis[i] = true, cnt++, dfs(i);
  cout << cnt - 1;
  return 0;
}
