r, c = gets.chomp.split.map(&:to_i)

arr = []

r.times do
  temp = gets.chomp.split.map(&:to_i)
  temp << temp.sum
  arr <<  temp
end

add_arr = Array.new(c + 1)
for i in 0..c do
  temp = 0
  arr.each do |c_arr|
    temp += c_arr[i]
  end
  add_arr[i] = temp
end
arr << add_arr

arr.each do |c_arr|
  puts c_arr.join(" ")
end
