r,c = gets.chomp.split(" ").map &:to_i

rows = Array.new()
r.times {
    data = gets.chomp.split(" ").map &:to_i
    data << data.inject(:+)
    rows << data
}

total = Array.new(c+1, 0)

rows.each { |rw|
    rw.length.times { |i|
        total[i] += rw[i]
        print " " if i != 0
        print "#{rw[i]}"
    }
    puts
}

total.length.times { |i|
    print " " if i != 0
    print "#{total[i]}"
}
puts