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

#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
typedef long long int ll;
ll mod = 1000000007;
const int N = 300004;
int ind[N] = {0};
int ind1[N] = {0};
set<int> posx, posy;
set<pair<int, int>> mat;
int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w;
    cin >> h >> w;
    int m;
    cin >> m;
    // int count=0;

    // int ma[m+1]={0};
    // int mat[h+1][w+1];
    // memset(mat,0,sizeof(mat));
    for (int i = 1; i <= m; i++) {
      int x, y;
      cin >> x >> y;
      // cin>>y;
      ind[x]++;
      ind1[y]++;
      mat.insert(pair<int, int>(x, y));
    }
    int maxm = INT_MIN;

    for (int i = 1; i <= h; i++) {
      maxm = max(maxm, ind[i]);
    }
    for (int i = 1; i <= h; i++) {
      if (ind[i] == maxm)
        posx.insert(i);
    }
    maxm = INT_MIN;
    for (int i = 1; i <= w; i++) {
      maxm = max(maxm, ind1[i]);
    }
    for (int i = 1; i <= w; i++) {
      if (ind1[i] == maxm)
        posy.insert(i);
    }

    int flag = 0;
    /* for(int i=1;i<=m;i++)
     {   if((posx.find(x[i])!=posx.end() && posy.find(y[i])==posy.end())
     ||(posx.find(x[i])==posx.end() && posy.find(y[i])!=posy.end())) {   flag++;
             break;
         }
     }*/
    for (auto itr = posx.begin(); itr != posx.end(); itr++) {
      for (auto itr1 = posy.begin(); itr1 != posy.end(); itr1++) {
        if (mat.find(pair<int, int>(*itr, *itr1)) == mat.end()) {
          flag++;
          break;
        }
      }
    }
    if (flag > 0)
      cout << ind[*posx.begin()] + ind1[*posy.begin()];
    else
      cout << ind[*posx.begin()] + ind1[*posy.begin()] - 1;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
typedef long long int ll;
ll mod = 1000000007;
const int N = 300004;
int ind[N] = {0};
int ind1[N] = {0};
int x[N];
int y[N];

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w;
    cin >> h >> w;
    int m;
    cin >> m;
    int count = 0;

    // int ma[m+1]={0};
    int mat[h + 1][w + 1];
    // memset(mat,0,sizeof(mat));
    for (int i = 1; i <= m; i++) {
      cin >> x[i];
      cin >> y[i];
      ind[x[i]]++;
      ind1[y[i]]++;
      mat[x[i]][y[i]] = 100;
    }
    int maxm = INT_MIN;
    set<int> posx, posy;
    for (int i = 1; i <= h; i++) {
      maxm = max(maxm, ind[i]);
    }
    for (int i = 1; i <= h; i++) {
      if (ind[i] == maxm)
        posx.insert(i);
    }
    maxm = INT_MIN;
    for (int i = 1; i <= w; i++) {
      maxm = max(maxm, ind1[i]);
    }
    for (int i = 1; i <= w; i++) {
      if (ind1[i] == maxm)
        posy.insert(i);
    }

    int flag = 0;
    /* for(int i=1;i<=m;i++)
     {   if((posx.find(x[i])!=posx.end() && posy.find(y[i])==posy.end())
     ||(posx.find(x[i])==posx.end() && posy.find(y[i])!=posy.end())) {   flag++;
             break;
         }
     }*/
    for (auto itr = posx.begin(); itr != posx.end(); itr++) {
      for (auto itr1 = posy.begin(); itr1 != posy.end(); itr1++) {
        if (mat[*itr][*itr1] != 100) {
          flag++;
          break;
        }
      }
    }
    if (flag > 0)
      cout << ind[*posx.begin()] + ind1[*posy.begin()];
    else
      cout << ind[*posx.begin()] + ind1[*posy.begin()] - 1;
  }
  return 0;
}