r,c = gets.split.map(&:to_i)
arr = Array.new()
sum = Array.new()
r.times{
    |i|
    arr[i] = gets.split.map(&:to_i)
    sum[i] = arr[i].inject(:+)
}
r.times{
    |i|
    puts "#{arr[i].join(" ")} #{sum[i]}"
}
n = arr.transpose
c.times{ |i|
print n[i].inject(:+)
print " "
}
puts "#{sum.inject(:+)}"
