r,c  = gets.chomp.split(" ").map{|item| item.to_i}
table = Array.new(r+1).map{ Array.new(c+1,0) }
r.times do |y|
    table[y] = gets.chomp.split(" ").map{|item| item.to_i}
    table[y][c]=0
    
    c.times do |x|
        table[y][c]+=table[y][x]
        table[r][x]+=table[y][x]
    end
    table[r][c]+=table[y][c]
end

(r+1).times do |y|
    (c+1).times do |x|
        if c!=0 
            print(" ")
        end
        print(table[y][x])
    end
    puts
end