r, c = gets.split(" ").map(&:to_i)
a = Array.new(r+1).map{Array.new(c+1, 0)}

r.times { |i|
    a[i] = gets.split.map(&:to_i)
    a[i][c] = a[i].inject{ |sum, j| sum + j}
}

for k in 0..c
    for h in 0..r-1
        a[r][k] += a[h][k]
    end
end

(0..r).each { |l|
    puts a[l].join(" ")
}