l=readlines
r=Array.new(l.shift.split(' ').last.to_i, 0)
l.map{|a|a.split(' ').map(&:to_i)}.each do |a|
  puts (a+[a.inject(&:+)])*' '
  a.each_with_index{|v,i| r[i]+=v}
end
puts (r+[r.inject(&:+)])*' '