var a:array[1..9000,1..9000]of longint;
b,c:array[1..9000]of longint;
n,m,i,j,t,temp:longint;
begin
writeln('nhap lan luot so hang va so cot');readln(n,m);
fillchar(c,sizeof(c),0);
for i:=1 to n do begin
writeln('nhap lan luot m phan tu cua cot ',i);
for j:=1 to m do begin
readln(a[i,j]);
c[i]:=c[i]+a[i,j];
end;
for j:=1 to m-1 do
for t:=j+1 to m do
if a[j]>a[t] then begin
temp:=a[j];
a[j]:=a[t];
a[t]:=temp;
end;
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if c[i]>c[j] then begin
for t:=1 to m do
b[t]:=a[i,t];
for t:=1 to m do
a[i,t]:=a[j,t];
for t:=1 to m do
a[j,t]:=b[t];
temp:=c[i];
c[i]:=c[j];
c[j]:=temp;
end;
writeln('mang hoan chinh');
for i:=1 to n do begin
for j:=1 to m do
write(a[i,j],' ');
writeln;
end;
readln;
end.