Problem p03014 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define REP(n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using INT64 = long long;
using UINT64 = unsigned long long;

int main(void) {
  IOS int h, w;
  cin >> h >> w;
  vector<string> ss(h);
  REP(h) cin >> ss[i];

  vector<vector<int>> v(h, vector<int>(w, 0));
  vector<int> lh(h, 0); // ライト縦方向
  int lw;               // ライト横方向
  FOR(i, 0, h) {
    lw = 0;
    FOR(j, 0, w) {
      if (ss[i][j] == '#') {
        int i2 = i - 1;
        while (i2 >= 0 && ss[i2][j] == '.') {
          v[i2][j] += lh[j];
          i2--;
        }
        int j2 = j - 1;
        while (j2 >= 0 && ss[i][j2] == '.') {
          v[i][j2] += lw;
          j2--;
        }
        lh[j] = 0;
        lw = 0;
        continue;
      }

      lh[j]++;
      lw++;

      if (j == w - 1) {
        int j2 = j;
        while (j2 >= 0 && ss[i][j2] == '.') {
          v[i][j2] += lw;
          j2--;
        }
      }
      if (i == h - 1) {
        // cerr << lh[j];
        int i2 = i;
        while (i2 >= 0 && ss[i2][j] == '.') {
          v[i2][j] += lh[j];
          i2--;
        }
      }
    }
    // REP(h) cerr << lh[i];
    // cerr << endl;
  }

  REP(h) {
    // FOR(j,0,w) cerr << v[i][j] << " ";
    // cerr << endl;
    sort(RALL(v[i]));
  }

  int ans = 0;
  REP(h) {
    // FOR(j,0,w) cerr << v[i][j];
    // cerr << endl;
    if (ans < v[i][0])
      ans = v[i][0];
  }

  cout << ans - 1 << endl;

  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define REP(n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using INT64 = long long;
using UINT64 = unsigned long long;

int main(void) {
  IOS int h, w;
  cin >> h >> w;
  vector<string> ss(h);
  REP(h) cin >> ss[i];

  vector<vector<int>> v(h, vector<int>(w, 0));
  vector<int> lh(w, 0); // ライト縦方向
  int lw;               // ライト横方向
  FOR(i, 0, h) {
    lw = 0;
    FOR(j, 0, w) {
      if (ss[i][j] == '#') {
        int i2 = i - 1;
        while (i2 >= 0 && ss[i2][j] == '.') {
          v[i2][j] += lh[j];
          i2--;
        }
        int j2 = j - 1;
        while (j2 >= 0 && ss[i][j2] == '.') {
          v[i][j2] += lw;
          j2--;
        }
        lh[j] = 0;
        lw = 0;
        continue;
      }

      lh[j]++;
      lw++;

      if (j == w - 1) {
        int j2 = j;
        while (j2 >= 0 && ss[i][j2] == '.') {
          v[i][j2] += lw;
          j2--;
        }
      }
      if (i == h - 1) {
        // cerr << lh[j];
        int i2 = i;
        while (i2 >= 0 && ss[i2][j] == '.') {
          v[i2][j] += lh[j];
          i2--;
        }
      }
    }
    // REP(h) cerr << lh[i];
    // cerr << endl;
  }

  REP(h) {
    // FOR(j,0,w) cerr << v[i][j] << " ";
    // cerr << endl;
    sort(RALL(v[i]));
  }

  int ans = 0;
  REP(h) {
    // FOR(j,0,w) cerr << v[i][j];
    // cerr << endl;
    if (ans < v[i][0])
      ans = v[i][0];
  }

  cout << ans - 1 << endl;

  return 0;
}