r,c = gets.split(" ").map(&:to_i)
array = []
r.times{
	linec_r = gets.split(" ").map(&:to_i)
	component_r = linec_r.inject(:+)
	array.push(linec_r.push(component_r))
}

sum_col = []
for i in 0..c
	sum = 0
	for j in 0..r-1
		sum += array[j][i] 
	end
	sum_col.push(sum)
end
array.each{|line|
    puts line.join(" ")
}
puts sum_col.join(" ")