Problem p00116 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int W, H;

struct RECT {
  int height, pos;
};

int solve(int data[]) {
  int ret = 0;
  data[W] = 0;
  RECT t;
  stack<RECT> S;
  for (int i = 0; i <= W; i++) {
    t.height = data[i];
    t.pos = i;
    if (S.empty()) {
      S.push(t);
    } else {
      if (data[i] > S.top().height) {
        S.push(t);
      } else if (data[i] < S.top().height) {
        int here = i - 1;
        while (!S.empty() && S.top().height > data[i]) {
          RECT s = S.top();
          S.pop();
          t.pos = s.pos;
          int width = here - s.pos + 1;
          ret = max(ret, width * s.height);
        }
        S.push(t);
      }
    }
  }

  return ret;
}

int main() {
  while (cin >> H >> W, H) {
    string s;
    getline(cin, s);
    int ret = 0;

    int dp[501][501] = {{0}};
    for (int i = 1; i <= H; i++) {
      getline(cin, s);
      rep(j, s.length()) {
        if (s[j] == '.')
          dp[i][j] = dp[i - 1][j] + 1;
      }
    }
    for (int i = 1; i <= H; i++) {
      ret = max(solve(dp[i]), ret);
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  bool y_stop, x_stop;
  char data[502][502];
  int h, w, x, y, ret;
  while (cin >> h >> w && h || w) {
    ret = 0;

    rep(i, 502) rep(j, 502) data[i][j] = '*';

    rep(i, h) rep(j, w) cin >> data[i + 1][j + 1];

    rep(Y, h + 1) rep(X, w + 1) {
      y_stop = x_stop = false;
      y = x = 0;
      if (data[Y][X] == '*')
        continue;
      while (!y_stop || !x_stop) {
        if (!y_stop)
          y++;
        if (!x_stop)
          x++;
        rep(i, y) if (data[Y + i][X + x] == '*') { x_stop = true; }
        rep(i, x) if (data[Y + y][X + i] == '*') { y_stop = true; }
      }
      //			cout << X << "," << Y << "=" << x*y << endl;
      ret = max(x * y, ret);
      y_stop = x_stop = false;
      y = x = 0;
      if (data[Y][X] == '*')
        continue;
      while (!y_stop || !x_stop) {
        if (!y_stop)
          y++;
        if (!x_stop)
          x++;
        rep(i, y) if (data[Y - i][X - x] == '*') { x_stop = true; }
        rep(i, x) if (data[Y - y][X - i] == '*') { y_stop = true; }
      }
      //			cout << X << "," << Y << "=" << x*y << endl;
      ret = max(x * y, ret);

      y_stop = x_stop = false;
      y = x = 0;
      if (data[Y][X] == '*')
        continue;
      while (!y_stop || !x_stop) {
        if (!y_stop)
          y++;
        if (!x_stop)
          x++;
        rep(i, y) if (data[Y - i][X - x] == '*') { x_stop = true; }
        rep(i, x) if (data[Y + y][X + i] == '*') { y_stop = true; }
      }
      //			cout << X << "," << Y << "=" << x*y << endl;
      ret = max(x * y, ret);
      y_stop = x_stop = false;
      y = x = 0;
      if (data[Y][X] == '*')
        continue;
      while (!y_stop || !x_stop) {
        if (!y_stop)
          y++;
        if (!x_stop)
          x++;
        rep(i, y) if (data[Y + i][X + x] == '*') { x_stop = true; }
        rep(i, x) if (data[Y - y][X - i] == '*') { y_stop = true; }
      }
      //			cout << X << "," << Y << "=" << x*y << endl;
      ret = max(x * y, ret);
    }
    cout << ret << endl;
  }
}