Problem p03630 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>

// int mod=1e9+7;
// inline int mul(int a,int b){return (a*1ll*b)%mod;}
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
// inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
// inline int power(int a,int b){int
// rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;} inline int
// inv(int a){return power(a,mod-2);} inline void modadd(int &a,int
// &b){a+=b;if(a>=mod)a-=mod;}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  int H, W;
  cin >> H >> W;
  int i, j, k;
  int ans = max(H, W);
  if (H == 1 || W == 1) {
    cout << ans;
    return 0;
  }
  vi chn(W - 1, -1);
  vi len(W - 1, 0);
  for (j = 0; j < H; j++) {
    string s;
    cin >> s;
    for (i = 0; i < W - 1; i++) {
      int c = s[i] == s[i + 1] ? 0 : 1;
      if (c == chn[i] or chn[i] == -1)
        len[i]++;
      else
        len[i] = 1;
      chn[i] = c;
    }
    vi lef(W - 1, 0);
    vi rig(W - 1, 0);
    vi cur;
    // trace(len);
    for (i = 0; i < W - 1; i++) {
      int cu = len[i];
      while (!cur.empty()) {
        int x = cur.back();
        if (len[x] <= cu)
          break;
        rig[x] = i - x;
        cur.pop_back();
      }
      cur.pb(i);
    }
    for (auto z : cur)
      rig[z] = W - z - 1;

    cur.clear();
    for (i = W - 1; i >= 0; i--) {
      int cu = len[i];
      while (!cur.empty()) {
        int x = cur.back();
        if (len[x] <= cu)
          break;
        lef[x] = x - i;
        cur.pop_back();
      }
      cur.pb(i);
    }
    for (auto z : cur)
      lef[z] = 1 + z;
    // trace(lef, rig);
    for (i = 0; i < W - 1; i++)
      ans = max(ans, len[i] * (lef[i] + rig[i]));
  }
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif

#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pii>

// int mod=1e9+7;
// inline int mul(int a,int b){return (a*1ll*b)%mod;}
// inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
// inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
// inline int power(int a,int b){int
// rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;} inline int
// inv(int a){return power(a,mod-2);} inline void modadd(int &a,int
// &b){a+=b;if(a>=mod)a-=mod;}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  int H, W;
  cin >> H >> W;
  int i, j, k;
  int ans = max(H, W);
  if (H == 1 || W == 1) {
    cout << ans;
    return 0;
  }
  vi chn(W - 1, -1);
  vi len(W - 1, 0);
  for (j = 0; j < H; j++) {
    string s;
    cin >> s;
    for (i = 0; i < W - 1; i++) {
      int c = s[i] == s[i + 1] ? 0 : 1;
      if (c == chn[i] or chn[i] == -1)
        len[i]++;
      else
        len[i] = 1;
      chn[i] = c;
    }
    vi lef(W - 1, 0);
    vi rig(W - 1, 0);
    vi cur;
    // trace(len);
    for (i = 0; i < W - 1; i++) {
      int cu = len[i];
      while (!cur.empty()) {
        int x = cur.back();
        if (len[x] <= cu)
          break;
        rig[x] = i - x;
        cur.pop_back();
      }
      cur.pb(i);
    }
    for (auto z : cur)
      rig[z] = W - z - 1;

    cur.clear();
    for (i = W - 2; i >= 0; i--) {
      int cu = len[i];
      while (!cur.empty()) {
        int x = cur.back();
        if (len[x] <= cu)
          break;
        lef[x] = x - i;
        cur.pop_back();
      }
      cur.pb(i);
    }
    for (auto z : cur)
      lef[z] = 1 + z;
    // trace(lef, rig);
    for (i = 0; i < W - 1; i++)
      ans = max(ans, len[i] * (lef[i] + rig[i]));
  }
  cout << ans << "\n";
}