numebrs = gets.split.map(&:to_i)
matrix = []
numebrs[0].times do
    matrix.push(gets.split.map(&:to_i))
end
numebrs[0].times do |index|
    matrix[index].push(matrix[index].inject(:+))
end
new_array = []
(numebrs[1]+1).times do |index_c|
    temp = 0
    numebrs[0].times do |index_r|
        temp += matrix[index_r][index_c]
    end
    new_array.push(temp)
end
matrix.push(new_array)
matrix.each do |array|
    puts array.join(" ")
end
