uses crt; var t1,t:text; q,i,j,m,n:integer; a:array [1..100,1..100] of integer; begin clrscr; assign(t,'input.dat'); reset(t); read(t,n); read(t,m); for i:=1 to n do for j:=1 to m do read(t,a[i,j]); close(t); q:=a[n,1]; for i:=1 to n do begin for j:=m downto 1 do begin if (a[i,j]>a[i+1,j]) and (a[i-1,j]>a[i,j-1]) then q:=q+a[i+1,j]; if (a[i,j]>a[i,j-1]) and (a[i,j+1]>a[i+1,j]) then q:=q+a[i,j+1]; end; end; assign(t1,'input.sol'); rewrite(t1); for i:=1 to n do begin for j:=1 to m do begin write(t1,a[i,j],' '); end; writeln(t1,' '); end; writeln(t1,q); close(t1); end.