Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#define DEBUG 0
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using numr = std::numeric_limits<T>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll LLINF = 1e16;
const int mod = 1000000007;
const int mod2 = 998244353;
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head;
  debug_impl(tail...);
}
#if DEBUG
#define debug(...)                                                             \
  do {                                                                         \
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";                \
    debug_impl(__VA_ARGS__);                                                   \
    std::cerr << std::noboolalpha;                                             \
  } while (false)
#else
#define debug(...)                                                             \
  {}
#endif

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}

int H, W;
vector<string> fi;
int Left[2100][2100], Right[2100][2100], Up[2100][2100], Down[2100][2100];

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  cin >> H >> W;
  rep(i, H) cin >> fi[i];

  // 前処理
  for (int i = 0; i < H; ++i) {
    int cur = 0;
    for (int j = 0; j < W; ++j) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Left[i][j] = cur;
    }
  }
  for (int i = 0; i < H; ++i) {
    int cur = 0;
    for (int j = W - 1; j >= 0; --j) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Right[i][j] = cur;
    }
  }
  for (int j = 0; j < W; ++j) {
    int cur = 0;
    for (int i = 0; i < H; ++i) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Up[i][j] = cur;
    }
  }
  for (int j = 0; j < W; ++j) {
    int cur = 0;
    for (int i = H - 1; i >= 0; --i) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Down[i][j] = cur;
    }
  }

  int res = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (fi[i][j] == '#')
        continue;
      res = max(res, Left[i][j] + Right[i][j] + Up[i][j] + Down[i][j] - 3);
    }
  }
  cout << res << endl;
}

Fixed submission:
#define DEBUG 0
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using numr = std::numeric_limits<T>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll LLINF = 1e16;
const int mod = 1000000007;
const int mod2 = 998244353;
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head;
  debug_impl(tail...);
}
#if DEBUG
#define debug(...)                                                             \
  do {                                                                         \
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";                \
    debug_impl(__VA_ARGS__);                                                   \
    std::cerr << std::noboolalpha;                                             \
  } while (false)
#else
#define debug(...)                                                             \
  {}
#endif

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}

int H, W;
vector<string> fi;
int Left[2100][2100], Right[2100][2100], Up[2100][2100], Down[2100][2100];

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  cin >> H >> W;
  fi.resize(H);
  rep(i, H) cin >> fi[i];

  // 前処理
  for (int i = 0; i < H; ++i) {
    int cur = 0;
    for (int j = 0; j < W; ++j) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Left[i][j] = cur;
    }
  }
  for (int i = 0; i < H; ++i) {
    int cur = 0;
    for (int j = W - 1; j >= 0; --j) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Right[i][j] = cur;
    }
  }
  for (int j = 0; j < W; ++j) {
    int cur = 0;
    for (int i = 0; i < H; ++i) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Up[i][j] = cur;
    }
  }
  for (int j = 0; j < W; ++j) {
    int cur = 0;
    for (int i = H - 1; i >= 0; --i) {
      if (fi[i][j] == '#')
        cur = 0;
      else
        ++cur;
      Down[i][j] = cur;
    }
  }

  int res = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (fi[i][j] == '#')
        continue;
      res = max(res, Left[i][j] + Right[i][j] + Up[i][j] + Down[i][j] - 3);
    }
  }
  cout << res << endl;
}