Problem p03630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using LL = long long int;
const LL mod = 1000000007;

#define int long long

const int MAX_H = 1405;
const int MAX_W = 1405;
int H, W;
char C[MAX_H][MAX_W];

struct Rect {
  int height, pos;
};

int HMap[MAX_H][MAX_W];

int dx[] = {-1, 0, -1, 0};
int dy[] = {-1, -1, 0, 0};

int counter(int x, int y) {
  int ret = 0;
  for (int d = 0; d < 4; d++) {
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (C[ny][nx] == '#') {
      ret++;
    }
  }
  return ret;
}

signed main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
    }
  }

  for (int x = 0; x < W; x++) {
    int h = 1;
    for (int y = 0; y < H; y++) {
      if (x == W - 1) {
        HMap[y][x] = y + 1;
      } else {
        if (y == 0) {
          h = 1;
        } else {
          if (counter(x + 1, y) % 2 == 0) {
            h++;
          } else {
            h = 1;
          }
        }
        HMap[y][x] = h;
      }
    }
  }

  // for(int y = 0; y < H; y++) {
  //     for(int x = 0; x < W; x++) {
  //         cerr << HMap[y][x] << " ";
  //     }
  //     cerr << endl;
  // }

  int ans = max(H, W);

  for (int y = 0; y < H; y++) {
    stack<Rect> S;
    for (int x = 0; x < W; x++) {
      Rect rect;
      rect.pos = x;
      rect.height = HMap[y][x];
      if (S.empty()) {
        S.push(rect);
      } else if (S.top().height <= rect.height) {
        S.push(rect);
      } else { // 長方形最大値を求める
        while (S.size() > 0) {
          Rect r = S.top();
          if (r.height > rect.height) {
            S.pop();
            ans = max(ans, r.height * (x - r.pos + 1));
            rect.pos = r.pos;
          } else {
            break;
          }
        }
        S.push(rect);
      }
    }
    while (S.size() > 0) {
      Rect r = S.top();
      S.pop();
      ans = max(ans, r.height * (W - r.pos));
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using LL = long long int;
const LL mod = 1000000007;

#define int long long

const int MAX_H = 2005;
const int MAX_W = 2005;
int H, W;
char C[MAX_H][MAX_W];

struct Rect {
  int height, pos;
};

int HMap[MAX_H][MAX_W];

int dx[] = {-1, 0, -1, 0};
int dy[] = {-1, -1, 0, 0};

int counter(int x, int y) {
  int ret = 0;
  for (int d = 0; d < 4; d++) {
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (C[ny][nx] == '#') {
      ret++;
    }
  }
  return ret;
}

signed main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
    }
  }

  for (int x = 0; x < W; x++) {
    int h = 1;
    for (int y = 0; y < H; y++) {
      if (x == W - 1) {
        HMap[y][x] = y + 1;
      } else {
        if (y == 0) {
          h = 1;
        } else {
          if (counter(x + 1, y) % 2 == 0) {
            h++;
          } else {
            h = 1;
          }
        }
        HMap[y][x] = h;
      }
    }
  }

  // for(int y = 0; y < H; y++) {
  //     for(int x = 0; x < W; x++) {
  //         cerr << HMap[y][x] << " ";
  //     }
  //     cerr << endl;
  // }

  int ans = max(H, W);

  for (int y = 0; y < H; y++) {
    stack<Rect> S;
    for (int x = 0; x < W; x++) {
      Rect rect;
      rect.pos = x;
      rect.height = HMap[y][x];
      if (S.empty()) {
        S.push(rect);
      } else if (S.top().height <= rect.height) {
        S.push(rect);
      } else { // 長方形最大値を求める
        while (S.size() > 0) {
          Rect r = S.top();
          if (r.height > rect.height) {
            S.pop();
            ans = max(ans, r.height * (x - r.pos + 1));
            rect.pos = r.pos;
          } else {
            break;
          }
        }
        S.push(rect);
      }
    }
    while (S.size() > 0) {
      Rect r = S.top();
      S.pop();
      ans = max(ans, r.height * (W - r.pos));
    }
  }

  cout << ans << endl;

  return 0;
}