r, c = gets.split.map(&:to_i)

table = []
r.times do |i|
	table[i] = gets.split.map(&:to_i)
end

row_sum = 0
r.times do |i|
	c.times do |j|
		row_sum += table[i][j]
	end
	table[i].push(row_sum)
	row_sum = 0
end

column_sum = 0
table.push([])
(c+1).times do |j|
	r.times do |i|
		column_sum += table[i][j]
	end
	table[-1].push(column_sum)
	column_sum = 0
end

(r+1).times do |i|
	(c+1).times do |j|
		print "#{table[i][j]} "
	end
	print "\n"
end