#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n, m, q;     cin >> n >> m >> q;     vector<vector<int>> r(n), c(m);     for (int i = 0; i < q; ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       r[x].push_back(y);       c[y].push_back(x);     }     if (n == 1) {       cout << m - q;       return 0;     }     if (m == 1) {       cout << n - q;       return 0;     }     vector<char> used_row(n, false);     vector<char> used_col(m, false);     int ans = 0;     vector<int> cols_to_add(r[0]), next_cols_to_add;     used_row[0] = true;     for (int i = 0; i < m; ++i) {       if (used_col[i]) continue;       while (!cols_to_add.empty()) {         next_cols_to_add.clear();         for (int col : cols_to_add) {           if (used_col[col]) continue;           used_col[col] = true;           for (int row : c[col]) {             if (used_row[row]) continue;             used_row[row] = true;             for (int new_col : r[row]) {               next_cols_to_add.push_back(new_col);             }           }         }         cols_to_add = next_cols_to_add;       }       if (!used_col[i]) {         ++ans;         cols_to_add.push_back(i);       }     }     while (!cols_to_add.empty()) {       next_cols_to_add.clear();       for (int col : cols_to_add) {         if (used_col[col]) continue;         used_col[col] = true;         for (int row : c[col]) {           if (used_row[row]) continue;           used_row[row] = true;           for (int new_col : r[row]) {             next_cols_to_add.push_back(new_col);           }         }       }       cols_to_add = next_cols_to_add;     }     for (int i = 1; i < n; ++i) {       if (!used_row[i]) {         ++ans;       }     }     cout << ans;   }   