Problem p03014 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define INFF 9223372036854775807
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define ALL(N) (N.begin(), N.end())
#define de cout << "debug" << endl;
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int H, W;
vector<vector<char>> glid(2003, vector<char>(2003));
int ue = 0, sita = 0;
int migi = 0, hida = 0;
int ans = 0;
int cnt = 0;

int solveup(int X, int Y) {
  if (glid[X - 1][Y] == '.') {
    ue++;
    solveup(X - 1, Y);
  } else {
    return ue;
  }
}

int solvedown(int X, int Y) {
  if (glid[X + 1][Y] == '.') {
    sita++;
    solvedown(X + 1, Y);
  } else {
    return sita;
  }
}

int solveright(int X, int Y) {
  if (glid[X][Y + 1] == '.') {
    migi++;
    solveright(X, Y + 1);
  } else {
    return migi;
  }
}

int solveleft(int X, int Y) {
  if (glid[X][Y - 1] == '.') {
    hida++;
    solveleft(X, Y - 1);
  } else {
    return hida;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  REPP(i, 1, H + 1) {
    REPP(j, 1, W + 1) { cin >> glid[i][j]; }
  }
  REP(i, H + 2) { glid[i][0] == '#'; }
  REP(i, W + 2) { glid[0][i] == '#'; }
  int maxv = 0;
  REP(i, H + 2) {
    REP(j, W + 2) {
      if (glid[i][j] == '.') {
        ans += solveup(i, j);
        ans += solvedown(i, j);
        ans += solveright(i, j);
        ans += solveleft(i, j);
        maxv = max(maxv, ans);
        ans = 0;
        ue = 0;
        sita = 0;
        migi = 0;
        hida = 0;
      }
    }
  }

  cout << maxv + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define INFF 9223372036854775807
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, m, n) for (int i = m; i < n; i++)
#define ALL(N) (N.begin(), N.end())
#define de cout << "debug" << endl;
#define pb push_back
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int H, W;
vector<vector<char>> glid(2003, vector<char>(2003));
int ue = 0, sita = 0;
int migi = 0, hida = 0;
int ans = 0;
int cnt = 0;

int solveup(int X, int Y) {
  if (glid[X - 1][Y] == '.') {
    ue++;
    solveup(X - 1, Y);
  } else {
    return ue;
  }
}

int solvedown(int X, int Y) {
  if (glid[X + 1][Y] == '.') {
    sita++;
    solvedown(X + 1, Y);
  } else {
    return sita;
  }
}

int solveright(int X, int Y) {
  if (glid[X][Y + 1] == '.') {
    migi++;
    solveright(X, Y + 1);
  } else {
    return migi;
  }
}

int solveleft(int X, int Y) {
  if (glid[X][Y - 1] == '.') {
    hida++;
    solveleft(X, Y - 1);
  } else {
    return hida;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;
  REPP(i, 1, H + 1) {
    REPP(j, 1, W + 1) { cin >> glid[i][j]; }
  }
  REP(i, H + 2) { glid[i][0] == '#'; }
  REP(i, W + 2) { glid[0][i] == '#'; }
  int maxv = 0;
  REP(i, H + 2) {
    REP(j, W + 2) {
      if (glid[i][j] == '.') {
        ans += solveup(i, j);
        ans += solvedown(i, j);
        ans += solveright(i, j);
        ans += solveleft(i, j);
        maxv = max(maxv, ans);
        if (maxv == H + W - 2) {
          cout << maxv + 1 << endl;
          return 0;
        }
        ans = 0;
        ue = 0;
        sita = 0;
        migi = 0;
        hida = 0;
      }
    }
  }

  cout << maxv + 1 << endl;
}