program bai2_de_12_vong1;
uses crt;
const nmax=20;
var a:array[1..nmax,1..nmax]of integer; i,j,m,n,k:integer;
b:array[1..nmax*nmax] of integer;
fi:text;
procedure enter;
begin
Assign(fi,'C:\FPC\2.4.2\bin\i386-win32\inp.txt');
reset(fi);
Readln(fi,n,m);
for i:=1 to n do
begin
for j:=1 to m do Read(fi,A[i,j]);
Readln(fi);
end;
end;
procedure swap(var a,b:integer);
var tg:integer;
begin
tg:=a;
a:=b;
b:=tg;
end;
Procedure matrix2mang;
var i,j:integer;
begin
k:=0;
for i:=1 to n do
for j:=1 to m do
begin
inc(k);
b[k]:=A[i,j];
end;
end;
procedure sapxep;
begin
for i:=1 to k-1 do
for j:=k downto i+1 do
if b[j-1]>b[j] then
swap(b[j],b[j-1]);
end;
procedure mang2matrix;
begin
k:=0;
for i:=1 to n do
for j:=1 to m do
begin
inc(k);
a[i,j]:=b[k];
end;
end;
procedure xuat;
begin
for i:=1 to n do
begin
for j:=1 to m do
Write(a[i,j],' ');
Writeln;
end;
end;
Begin clrscr;
enter;
matrix2mang;
sapxep;
mang2matrix;
xuat;
close(fi);
readln;
end.