Problem p02580 (C++)

Buggy submission (status: Runtime Error):
//*************************************JAI SAI
//RAM*************************************//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
ll mat[3000001], vis[3000001], parent[1000001], ranks[1000001];
ll P = 998244353;
vector<pair<ll, ll>> path = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
ll find(ll x) {
  if (parent[x] == x)
    return x;
  return parent[x] = find(parent[x]);
}
void unions(ll x, ll y) {
  ll xp = find(x);
  ll yp = find(y);
  if (xp != yp) {
    if (ranks[xp] > ranks[yp])
      parent[yp] = xp;
    else {
      parent[xp] = yp;
      if (ranks[xp] == ranks[yp])
        ranks[yp]++;
    }
  }
}
vector<ll> adj[1000001];
void solve() {
  ll i, j, k, l, m, n, o, r, p, q, ss, c;
  ll ans = 0;
  //  cout<<ans<<endl;
  string s[1011];
  cin >> n >> m >> p;
  map<ll, ll> row, col;
  map<pair<ll, ll>, ll> mp;
  ll mat[n + 1][m + 1];
  memset(mat, 0, sizeof(mat));
  set<ll> ro, co;

  for (i = 0; i < p; i++) {
    cin >> j >> k;
    row[j]++;
    col[k]++;
    mp[{j, k}]++;
  }
  ans = 0;
  r = 0;
  for (i = 1; i <= n; i++) {
    r = max(r, row[i]);
  }
  c = 0;
  for (i = 1; i <= m; i++)
    c = max(c, col[i]);
  for (i = 1; i <= n; i++) {
    if (row[i] == r)
      ro.insert(i);
  }
  for (i = 1; i <= m; i++)
    if (col[i] == c)
      co.insert(i);
  ans = (r + c - 1);

  ll flag = 0;
  for (auto x : ro) {
    for (auto y : co) {
      if (mp[{x, y}] == 0) {
        ans++;
        flag = 1;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  ll o = 1;
  while (t--) {

    solve();
    o++;
  }
  return 0;
}

// manpreet.singh orz
// humane orz
// arnavvats orz
// leoamish300 orz
// cipher76 orz
// aug16arpit orz
// aayushindwan orz
// ig_monkey orz
// DeadlyCritic orz
// lucky_21 orz
// sak23 orz
// spookywooky orz
// supahotfire orz
//__FAILURE_orz
// Errichto orz
// tourist orz
// Um_nik orz
// 300iq orz
// antontrygub_Oo orz
// boboniu orz
// MikeMirzayanov orz
// MiFaFaOvO orz
//  Benq orz
// Radewoosh orz
// AshishGup orz
// Ashishchup orz
// aryanc403 orz
// ma_da_fa_ka orz
//  Striver_79 orz
// Love babbar orz
//  My Life of ultimate patience and hard work orz!!!!!!!


Fixed submission:
//*************************************JAI SAI
//RAM*************************************//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
ll mat[3000001], vis[3000001], parent[1000001], ranks[1000001];
ll P = 998244353;
vector<pair<ll, ll>> path = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
ll find(ll x) {
  if (parent[x] == x)
    return x;
  return parent[x] = find(parent[x]);
}
void unions(ll x, ll y) {
  ll xp = find(x);
  ll yp = find(y);
  if (xp != yp) {
    if (ranks[xp] > ranks[yp])
      parent[yp] = xp;
    else {
      parent[xp] = yp;
      if (ranks[xp] == ranks[yp])
        ranks[yp]++;
    }
  }
}
vector<ll> adj[1000001];
void solve() {
  ll i, j, k, l, m, n, o, r, p, q, ss, c;
  ll ans = 0;
  //  cout<<ans<<endl;
  string s[1011];
  cin >> n >> m >> p;
  map<ll, ll> row, col;
  map<pair<ll, ll>, ll> mp;
  memset(mat, 0, sizeof(mat));
  set<ll> ro, co;

  for (i = 0; i < p; i++) {
    cin >> j >> k;
    row[j]++;
    col[k]++;
    mp[{j, k}]++;
  }
  ans = 0;
  r = 0;
  for (i = 1; i <= n; i++) {
    r = max(r, row[i]);
  }
  c = 0;
  for (i = 1; i <= m; i++)
    c = max(c, col[i]);
  for (i = 1; i <= n; i++) {
    if (row[i] == r)
      ro.insert(i);
  }
  for (i = 1; i <= m; i++)
    if (col[i] == c)
      co.insert(i);
  ans = (r + c - 1);

  ll flag = 0;
  for (auto x : ro) {
    for (auto y : co) {
      if (mp[{x, y}] == 0) {
        ans++;
        flag = 1;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  ll o = 1;
  while (t--) {

    solve();
    o++;
  }
  return 0;
}

// manpreet.singh orz
// humane orz
// arnavvats orz
// leoamish300 orz
// cipher76 orz
// aug16arpit orz
// aayushindwan orz
// ig_monkey orz
// DeadlyCritic orz
// lucky_21 orz
// sak23 orz
// spookywooky orz
// supahotfire orz
//__FAILURE_orz
// Errichto orz
// tourist orz
// Um_nik orz
// 300iq orz
// antontrygub_Oo orz
// boboniu orz
// MikeMirzayanov orz
// MiFaFaOvO orz
//  Benq orz
// Radewoosh orz
// AshishGup orz
// Ashishchup orz
// aryanc403 orz
// ma_da_fa_ka orz
//  Striver_79 orz
// Love babbar orz
//  My Life of ultimate patience and hard work orz!!!!!!!