import java.util.*

data class Spreadsheet(val rows: List<List<Int>>) {
    val col = if (rows.size != 0) rows.get(0).size else 0
    fun withTotal(): Spreadsheet {
        val newElement = mutableListOf<List<Int>>()
        val totalRow = (1..col + 1).map { 0 }.toMutableList()
        for (row in rows) {
            val newRow = row + row.sum()
            totalRow.forEachIndexed { index, e -> totalRow.set(index, e + newRow[index]) }
            newElement.add(newRow)
        }
        newElement.add(totalRow)
        return Spreadsheet(newElement)
    }

    override fun toString(): String =
        this.rows.map { it.joinToString(" ") }.joinToString("\n")
}

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val rows = mutableListOf<List<Int>>()
    val r = scanner.nextInt()
    val c = scanner.nextInt()
    (1..r).forEach {
        val row = mutableListOf<Int>()
        (1..c).forEach {
            row.add(scanner.nextInt())
        }
        rows.add(row)
    }
    println(Spreadsheet(rows).withTotal())
}

