Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = 2e2 + 5;

char a[N][N];
vector<int> row[N];
vector<int> col[N];
int n, m;

int32_t main() {
  IOS;
  cin >> n >> m;
  int best = INT_MIN;
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> a[i][j]; }
  }
  rep(i, 1, n) {
    row[i].pb(0);
    rep(j, 1, m) {
      if (a[i][j] == '#')
        row[i].pb(j);
    }
    row[i].pb(m + 1);
  }
  rep(j, 1, m) {
    col[j].pb(0);
    rep(i, 1, n) {
      if (a[i][j] == '#')
        col[j].pb(i);
    }
    col[j].pb(n + 1);
  }
  rep(i, 1, n) {
    rep(j, 1, m) {
      if (a[i][j] == '.') {
        int q = lower_bound(row[i].begin(), row[i].end(), j) - row[i].begin();
        int p = lower_bound(col[j].begin(), col[j].end(), i) - col[j].begin();
        int ans = 0;
        int req = row[i][q];
        ans += (req - j - 1);
        q--;
        req = row[i][q];
        ans += (j - req - 1);
        req = col[j][p];
        ans += (req - i - 1);
        p--;
        req = col[j][p];
        ans += (i - req - 1);
        ans++;
        best = max(ans, best);
      }
    }
  }
  cout << best << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = 2005;

char a[N][N];
vector<int> row[N];
vector<int> col[N];
int n, m;

int32_t main() {
  IOS;
  cin >> n >> m;
  int best = INT_MIN;
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> a[i][j]; }
  }
  rep(i, 1, n) {
    row[i].pb(0);
    rep(j, 1, m) {
      if (a[i][j] == '#')
        row[i].pb(j);
    }
    row[i].pb(m + 1);
  }
  rep(j, 1, m) {
    col[j].pb(0);
    rep(i, 1, n) {
      if (a[i][j] == '#')
        col[j].pb(i);
    }
    col[j].pb(n + 1);
  }
  rep(i, 1, n) {
    rep(j, 1, m) {
      if (a[i][j] == '.') {
        int q = lower_bound(row[i].begin(), row[i].end(), j) - row[i].begin();
        int p = lower_bound(col[j].begin(), col[j].end(), i) - col[j].begin();
        int ans = 0;
        int req = row[i][q];
        ans += (req - j - 1);
        q--;
        req = row[i][q];
        ans += (j - req - 1);
        req = col[j][p];
        ans += (req - i - 1);
        p--;
        req = col[j][p];
        ans += (i - req - 1);
        ans++;
        best = max(ans, best);
      }
    }
  }
  cout << best << endl;
}