二维的二分答案。
代码:
var
r,c,a,b,i,j,li,re,mid:longint;
d,s:array[0..510,0..510]of longint;
function cal(x1,y1,x2,y2:longint):longint;
begin
exit(s[x2,y2]-s[x2,y1-1]-s[x1-1,y2]+s[x1-1,y1-1]);
end;
function check(x:longint):boolean;
var
i,j,lasta,lastb,na,nb:longint;
begin
lasta:=1;
for na:=1 to a do
begin
for i:=lasta to r+1 do
begin
lastb:=1;
for nb:=1 to b do
begin
for j:=lastb to c+1 do
if cal(lasta,lastb,i,j)>=x then break;
lastb:=j+1;
if lastb>c then break;
end;
if (nb=b)and(lastb<=c+1) then break;
end;
lasta:=i+1;
if lasta>r then break;
end;
exit((na=a)and(lasta<=r+1));
end;
begin
readln(r,c,a,b);
for i:=1 to r do
for j:=1 to c do
begin
read(s[i,j]);
s[i,j]:=s[i,j]+s[i-1,j]+s[i,j-1]-s[i-1,j-1];
end;
li:=0;
re:=1000000000;
while li<re do
begin
mid:=(li+re)div 2;
if check(mid) then li:=mid+1
else re:=mid;
end;
writeln(li-1);
end.