r,c = gets.split(" ").map do |i| i.to_i end
spreadsheet = Array.new(r+1)
(0..r).each do |r|
    input = gets.to_s
    spreadsheet[r] = input.split(" ").map do |i| i.to_i end
    spreadsheet[r].push(spreadsheet[r].inject(:+))
end

spreadsheet[-1] = Array.new(c+1)
spreadsheet[-1].fill(0)

(0..c).each do |c|
    (0..r-1).each do |r|
        spreadsheet[-1][c] += spreadsheet[r][c]
    end
end

(0..r).each do |r|
    puts "#{spreadsheet[r].join(" ")}"
end