r, c = gets.split( " " ).map { | val | val.to_i }

csums = Array.new( c, 0)
rsum = 0
r.times {
	matrix = gets.split( " " ).map { | val | val.to_i }

	rsum = 0
	for i in ( 0 ... c )
		rsum += matrix[i]
		print( "#{matrix[i]} " )
		csums[i] += matrix[i]
	end
	puts( rsum )
}

rsum = 0
csums.each { |val|
	rsum += val
	print( "#{val} " )
}
puts( rsum )