r, c = gets.split.map(&:to_i)
sum = [0] * c
r.times do
  s = 0
  tmp = gets.split.map(&:to_i)
  c.times do |j|
    s += tmp[j]
    sum[j] += tmp[j]
    printf("%d ", tmp[j])
  end
  puts s
end
s = 0
for x in sum
  s += x
  printf("%d ", x)
end
puts s

