r,c=gets.chomp.split(" ").map(&:to_i)
#2重リストの宣言
list=Array.new(r+1).map{Array.new(c+1)}
for i in 0..r-1 do
input=gets.chomp.split(" ").map(&:to_i)
for j in 0..c-1 do
list[i][j]=input[j]
end
list[i][c]=input.sum
end

for j in 0..c do
 sum=0
 for i in 0..r-1 do
  sum=sum+list[i][j]
 end
 list[r][j]=sum
end

for i in 0..r do
print"#{list[i].join(' ')}\n"
end 
