Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> H(N + 1, vector<int>(M + 1)),
      V(M + 1, vector<int>(N + 1));
  vector<vector<pair<char, pair<int, int>>>> X(
      N + 1, vector<pair<char, pair<int, int>>>(M + 1)); // char, (idh, idv)
  int cnt = 0, res = 0, id = 0;
  char c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> c;
      X[i][j] = make_pair(c, make_pair(0, 0));
    }
  }
  // horizontal ids
  for (int i = 0; i < N; i++) {
    id = 0;
    cnt = 0;
    for (int j = 0; j < M; j++) {
      if (X[i][j].first == '#' or j == M - 1) {
        H[i][id] = cnt;
        // cout << i << " " << j << " " << H[i][id] << " " <<id << endl;
        cnt = 0;
        if (X[i][j].first == '#') {
          if (j != 0) {
            id++;
          }
          X[i][j].second.first = id;
          H[i][id] = cnt;
          // cout << i << " " << j << " " << H[i][id] << " " <<id << endl;
          id++;
        } else if (j == M - 1) {
          H[i][id]++;
          X[i][j].second.first = id;
          // cout << " hh " << H[i][id] << endl;
        }
      } else {
        X[i][j].second.first = id;
        cnt++;
      }
    }
  }
  // vertical ids
  for (int i = 0; i < M; i++) {
    id = 0;
    cnt = 0;
    for (int j = 0; j < N; j++) {
      if (X[j][i].first == '#' or j == N - 1) {
        V[i][id] = cnt;
        cnt = 0;
        // cout << i << " " << j << " " << V[i][id] << " " <<id << endl;
        if (X[j][i].first == '#') {
          if (j != 0) {
            id++;
          }
          X[j][i].second.second = id;
          V[i][id] = cnt;
          // cout << i << " " << j << " " << V[i][id] << " " <<id << endl;
          id++;
        } else if (j == N - 1) {
          V[i][id]++;
          X[j][i].second.second = id;
          // cout <<" hh" <<  i << " " << j << " " << V[i][id] << " " <<id <<
          // endl;
        }
      } else {
        X[j][i].second.second = id;
        cnt++;
      }
    }
  }
  /*
  for(int i = 0; i < N ;i ++){
          for(int j =0;j < M; j++){
                  cout << H[i][j] << " ";
          }
          cout << endl;
  }
  cout << endl;
  for(int i = 0; i < M ;i ++){
          for(int j =0;j < N; j++){
                  cout << V[i][j] << " ";
          }
          cout << endl;
  }*/

  // get answer
  int id1 = 0, id2 = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      id1 = X[i][j].second.first, id2 = X[i][j].second.second;
      res = max(H[i][id1] + V[j][id2] - 1, res);
      // cout << i << " " << j << " " << id1 << " " << id2 << " " << H[i][id1]
      // << " " << V[j][id2] << endl;
    }
  }
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> H(N + 1, vector<int>(20000)),
      V(M + 1, vector<int>(20000));
  vector<vector<pair<char, pair<int, int>>>> X(
      N + 1, vector<pair<char, pair<int, int>>>(M + 1)); // char, (idh, idv)
  int cnt = 0, res = 0, id = 0;
  char c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> c;
      X[i][j] = make_pair(c, make_pair(0, 0));
    }
  }
  // horizontal ids
  for (int i = 0; i < N; i++) {
    id = 0;
    cnt = 0;
    for (int j = 0; j < M; j++) {
      if (X[i][j].first == '#' or j == M - 1) {
        H[i][id] = cnt;
        // cout << i << " " << j << " " << H[i][id] << " " <<id << endl;
        cnt = 0;
        if (X[i][j].first == '#') {
          if (j != 0) {
            id++;
          }
          X[i][j].second.first = id;
          H[i][id] = cnt;
          // cout << i << " " << j << " " << H[i][id] << " " <<id << endl;
          id++;
        } else if (j == M - 1) {
          H[i][id]++;
          X[i][j].second.first = id;
          // cout << " hh " << H[i][id] << endl;
        }
      } else {
        X[i][j].second.first = id;
        cnt++;
      }
    }
  }
  // vertical ids
  for (int i = 0; i < M; i++) {
    id = 0;
    cnt = 0;
    for (int j = 0; j < N; j++) {
      if (X[j][i].first == '#' or j == N - 1) {
        V[i][id] = cnt;
        cnt = 0;
        // cout << i << " " << j << " " << V[i][id] << " " <<id << endl;
        if (X[j][i].first == '#') {
          if (j != 0) {
            id++;
          }
          X[j][i].second.second = id;
          V[i][id] = cnt;
          // cout << i << " " << j << " " << V[i][id] << " " <<id << endl;
          id++;
        } else if (j == N - 1) {
          V[i][id]++;
          X[j][i].second.second = id;
          // cout <<" hh" <<  i << " " << j << " " << V[i][id] << " " <<id <<
          // endl;
        }
      } else {
        X[j][i].second.second = id;
        cnt++;
      }
    }
  }
  /*
  for(int i = 0; i < N ;i ++){
          for(int j =0;j < M; j++){
                  cout << H[i][j] << " ";
          }
          cout << endl;
  }
  cout << endl;
  for(int i = 0; i < M ;i ++){
          for(int j =0;j < N; j++){
                  cout << V[i][j] << " ";
          }
          cout << endl;
  }*/

  // get answer
  int id1 = 0, id2 = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      id1 = X[i][j].second.first, id2 = X[i][j].second.second;
      res = max(H[i][id1] + V[j][id2] - 1, res);
      // cout << i << " " << j << " " << id1 << " " << id2 << " " << H[i][id1]
      // << " " << V[j][id2] << endl;
    }
  }
  cout << res;
  return 0;
}