一道单调栈好题

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define gc getchar()
using namespace std;
const int N=1e3+10;
inline void qr(int &x)
{
    x=0;char c=gc;
    while(c<'0'||c>'9')c=gc;
    while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;}
}
void qw(int x)
{
    if(x<0)x=-x,putchar('-');
    if(x/10)qw(x/10);
    putchar(x%10+48);
}
char mp[N][N],s[N][N];int n,m;
inline void change(int i)//1--->a 2--->b 3--->c
{
    if(i==1)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(mp[i][j]=='w'||mp[i][j]=='y'||mp[i][j]=='z')
                    s[i][j]='a';
                else
                    s[i][j]=mp[i][j];
    }
    else if(i==2)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(mp[i][j]=='w'||mp[i][j]=='x'||mp[i][j]=='z')
                    s[i][j]='b';
                else
                    s[i][j]=mp[i][j];
    }
    else
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(mp[i][j]=='x'||mp[i][j]=='y'||mp[i][j]=='z')
                    s[i][j]='c';
                else
                    s[i][j]=mp[i][j];
    }
}
int h[N],l[N],r[N];
inline int solve(char c)
{
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++){l[j]=j,r[j]=j;if(s[i][j]==c)h[j]++;else h[j]=0;}
        r[m+1]=m+1;h[m+1]=h[0]=-1e9;
        for(int j=2;j<=m;j++)
            while(h[j]<=h[l[j]-1])l[j]=l[l[j]-1];
        for(int j=m-1;j>=1;j--)
            while(h[j]<=h[r[j]+1])r[j]=r[r[j]+1];
        for(int i=1;i<=m;i++)
            ans=max(ans,(r[i]-l[i]+1)*h[i]);
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int ans=0;
        for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
        change(1);memset(h,0,sizeof(h));
        ans=max(ans,solve('a'));
        change(2);memset(h,0,sizeof(h));
        ans=max(ans,solve('b'));
        change(3);memset(h,0,sizeof(h));
        ans=max(ans,solve('c'));
        qw(ans);puts("");
    }
    return 0;
}