r, c = gets.split.map(&:to_i)
rows = r.times.map { gets.split.map(&:to_i) }

rows.each do |row|
  print row.join(' ')
  print ' ', row.inject(:+), "\n"
end

sums = c.times.map { |i| rows.map {|row| row[i] }.inject(:+) }
print sums.join(' ')
print ' ', sums.inject(:+), "\n"