Problem p03014 (C++)

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<vector<int>> l(n, vector<int>(m));
  vector<vector<int>> r(n, vector<int>(m));
  vector<vector<int>> u(n, vector<int>(m));
  vector<vector<int>> d(n, vector<int>(m));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (j == 0) {
        if (s[i][j] == '.')
          l[i][j] = 1;
        else
          l[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          l[i][j] = l[i][j - 1] + 1;
        else
          l[i][j] = 0;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = m - 1; j >= 0; j--) {
      if (j == m - 1) {
        if (s[i][j] == '.')
          r[i][j] = 1;
        else
          r[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          r[i][j] = r[i][j + 1] + 1;
        else
          r[i][j] = 0;
      }
    }
  }

  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        if (s[i][j] == '.')
          u[i][j] = 1;
        else
          u[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          u[i][j] = u[i - 1][j] + 1;
        else
          u[i][j] = 0;
      }
    }
  }

  for (int j = 0; j < n; j++) {
    for (int i = n - 1; i >= 0; i--) {
      if (i == n - 1) {
        if (s[i][j] == '.')
          d[i][j] = 1;
        else
          d[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          d[i][j] = d[i + 1][j] + 1;
        else
          d[i][j] = 0;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        ans = max(ans, l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3);
      }
    }
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<vector<int>> l(n, vector<int>(m));
  vector<vector<int>> r(n, vector<int>(m));
  vector<vector<int>> u(n, vector<int>(m));
  vector<vector<int>> d(n, vector<int>(m));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (j == 0) {
        if (s[i][j] == '.')
          l[i][j] = 1;
        else
          l[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          l[i][j] = l[i][j - 1] + 1;
        else
          l[i][j] = 0;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = m - 1; j >= 0; j--) {
      if (j == m - 1) {
        if (s[i][j] == '.')
          r[i][j] = 1;
        else
          r[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          r[i][j] = r[i][j + 1] + 1;
        else
          r[i][j] = 0;
      }
    }
  }

  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        if (s[i][j] == '.')
          u[i][j] = 1;
        else
          u[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          u[i][j] = u[i - 1][j] + 1;
        else
          u[i][j] = 0;
      }
    }
  }

  for (int j = 0; j < m; j++) {
    for (int i = n - 1; i >= 0; i--) {
      if (i == n - 1) {
        if (s[i][j] == '.')
          d[i][j] = 1;
        else
          d[i][j] = 0;
      } else {
        if (s[i][j] == '.')
          d[i][j] = d[i + 1][j] + 1;
        else
          d[i][j] = 0;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.') {
        ans = max(ans, l[i][j] + r[i][j] + u[i][j] + d[i][j] - 3);
      }
    }
  }

  cout << ans;

  return 0;
}