#include using namespace std; class UnionFind{ public: vector par,siz; void make(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } void unite(int u, int v){ u = root(u),v = root(v); if(u == v) return; if(siz.at(u) < siz.at(v)) swap(u,v); par.at(v) = u; siz.at(u) += siz.at(v); } bool issame(int u, int v){ if(root(u) == root(v)) return true; else return false; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W,N; cin >> H >> W >> N; UnionFind uf; uf.make(H+W); while(N--){ int a,b; cin >> a >> b; a--; b--; b += H; uf.unite(a,b); } vector> need(H+W); for(int i=0; i &a,pair b,int c) -> void { auto &[mina,maxa] = a; auto &[minb,maxb] = b; if(b == make_pair(W*2,-W*2)) return; mina = min(mina,minb+c); maxa = max(maxa,maxb+c); }; vector> dp(H+1,{W*2,-W*2}); dp.at(0) = {0,0}; for(auto &[r,c] : need){ if(r+c == 0) continue; for(int i=H-r; i>=0; i--) update(dp.at(i+r),dp.at(i),c); } int answer = 0; for(int i=0; i<=H; i++){ auto &[minv,maxv] = dp.at(i); if(minv != W*2) answer = max(answer,(-2*i+H)*minv+i*W); if(maxv != -W*2) answer = max(answer,(-2*i+H)*maxv+i*W); } cout << answer << endl; }