r, c = gets.split(" ").map(&:to_i)
hyou = Array.new(r){
  arr = gets.split(" ").map(&:to_i)
  arr << arr.inject(&:+)
}
arr = []
(c + 1).times do |i|
  arr << hyou.inject(0){|sum, item|
    sum + item[i]
  }
  end
hyou.each{|item|puts item.join(" ")}
puts arr.join(" ")
    