Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
template <typename T> bool chmax(T &u, const T z) {
  if (u < z) {
    u = z;
    return true;
  } else
    return false;
}
template <typename T> bool chmin(T &u, const T z) {
  if (u > z) {
    u = z;
    return true;
  } else
    return false;
}
const int INF = 10000000;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  map<int, int> mph, mpw;
  set<pair<int, int>> zahyou;
  rep(i, n) {
    ll r, c;
    cin >> r >> c;
    zahyou.insert({r, c});
    mph[r]++;
    mpw[c]++;
  }
  int maxh = 0, maxw = 0;
  for (auto p : mph)
    chmax(maxh, p.second);
  for (auto p : mpw)
    chmax(maxw, p.second);
  vector<int> numh, numw;
  for (auto p : mph)
    if (p.second == maxh)
      numh.push_back(p.first);
  for (auto p : mpw)
    if (p.second == maxw)
      numw.push_back(p.first);
  rep(i, numh.size()) rep(o, numw.size()) {
    if (!zahyou.count({numh[i], numw[o]})) {
      cout << maxh + maxw << endl;
      return 0;
    }
  }
  cout << maxh + maxw - 1 << endl;
}

Error: -11

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
ll dp[1000002];
bool xy[30000][30000];
int main() {
  ll a, b, c = 0, ans = 0, d, e;
  cin >> a >> b >> c;
  vector<ll> x(a, 0), y(b, 0);
  rep(i, c) {
    cin >> d >> e;
    d--;
    e--;
    x[d]++;
    y[e]++;
    xy[d][e] = true;
  }
  rep(i, a) {
    ll kotae = 0;
    rep(o, b) {
      if (xy[i][o])
        kotae = max(kotae, y[o] - 1);
      else
        kotae = max(kotae, y[o]);
    }
    ans = max(ans, kotae + x[i]);
  }
  cout << ans;
}