
m,n = gets.split.map(&:to_i)
mat = []

for i in 1..m
temp = gets.split.map(&:to_i)
temp.push(temp.inject(:+))
mat.push(temp)
puts temp.join(" ")
end

temp = []
for i in 0..n

sum = 0
for j in 0..(m-1)
sum += mat[j][i]
end

temp.push(sum)

end

puts temp.join(" ")


