num = []
r, c = gets.split.map(&:to_i)
sum_line = Array.new(c, 0)
r.times do
  num << gets.split.map(&:to_i)
end

num.each_with_index do |m, i|
  puts num[i].join(" ") + " " + num[i].inject(:+).to_s
  sum_line.map!.with_index {|arr, j| arr + num[i][j]}
end
 puts sum_line.join(" ") + " " + sum_line.inject(:+).to_s