Problem p03014 (C++)

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

ll H, W;
vector<string> S;

void solve() {
  vector<vector<ll>> L(H, vector<ll>(W));
  vector<vector<ll>> R(H, vector<ll>(W));
  vector<vector<ll>> U(H, vector<ll>(W));
  vector<vector<ll>> D(H, vector<ll>(W));

  for (int i = 0; i < H; i++) {
    L[i][0] = (S[i][0] == '.') ? 1 : 0;
    R[i][W - 1] = (S[i][W - 1] == '.') ? 1 : 0;
  }

  for (int j = 0; j < H; j++) {
    U[0][j] = (S[0][j] == '.') ? 1 : 0;
    D[H - 1][j] = (S[H - 1][j] == '.') ? 1 : 0;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (S[i][j] == '#') {
        L[i][j] = 0;
      } else {
        L[i][j] = L[i][j - 1] + 1;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = W - 2; j >= 0; j--) {
      if (S[i][j] == '#') {
        R[i][j] = 0;
      } else {
        R[i][j] = R[i][j + 1] + 1;
      }
    }
  }

  for (int i = 1; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        U[i][j] = 0;
      } else {
        U[i][j] = U[i - 1][j] + 1;
      }
    }
  }

  for (int i = H - 2; i >= 0; i--) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        D[i][j] = 0;
      } else {
        D[i][j] = D[i + 1][j] + 1;
      }
    }
  }

  ll res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ll tmp = L[i][j] + R[i][j] + U[i][j] + D[i][j];
      if (S[i][j] == '.') {
        res = max(res, tmp - 3);
      } else {
        res = max(res, tmp);
      }
    }
  }
  cout << res << '\n';
}

int main() {
  cin >> H >> W;
  S.resize(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  solve();
  return 0;
}

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

ll H, W;
vector<string> S;

void solve() {
  vector<vector<ll>> L(H, vector<ll>(W));
  vector<vector<ll>> R(H, vector<ll>(W));
  vector<vector<ll>> U(H, vector<ll>(W));
  vector<vector<ll>> D(H, vector<ll>(W));

  for (int i = 0; i < H; i++) {
    L[i][0] = (S[i][0] == '.') ? 1 : 0;
    R[i][W - 1] = (S[i][W - 1] == '.') ? 1 : 0;
  }

  for (int j = 0; j < W; j++) {
    U[0][j] = (S[0][j] == '.') ? 1 : 0;
    D[H - 1][j] = (S[H - 1][j] == '.') ? 1 : 0;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (S[i][j] == '#') {
        L[i][j] = 0;
      } else {
        L[i][j] = L[i][j - 1] + 1;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = W - 2; j >= 0; j--) {
      if (S[i][j] == '#') {
        R[i][j] = 0;
      } else {
        R[i][j] = R[i][j + 1] + 1;
      }
    }
  }

  for (int i = 1; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        U[i][j] = 0;
      } else {
        U[i][j] = U[i - 1][j] + 1;
      }
    }
  }

  for (int i = H - 2; i >= 0; i--) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        D[i][j] = 0;
      } else {
        D[i][j] = D[i + 1][j] + 1;
      }
    }
  }

  ll res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ll tmp = L[i][j] + R[i][j] + U[i][j] + D[i][j];
      if (S[i][j] == '.') {
        res = max(res, tmp - 3);
      } else {
        res = max(res, tmp);
      }
    }
  }
  cout << res << '\n';
}

int main() {
  cin >> H >> W;
  S.resize(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  solve();
  return 0;
}