Problem p03014 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//
// https://atcoder.jp/contests/abc127/tasks/abc127_f

#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main(int argc, char *argv[]) {
  int H, W;
  cin >> H >> W;
  struct Cell {
    int hor_len = -1;
    int ver_len = -1;
    pair<int, int> hor_ptr = {-1, -1};
    pair<int, int> ver_ptr = {-1, -1};
    bool is_wall() { return hor_ptr == pair<int, int>{-1, -1}; }
  };
  vector<vector<Cell>> grid;
  for (int x = 0; x <= W; x++) {
    grid.push_back(vector<Cell>(H + 1));
  }
  for (int y = 0; y <= H; y++) {
    string line;
    if (y < H) {
      cin >> line;
    }
    for (int x = 0; x <= W; x++) {
      char c = 0;
      if (x < W) {
        c = line[x];
      }
      if (x == W || y == H || c == '#') {
        if (x > 0 && !grid[x - 1][y].is_wall()) {
          pair<int, int> head = grid[x - 1][y].hor_ptr;
          grid[head.first][head.second].hor_len = x - head.first;
        }
        if (y > 0 && !grid[x][y - 1].is_wall()) {
          pair<int, int> head = grid[x][y - 1].ver_ptr;
          grid[head.first][head.second].ver_len = y - head.second;
        }
      } else {
        if (x > 0 && !grid[x - 1][y].is_wall()) {
          grid[x][y].hor_ptr = grid[x - 1][y].hor_ptr;
        } else {
          grid[x][y].hor_ptr = {x, y};
        }
        if (y > 0 && !grid[x][y - 1].is_wall()) {
          grid[x][y].ver_ptr = grid[x][y - 1].ver_ptr;
        } else {
          grid[x][y].ver_ptr = {x, y};
        }
      }
    }
  }
  int max_val = numeric_limits<int>::min();
  for (int x = 0; x < W; x++) {
    for (int y = 0; y < H; y++) {
      if (grid[x][y].is_wall()) {
        continue;
      }
      int len = -1;
      pair<int, int> hor_head = grid[x][y].hor_ptr;
      len += grid[hor_head.first][hor_head.second].hor_len;
      pair<int, int> ver_head = grid[x][y].ver_ptr;
      len += grid[ver_head.first][ver_head.second].ver_len;
      max_val = max(max_val, len);
    }
  }
  cout << max_val << endl;
}


Fixed submission:
//
//  main.cpp
//
// https://atcoder.jp/contests/abc127/tasks/abc127_f

#include <algorithm>
#include <array>
#include <assert.h>
#include <functional>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main(int argc, char *argv[]) {
  int H, W;
  cin >> H >> W;
  struct Cell {
    int hor_len = -1;
    int ver_len = -1;
    pair<int, int> hor_ptr = {-1, -1};
    pair<int, int> ver_ptr = {-1, -1};
    bool is_wall() { return hor_ptr == pair<int, int>{-1, -1}; }
  };
  vector<vector<Cell>> grid;
  for (int x = 0; x <= W; x++) {
    grid.push_back(vector<Cell>(H + 1));
  }
  for (int y = 0; y <= H; y++) {
    string line;
    if (y < H) {
      cin >> line;
    }
    for (int x = 0; x <= W; x++) {
      char c = 0;
      if (y < H && x < W) {
        c = line[x];
      }
      if (x == W || y == H || c == '#') {
        if (x > 0 && !grid[x - 1][y].is_wall()) {
          pair<int, int> head = grid[x - 1][y].hor_ptr;
          grid[head.first][head.second].hor_len = x - head.first;
        }
        if (y > 0 && !grid[x][y - 1].is_wall()) {
          pair<int, int> head = grid[x][y - 1].ver_ptr;
          grid[head.first][head.second].ver_len = y - head.second;
        }
      } else {
        if (x > 0 && !grid[x - 1][y].is_wall()) {
          grid[x][y].hor_ptr = grid[x - 1][y].hor_ptr;
        } else {
          grid[x][y].hor_ptr = {x, y};
        }
        if (y > 0 && !grid[x][y - 1].is_wall()) {
          grid[x][y].ver_ptr = grid[x][y - 1].ver_ptr;
        } else {
          grid[x][y].ver_ptr = {x, y};
        }
      }
    }
  }
  int max_val = numeric_limits<int>::min();
  for (int x = 0; x < W; x++) {
    for (int y = 0; y < H; y++) {
      if (grid[x][y].is_wall()) {
        continue;
      }
      int len = -1;
      pair<int, int> hor_head = grid[x][y].hor_ptr;
      len += grid[hor_head.first][hor_head.second].hor_len;
      pair<int, int> ver_head = grid[x][y].ver_ptr;
      len += grid[ver_head.first][ver_head.second].ver_len;
      max_val = max(max_val, len);
    }
  }
  cout << max_val << endl;
}