Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;
vector<vector<bool>> maze;

int main() {
  cin >> H;
  cin >> W;

  maze = vector<vector<bool>>(H, vector<bool>(W, true));

  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s.at(j) == '#') {
        maze.at(i).at(j) = false;
      }
    }
  }

  vector<vector<int>> h_len = vector<vector<int>>(H, vector<int>(W, 0));
  vector<vector<int>> w_len = vector<vector<int>>(H, vector<int>(W, 0));

  for (int i = 0; i < H; i++) {
    int count = 0;
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j)) {
        count++;
        w_len.at(i).at(j) = count;
      } else {
        count = 0;
      }
    }

    for (int j = W - 2; j >= 0; j--) {
      if (w_len.at(i).at(j) > 0 && w_len.at(i).at(j + 1) > 0) {
        w_len.at(i).at(j) = w_len.at(i).at(j + 1);
      }
    }
  }

  for (int j = 0; j < W; j++) {
    int count = 0;
    for (int i = 0; i < H; i++) {
      if (maze.at(i).at(j)) {
        count++;
        h_len.at(i).at(j) = count;
      } else {
        count = 0;
      }
    }

    for (int i = H - 2; i >= 0; i--) {
      if (h_len.at(i).at(j) > 0 && h_len.at(i + 1).at(j) > 0) {
        h_len.at(i).at(j) = h_len.at(i + 1).at(j);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j)) {
        int tmp = h_len.at(i).at(j) + w_len.at(i).at(i) - 1;
        ans = max(ans, tmp);
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;
vector<vector<bool>> maze;

int main() {
  cin >> H;
  cin >> W;

  maze = vector<vector<bool>>(H, vector<bool>(W, true));

  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s.at(j) == '#') {
        maze.at(i).at(j) = false;
      }
    }
  }

  vector<vector<int>> h_len = vector<vector<int>>(H, vector<int>(W, 0));
  vector<vector<int>> w_len = vector<vector<int>>(H, vector<int>(W, 0));

  for (int i = 0; i < H; i++) {
    int count = 0;
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j)) {
        count++;
        w_len.at(i).at(j) = count;
      } else {
        count = 0;
      }
    }

    for (int j = W - 2; j >= 0; j--) {
      if (w_len.at(i).at(j) > 0 && w_len.at(i).at(j + 1) > 0) {
        w_len.at(i).at(j) = w_len.at(i).at(j + 1);
      }
    }
  }

  for (int j = 0; j < W; j++) {
    int count = 0;
    for (int i = 0; i < H; i++) {
      if (maze.at(i).at(j)) {
        count++;
        h_len.at(i).at(j) = count;
      } else {
        count = 0;
      }
    }

    for (int i = H - 2; i >= 0; i--) {
      if (h_len.at(i).at(j) > 0 && h_len.at(i + 1).at(j) > 0) {
        h_len.at(i).at(j) = h_len.at(i + 1).at(j);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j)) {
        int tmp = h_len.at(i).at(j) + w_len.at(i).at(j) - 1;
        ans = max(ans, tmp);
      }
    }
  }

  cout << ans << endl;

  return 0;
}