uses crt;
const nmax=20;
var a:array[1..nmax,1..nmax] of integer;
m,n,i,j:byte;
sm,so:integer;
begin
clrscr;
randomize;
repeat
write('Количество строк до ',nmax-1,' m=');
readln(m);
until m in [1..nmax];
repeat
write('Количество столбцов до ',nmax-1,' n=');
readln(n);
until n in [1..nmax];
writeln('Исходная матрица:');
so:=0;
for i:=1 to m do
begin
sm:=0;
for j:=1 to n do
begin
a[i,j]:=random(8)+1;
write(a[i,j]:3);
sm:=sm+a[i,j];
end;
writeln;
a[i,n+1]:=sm;
so:=so+sm;
end;
writeln;
for j:=1 to n do
begin
sm:=0;
for i:=1 to m do
sm:=sm+a[i,j];
a[m+1,j]:=sm;
end;
a[m+1,n+1]:=so;
writeln('Расширенная матрица:');
writeln('Сумма':n*3+14);
for i:=1 to m do
begin
write(' ':6);
for j:=1 to n+1 do
if j=n+1 then write(a[i,j]:6)
else write(a[i,j]:3);
writeln;
end;
writeln;
write('Сумма:');
for j:=1 to n+1 do
if j=n+1 then write(a[m+1,j]:6)
else write(a[m+1,j]:3);
readln
end.