传送门:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
#define re register
#define pb push_back
const int N=1005;
inline void read(int &a)
{
a=0;
int d=1;
char ch;
while(ch=getchar(),ch>'9'||ch<'0')
if(ch=='-')
d=-1;
a=ch^48;
while(ch=getchar(),ch>='0'&&ch<='9')
a=(a<<3)+(a<<1)+(ch^48);
a*=d;
}
int a[N][N],minq1[N][N],minq2[N][N],q[N],x[N][N],y[N][N];
int main()
{
int n,m,A,B,C,D,h1,h2,t1,t2;
read(n);
read(m);
read(A);
read(B);
read(C);
read(D);
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
{
read(a[i][j]);
a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
}
for(re int i=C+1;i<n;i++)
for(re int j=D+1;j<m;j++)
y[i][j]=a[i][j]-a[i-C][j]-a[i][j-D]+a[i-C][j-D];
for(re int i=A;i<=n;i++)
for(re int j=B;j<=m;j++)
x[i][j]=a[i][j]-a[i-A][j]-a[i][j-B]+a[i-A][j-B];
for(re int i=C+1;i<n;i++)
{
t1=h1=1;
for(re int j=D+1;j<m;j++)
{
while(h1<t1&&j-q[h1]>=B-2-D)
h1++;
while(h1<t1&&y[i][q[t1-1]]>=y[i][j])
t1--;
q[t1++]=j;
if(j+1>=B)
minq1[i][j+1]=y[i][q[h1]];
}
}
for(re int i=B;i<=m;i++)
{
t1=h1=1;
for(re int j=C+1;j<n;j++)
{
while(h1<t1&&j-q[h1]>=A-2-C)
h1++;
while(h1<t1&&minq1[q[t1-1]][i]>=minq1[j][i])
t1--;
q[t1++]=j;
if(j+1>=A)
minq2[j+1][i]=minq1[q[h1]][i];
}
}
int ans=0;
for(re int i=A;i<=n;i++)
for(re int j=B;j<=m;j++)
ans=max(ans,x[i][j]-minq2[i][j]);
printf("%d",ans);
return 0;
}