r,c = gets.split(" ").map{|x| x.to_i}
arr = Array.new(r)
r_sum = Array.new(r)
c_sum = Array.new(c)
r.times do |i|
	r_sum[i] = 0
end
c.times do |i|
	c_sum[i] = 0
end
r.times do |i|
	arr[i] = gets.split(" ").map{|x| x.to_i}
end
r.times do |i|
	c.times do |j|
		r_sum[i] += arr[i][j]
	end
end
c.times do |i|
	r.times do |j|
		c_sum[i] += arr[j][i]
	end
end
arr2 = arr.flatten
all_sum = arr2.inject(:+)
r.times do |i|
	arr[i] = arr[i].join(" ") + " #{r_sum[i]}"
	puts arr[i]
end
c_sum = c_sum.join(" ") + " #{all_sum}"
puts c_sum