
tate, yoko = gets.split(" ").map(&:to_i)
arr = []

tate.times do
  temp = gets.split(" ").map(&:to_i)
  temp << temp.inject{ |val, mem| val + mem }
  arr << temp
end

i = 0
last_row = []
while i < arr.count + 2
  temp = 0
  arr.each do |arr2|
    temp += arr2[i]
  end
  last_row << temp
  i += 1
end

arr << last_row

arr.each do |arr2|
  arr2.each_with_index do |val, ind|
    if ind != last_row.count - 1
      print val.to_s + " "
    else
      print val
    end
  end
  print "\n"
end