r, c = gets.chomp.split.map(&:to_i)
data = Array.new(r + 1){Array.new(c + 1, 0)}
r.times{|x|
    data[x] = gets.chomp.split.map(&:to_i)
    sum = data[x].inject(:+)
    data[x] << sum
    data[x].each_with_index{|number, i| data[r][i] += number}
}
data.each{|row| puts row.join(" ")}
