var
i,j,k,w,tt,vv,n,m,x,y,p,t1,t2:longint;
r:array[0..400,0..400]of longint;
father:array[0..400]of longint;
v:array[0..6000]of boolean;
t:array[0..6000,0..50]of boolean;
c:array[0..6000,0..3]of longint;
f:array[0..60]of longint;
procedure sort(l,r:longint);
var
i,j,x,tt:longint;
begin
i:=l;
j:=r;
x:=c[(i+j)div 2,0];
repeat
while (c[i,0]<x) do
i:=i+1;
while (c[j,0]>x) do
j:=j-1;
if (i<=j) then
begin
tt:=c[i,0];
c[i,0]:=c[j,0];
c[j,0]:=tt;
tt:=c[i,1];
c[i,1]:=c[j,1];
c[j,1]:=tt;
tt:=c[i,2];
c[i,2]:=c[j,2];
c[j,2]:=tt;
i:=i+1;
j:=j-1;
end;
until (i>j);
if (l<j) then sort(l,j);
if (i<r) then sort(i,r);
end;
function getfather(i:longint):longint;
begin
if (father[i]=i) then exit(i);
father[i]:=getfather(father[i]);
exit(father[i]);
end;
function cost(b,e:longint):longint;
var
i,j,now,k:longint;
begin
cost:=0;
fillchar(v,sizeof(v),false);
for i:=1 to n do
father[i]:=i;
for i:=1 to m do
for j:=b to e do
if (t[i,j]) then
v[i]:=true;
i,j,k,w,tt,vv,n,m,x,y,p,t1,t2:longint;
r:array[0..400,0..400]of longint;
father:array[0..400]of longint;
v:array[0..6000]of boolean;
t:array[0..6000,0..50]of boolean;
c:array[0..6000,0..3]of longint;
f:array[0..60]of longint;
procedure sort(l,r:longint);
var
i,j,x,tt:longint;
begin
i:=l;
j:=r;
x:=c[(i+j)div 2,0];
repeat
while (c[i,0]<x) do
i:=i+1;
while (c[j,0]>x) do
j:=j-1;
if (i<=j) then
begin
tt:=c[i,0];
c[i,0]:=c[j,0];
c[j,0]:=tt;
tt:=c[i,1];
c[i,1]:=c[j,1];
c[j,1]:=tt;
tt:=c[i,2];
c[i,2]:=c[j,2];
c[j,2]:=tt;
i:=i+1;
j:=j-1;
end;
until (i>j);
if (l<j) then sort(l,j);
if (i<r) then sort(i,r);
end;
function getfather(i:longint):longint;
begin
if (father[i]=i) then exit(i);
father[i]:=getfather(father[i]);
exit(father[i]);
end;
function cost(b,e:longint):longint;
var
i,j,now,k:longint;
begin
cost:=0;
fillchar(v,sizeof(v),false);
for i:=1 to n do
father[i]:=i;
for i:=1 to m do
for j:=b to e do
if (t[i,j]) then
v[i]:=true;