Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define reps(i, x) for (int i = 1; i <= (int)(x); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = ((int)(x)); i > 0; i--)

#define all(x) (x).begin(), (x).end()

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using ll = long long;

int main() {
  // 入力、初期化
  int H, W;
  cin >> H >> W;
  vector<vector<int>> cnt(H, vector<int>(W));
  vector<string> s(H);
  rep(i, H) { cin >> s[i]; }

  rep(i, H) {
    vector<int> done(W);
    rep(j, W) {
      if (s[i][j] == '#')
        continue;
      if (done[j])
        continue;

      int l = 0;
      while (j + l < W) {
        if (s[i][j + l] == '#')
          break;
        l++;
      }

      rep(k, l) {
        cnt[i][j + k] += l;
        done[j + k] = 1;
      }
    }
  }
  rep(j, W) {
    vector<int> done(W);
    rep(i, H) {
      if (s[i][j] == '#')
        continue;
      if (done[i])
        continue;

      int l = 0;
      while (i + l < H) {
        if (s[i + l][j] == '#')
          break;
        l++;
      }

      rep(k, l) {
        cnt[i + k][j] += l;
        done[i + k] = 1;
      }
    }
  }

  int ans = 0;
  rep(i, H) rep(j, W) ans = max(ans, cnt[i][j] - 1);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define reps(i, x) for (int i = 1; i <= (int)(x); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = ((int)(x)); i > 0; i--)

#define all(x) (x).begin(), (x).end()

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using ll = long long;

int main() {
  // 入力、初期化
  int H, W;
  cin >> H >> W;
  vector<vector<int>> cnt(H, vector<int>(W));
  vector<string> s(H);
  rep(i, H) { cin >> s[i]; }

  rep(i, H) {
    vector<int> done(W);
    rep(j, W) {
      if (s[i][j] == '#')
        continue;
      if (done[j])
        continue;

      int l = 0;
      while (j + l < W) {
        if (s[i][j + l] == '#')
          break;
        l++;
      }

      rep(k, l) {
        cnt[i][j + k] += l;
        done[j + k] = 1;
      }
    }
  }
  rep(j, W) {
    vector<int> done(H);
    rep(i, H) {
      if (s[i][j] == '#')
        continue;
      if (done[i])
        continue;

      int l = 0;
      while (i + l < H) {
        if (s[i + l][j] == '#')
          break;
        l++;
      }

      rep(k, l) {
        cnt[i + k][j] += l;
        done[i + k] = 1;
      }
    }
  }

  int ans = 0;
  rep(i, H) rep(j, W) ans = max(ans, cnt[i][j] - 1);
  cout << ans << endl;
  return 0;
}