
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 = []

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

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