r, c = gets.split.map(&:to_i)
mat = []
(1..r).each do |_x|
  mat << gets.split.map(&:to_i)
end

mat.each do |row|
  row << row.inject(:+)
end
mat[r] = []
(0..c).each do |col|
  sum = 0
  (0..r - 1).each do |row|
    sum += mat[row][col]
  end
  mat[r] << sum
end

mat.each do |row|
  puts row.join(' ')
end