Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vec> vec2;
typedef map<ll, ll> MPll;
typedef set<ll> setl;

const ll INF = 1ll << 60;
const ld EPS = 1e-10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mods = 1000000007;

// for文
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define FORE(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define REP(i, size) for (ll i = (ll)0; i < size; i++)
#define REPE(i, size) for (ll i = (ll)0; i <= size; i++)
#define REPR(i, size) for (ll i = (ll)size; i >= 0; i--)
#define FOREACH(it, vec) for (auto it = vec.begin(); it != vec.end(); it++)

// ソート
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))
#define SORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORT(vec) sort((vec).rbegin(), (vec).rend())
#define REV(vec) reverse(ALL(vec))
#define REVA(arr) reverse(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll))), REVA(arr)

// 最大値最小値
#define MAX(vec) *max_element(ALL(vec))
#define UNIQ(vec)                                                              \
  SORT(vec);                                                                   \
  vec.erase(unique(ALL(vec)), vec.end())
#define MIN(vec) *min_element(ALL(vec))

// 出力
#define printl(a) cout << a << "\n"
#define print(a) cout << a
#define OUT(a) printf("%lld\n", a)
#define OUTA(array)                                                            \
  REP(i, sizeof(array) / sizeof(ll)) printf("%lld\n", array[i])
#define OUTV(vec) REP(i, vec.size()) printf("%lld\n", vec[i])
#define SP printf(" ")

// 入力
#define IN(x) scanf("%lld", &x)
#define INV(vec) REP(i, vec.size()) scanf("%lld", &vec[i])
#define INA(array) REP(i, sizeof(array) / sizeof(ll)) scanf("%lld", array + i)
#define INS(x) cin >> x
#define INCH(x) scanf(" %c", &x)

// 型
#define P pair
#define vp vector<P>
#define F first
#define S second

// その他
#define PB push_back
#define MP make_pair
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define INFI(a) memset(a, INF, sizeof(a))
#define MEM(a, b) memset(a, (b), sizeof(a))

// 関数
template <class T> inline void amax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void amin(T &a, const T &b) {
  if (a > b)
    a = b;
}
/*
struct structure{
    ll num1,num2,string s;
    bool operator<(const rest &another) const{
        return this->s < another.s;
    }
    structure(ll a,ll b,string s){
        this->num1 = a,this->num2=b,this->s=s;
    }
};a
*/
// 特殊
// #define int ll
#define _CRT_SECURE_NO_WARNINGS

/*
template <typename T>
struct UnionFindTree{

    vector<T> par;

    UnionFindTree(T n) : par(n+1){
        for (int i = 1; i <= n; ++i) {
            par[i] = i;
        }
    }

    T root(T x){
        if(par[x] == x) return x;
        else  return par[x] = root(par[x]);
    }

    void unite(T x,T y){
        if(!same(x,y)) par[root(x)] = root(par[y]);
    }

    T same(T x,T y){
        return root(x) == root(y);
    }

};
*/

#define DEBUG

#ifdef DEBUG
#define debugl(x) cerr << #x << ":" << x << "\n"
#define debug(x) cerr << x
#define debugV(V)                                                              \
  REP(i, V.size()) { cerr << i << ":" << V[i] << endl; }
#define debugA(A)                                                              \
  REP(i, sizeof(A) / sizeof(ll)) { cerr << i << ":" << V[i] << endl; }
#else
#define debug(x)
#define debugV(x)
#define debugA(x)
#define debugl(x)
#endif

#define ZERO(a) memset(a, 0, sizeof(a))
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))

signed main() {

  ll H;
  ll W;
  IN(H);
  IN(W);
  string S[H + 2];
  string temp;

  S[0] = '#';
  S[H + 1] = '#';
  FOR(i, 0, W + 2) S[0] += '#';
  FOR(i, 0, W + 2) S[H + 1] += '#';

  // cerr << S[0] << endl;

  FOR(i, 1, H + 1) {
    S[i] = "#";
    cin >> temp;
    S[i] += temp;
    S[i] += "#";
    // cerr << S[i] << endl;
  }

  // cerr << S[H+1] << endl;

  vec2 hidari(W + 2, vec(H + 2, 0));
  vec2 migi(W + 2, vec(H + 2, 0));
  vec2 ue(W + 2, vec(H + 2, 0));
  vec2 sita(W + 2, vec(H + 2, 0));

  FOR(h, 1, H + 1) FOR(w, 1, W + 1) {
    if (S[h][w] == '#') {
      hidari[h][w] = 0;
    } else {
      hidari[h][w] = hidari[h][w - 1] + 1;
    }
  }

  FOR(w, 1, W + 1) FOR(h, 1, H + 1) {
    if (S[h][w] == '#') {
      ue[h][w] = 0;
    } else {
      ue[h][w] = ue[h - 1][w] + 1;
    }
  }

  for (int w = W; w >= 1; --w) {
    for (int h = H; h >= 1; --h) {
      if (S[h][w] == '#') {
        sita[h][w] = 0;
      } else {
        sita[h][w] = sita[h + 1][w] + 1;
      }
    }
  }

  for (int h = H; h >= 1; --h) {
    for (int w = W; w >= 1; --w) {
      if (S[h][w] == '#') {
        migi[h][w] = 0;
      } else {
        migi[h][w] = migi[h][w + 1] + 1;
      }
    }
  }

  ll ans = 0;
  ll num = 0;

  FOR(w, 1, W + 1) FOR(h, 1, H + 1) {
    if (S[h][w] == '.') {
      num = migi[h][w] + hidari[h][w] + sita[h][w] + ue[h][w] - 3;
      // cerr <<w<<h<<","<<
      // migi[h][w]<<","<<hidari[h][w]<<","<<sita[h][w]<<","<<ue[h][w]<<endl;
      amax(ans, num);
    }
  }

  OUT(ans);
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vec> vec2;
typedef map<ll, ll> MPll;
typedef set<ll> setl;

const ll INF = 1ll << 60;
const ld EPS = 1e-10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mods = 1000000007;

// for文
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define FORE(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define REP(i, size) for (ll i = (ll)0; i < size; i++)
#define REPE(i, size) for (ll i = (ll)0; i <= size; i++)
#define REPR(i, size) for (ll i = (ll)size; i >= 0; i--)
#define FOREACH(it, vec) for (auto it = vec.begin(); it != vec.end(); it++)

// ソート
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))
#define SORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORT(vec) sort((vec).rbegin(), (vec).rend())
#define REV(vec) reverse(ALL(vec))
#define REVA(arr) reverse(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll))), REVA(arr)

// 最大値最小値
#define MAX(vec) *max_element(ALL(vec))
#define UNIQ(vec)                                                              \
  SORT(vec);                                                                   \
  vec.erase(unique(ALL(vec)), vec.end())
#define MIN(vec) *min_element(ALL(vec))

// 出力
#define printl(a) cout << a << "\n"
#define print(a) cout << a
#define OUT(a) printf("%lld\n", a)
#define OUTA(array)                                                            \
  REP(i, sizeof(array) / sizeof(ll)) printf("%lld\n", array[i])
#define OUTV(vec) REP(i, vec.size()) printf("%lld\n", vec[i])
#define SP printf(" ")

// 入力
#define IN(x) scanf("%lld", &x)
#define INV(vec) REP(i, vec.size()) scanf("%lld", &vec[i])
#define INA(array) REP(i, sizeof(array) / sizeof(ll)) scanf("%lld", array + i)
#define INS(x) cin >> x
#define INCH(x) scanf(" %c", &x)

// 型
#define P pair
#define vp vector<P>
#define F first
#define S second

// その他
#define PB push_back
#define MP make_pair
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define INFI(a) memset(a, INF, sizeof(a))
#define MEM(a, b) memset(a, (b), sizeof(a))

// 関数
template <class T> inline void amax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void amin(T &a, const T &b) {
  if (a > b)
    a = b;
}
/*
struct structure{
    ll num1,num2,string s;
    bool operator<(const rest &another) const{
        return this->s < another.s;
    }
    structure(ll a,ll b,string s){
        this->num1 = a,this->num2=b,this->s=s;
    }
};a
*/
// 特殊
// #define int ll
#define _CRT_SECURE_NO_WARNINGS

/*
template <typename T>
struct UnionFindTree{

    vector<T> par;

    UnionFindTree(T n) : par(n+1){
        for (int i = 1; i <= n; ++i) {
            par[i] = i;
        }
    }

    T root(T x){
        if(par[x] == x) return x;
        else  return par[x] = root(par[x]);
    }

    void unite(T x,T y){
        if(!same(x,y)) par[root(x)] = root(par[y]);
    }

    T same(T x,T y){
        return root(x) == root(y);
    }

};
*/

#define DEBUG

#ifdef DEBUG
#define debugl(x) cerr << #x << ":" << x << "\n"
#define debug(x) cerr << x
#define debugV(V)                                                              \
  REP(i, V.size()) { cerr << i << ":" << V[i] << endl; }
#define debugA(A)                                                              \
  REP(i, sizeof(A) / sizeof(ll)) { cerr << i << ":" << V[i] << endl; }
#else
#define debug(x)
#define debugV(x)
#define debugA(x)
#define debugl(x)
#endif

#define ZERO(a) memset(a, 0, sizeof(a))
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))

signed main() {

  ll H;
  ll W;
  IN(H);
  IN(W);
  string S[H + 2];
  string temp;

  S[0] = '#';
  S[H + 1] = '#';
  FOR(i, 0, W + 2) S[0] += '#';
  FOR(i, 0, W + 2) S[H + 1] += '#';

  // cerr << S[0] << endl;

  FOR(i, 1, H + 1) {
    S[i] = "#";
    cin >> temp;
    S[i] += temp;
    S[i] += "#";
    // cerr << S[i] << endl;
  }

  // cerr << S[H+1] << endl;

  vec2 hidari(H + 2, vec(W + 2, 0));
  vec2 migi(H + 2, vec(W + 2, 0));
  vec2 ue(H + 2, vec(W + 2, 0));
  vec2 sita(H + 2, vec(W + 2, 0));

  FOR(h, 1, H + 1) FOR(w, 1, W + 1) {
    if (S[h][w] == '#') {
      hidari[h][w] = 0;
    } else {
      hidari[h][w] = hidari[h][w - 1] + 1;
    }
  }

  FOR(w, 1, W + 1) FOR(h, 1, H + 1) {
    if (S[h][w] == '#') {
      ue[h][w] = 0;
    } else {
      ue[h][w] = ue[h - 1][w] + 1;
    }
  }

  for (int w = W; w >= 1; --w) {
    for (int h = H; h >= 1; --h) {
      if (S[h][w] == '#') {
        sita[h][w] = 0;
      } else {
        sita[h][w] = sita[h + 1][w] + 1;
      }
    }
  }

  for (int h = H; h >= 1; --h) {
    for (int w = W; w >= 1; --w) {
      if (S[h][w] == '#') {
        migi[h][w] = 0;
      } else {
        migi[h][w] = migi[h][w + 1] + 1;
      }
    }
  }

  ll ans = 0;
  ll num = 0;

  FOR(w, 1, W + 1) FOR(h, 1, H + 1) {
    if (S[h][w] == '.') {
      num = migi[h][w] + hidari[h][w] + sita[h][w] + ue[h][w] - 3;
      // cerr <<w<<h<<","<<
      // migi[h][w]<<","<<hidari[h][w]<<","<<sita[h][w]<<","<<ue[h][w]<<endl;
      amax(ans, num);
    }
  }

  OUT(ans);
}