r,c = gets.chomp.split.map(&:to_i)
t = Array.new(r){Array.new(c, 0)}
s = Array.new(c+1, 0)
t.each do |ti|
  ti.replace(gets.chomp.split.map(&:to_i))
  ti.push(ti.inject(:+))
  ti.each_with_index do |item, index| s[index] += item end
end
t.push(s)
t.each do |item| puts item*" " end