row, col = gets.chomp.split.map(&:to_i)

ary = Array.new(row) do
    Array.new(col)
end

row.times do |r|
    ary[r] = gets.chomp.split.map(&:to_i)
end

ary.each do |r|
    rowsum = 0
    r.each do |i|
        print "#{i} "
        rowsum += i
    end
    puts rowsum
end

colsum = Array.new(col, 0)
col.times do |c|
    ary.each do |r|
        colsum[c] += r[c]
    end
end

allsum = 0
colsum.each do |cs|
    print "#{cs} "
    allsum += cs
end
puts allsum