def array_integer(a)
	0.upto(a.count) {|x|
		a[x] = a[x].to_i
	}
	return a
end

r,c = array_integer(STDIN.gets.split(" "))

matrix = Array.new(r+1).map{Array.new(c+1, 0)}
vertical_sum = Array.new(c, 0)

0.upto(r-1) {|i|
	matrix[i] = array_integer(STDIN.gets.split(" "))
	matrix[i][c] = matrix[i].sum
	0.upto(c-1) {|j|
		vertical_sum[j] += matrix[i][j]
	}
}
matrix[r] = vertical_sum
matrix[r][c] = vertical_sum.sum

0.upto(r) {|i|
	0.upto(c) {|j|
		print matrix[i][j]
		print " " if j != c
	}
	print "\n"
}

