#include #include using namespace std; using ll = long long; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int h, w, n; cin >> h >> w >> n; atcoder::dsu uf(h + w); vector a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; uf.merge(a[i], b[i] + h); } int cur = 0; vector> dp(2, vector(w + 1, 1e9)), ep(2, vector(w + 1, -1e9)); dp[cur][0] = ep[cur][0] = 0; const auto G = uf.groups(); const int K = G.size(); for (int i = 0; i < K; i++) { int nxt = 1 - cur; for (int j = 0; j <= w; j++) dp[nxt][j] = dp[cur][j], ep[nxt][j] = ep[cur][j]; int r = 0, c = 0; for (int x : G[i]) { r += x < h; c += x >= h; } for (int j = 0; j <= w; j++) { if (dp[cur][j] != 1e9) { dp[nxt][j + c] = min(dp[nxt][j + c], dp[cur][j] + r); } if (ep[cur][j] != -1e9) { ep[nxt][j + c] = max(ep[nxt][j + c], ep[cur][j] + r); } } cur = nxt; } ll ans = 0; for (int j = 0; j <= w; j++) { if (dp[cur][j] != 1e9) { int i = dp[cur][j]; ans = max(ans, i * w + j * h - 2 * i * j); } if (ep[cur][j] != -1e9) { int i = ep[cur][j]; ans = max(ans, i * w + j * h - 2 * i * j); } } cout << ans << "\n"; }