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

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

	rsum = 0
	for i in ( 0 ... c )
		rsum += matrix[i]
		csums[i] += matrix[i]
		output.push( matrix[i] )
		output.push( " " )		
	end
	output.pop
	output.push( "#{rsum}\n" )
}

rsum = 0
csums.each { |val|
	rsum += val
	output.push( val )
	output.push( " " )			
}
output.push( "\n" )	
output.push( "#{rsum}\n" )
print( output.join )