matrix = []

r, c = $stdin.readline.split(" ").map {|s| s.to_i }

(1..r).each do |row|
  cols = $stdin.readline.split(" ").map {|s| s.to_i }
  matrix.push(cols)
end

matrix.each do |row|
  print row.join(" ")
  print " "
  puts row.reduce(0) {|result, col| result + col }
end

total = 0
(1..c).each do |col|
  sum = 0
  (1..r).each do |row|
    sum += matrix[row-1][col-1]
    total += matrix[row-1][col-1]
  end
  print sum.to_s + " "
end

puts total

