fun main(args: Array<String>) {
    val inStr = readLine() ?: return
    val (r, c) = inStr.split(' ').map { e -> e.toInt() }

    var columnSum = IntArray(c)

    for (i in 0 until r) {
        val line = readLine() ?: return
        val rowData = line.split(' ').map { e -> e.toInt() }.toIntArray()
        val rowSum = rowData.sum()
        println("%s %d".format(line, rowSum))

        columnSum = columnSum.zip(rowData).map { (t, e) -> t + e }.toIntArray()
    }
    println("%s %d".format(columnSum.joinToString(" "), columnSum.sum()))

}

