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

array = []

while input = gets
    tmp = input.chomp.split(" ").map(&:to_i)
    rsum = 0
    tmp.each {|x| rsum += x}
    puts "#{input.chomp} #{rsum}"
    
    tmp << rsum
    array << tmp
end

for i in 0..c
    csum = 0
    for j in 0..r - 1
        csum += array[j][i]
    end
    print csum
    if i != c
        print " "
    end
end
puts ""
