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

ary.push([])

ary[0].length.times{|i|
    sum = 0
    r.times{|j|
        sum += ary[j][i]
    }
    ary[-1].push(sum)
}

ary.each{|col|
    puts col.join(" ")
}