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

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mpX;
  map<int, int> mpY;
  map<pair<int, int>, bool> mpXY;
  rep(i, m) {
    int x, y;
    x = i, y = i;
    cin >> x >> y;
    --x, --y;
    ++mpX[x];
    ++mpY[y];
    mpXY[{x, y}] = true;
  }
  int mxX = 0, mxY = 0;
  vector<int> X, Y;
  for (auto p : mpX)
    chmax(mxX, p.second);
  for (auto p : mpY)
    chmax(mxY, p.second);
  for (auto p : mpX)
    if (p.second == mxX)
      X.push_back(p.first);
  for (auto p : mpY)
    if (p.second == mxY)
      Y.push_back(p.first);
  for (int x : X)
    for (int y : Y) {
      if (!mpXY[{x, y}]) {
        cout << mxX + mxY << endl;
        return 0;
      }
    }
  cout << mxX + mxY - 1 << endl;
}

Error: 0

Fixed submission:

#pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()

#pragma region UnionFind
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion
#pragma endregion

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mpX;
  map<int, int> mpY;
  vector<vector<bool>> b(h, vector<bool>(w));
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    ++mpX[x];
    ++mpY[y];
    b[x][y] = true;
  }
  int res = 0;
  rep(i, h) rep(j, w) {
    int tmp = mpX[i] + mpY[j];
    if (b[i][j])
      --tmp;
    chmax(res, tmp);
  }
  cout << res << endl;
}