Problem p03014 (C++)

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

using ll = int64_t;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) x.begin(), x.end()

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(ll x, int suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341000];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

const int mod = 1e9 + 7;

const int maxWH = 2005;
array<array<int, maxWH>, maxWH> grid;

signed main() {
  int h, w;
  std::cin >> h >> w;
  REP(i, w + 2) { grid[0][i] = 1, grid[h + 1][i] = 1; }
  REP(i, h + 2) { grid[i][0] = 1, grid[i][w + 1] = 1; }
  FOR(y, 1, h + 1) {
    string s = readString();
    int x = 1;
    for (auto &c : s) {
      if (c == '#')
        grid[y][x++] = 1;
      else
        grid[y][x++] = 0;
    }
  }
  auto cntVertical = [](int y, int x) {
    int ret = 0;
    for (int i = y - 1; grid[i][x] == 0; i--)
      ret++;
    for (int i = y + 1; grid[i][x] == 0; i++)
      ret++;
    return ret;
  };
  auto cntHorizontal = [](int y, int x) {
    int ret = 0;
    for (int i = x - 1; grid[y][i] == 0; i--)
      ret++;
    for (int i = x + 1; grid[y][i] == 0; i++)
      ret++;
    return ret;
  };
  int ans = 0;
  int horCnt[w + 1], verCnt[h + 1];
  FOR(y, 1, h + 1) FOR(x, 1, w + 1) {
    if (grid[y][x] == 1)
      continue;
    int tmp = 1;
    // vertical
    if (grid[y - 1][x] == 1)
      verCnt[x] = cntVertical(y, x);
    tmp += verCnt[x];
    // horizontal
    if (grid[y][x - 1] == 1)
      horCnt[y] = cntHorizontal(y, x);
    tmp += horCnt[y];
    chmax(ans, tmp);
  }
  print(ans);
}


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

using ll = int64_t;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) x.begin(), x.end()

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(ll x, int suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341000];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

const int mod = 1e9 + 7;

const int maxWH = 2005;
array<array<int, maxWH>, maxWH> grid;

signed main() {
  int h, w;
  std::cin >> h >> w;
  REP(i, w + 2) { grid[0][i] = 1, grid[h + 1][i] = 1; }
  REP(i, h + 2) { grid[i][0] = 1, grid[i][w + 1] = 1; }
  FOR(y, 1, h + 1) {
    string s = readString();
    int x = 1;
    for (auto &c : s) {
      if (c == '#')
        grid[y][x++] = 1;
      else
        grid[y][x++] = 0;
    }
  }
  auto cntVertical = [](int y, int x) {
    int ret = 0;
    for (int i = y - 1; grid[i][x] == 0; i--)
      ret++;
    for (int i = y + 1; grid[i][x] == 0; i++)
      ret++;
    return ret;
  };
  auto cntHorizontal = [](int y, int x) {
    int ret = 0;
    for (int i = x - 1; grid[y][i] == 0; i--)
      ret++;
    for (int i = x + 1; grid[y][i] == 0; i++)
      ret++;
    return ret;
  };
  int ans = 0;
  int horCnt[h + 1], verCnt[w + 1];
  FOR(y, 1, h + 1) FOR(x, 1, w + 1) {
    if (grid[y][x] == 1)
      continue;
    int tmp = 1;
    // vertical
    if (grid[y - 1][x] == 1)
      verCnt[x] = cntVertical(y, x);
    tmp += verCnt[x];
    // horizontal
    if (grid[y][x - 1] == 1)
      horCnt[y] = cntHorizontal(y, x);
    tmp += horCnt[y];
    chmax(ans, tmp);
  }
  print(ans);
}