fun main(args: Array<String>) {
    val (r, c) = readLine()?.split(" ")?.map { it.toInt() } ?: return
    val matrix = Array(r, { intArrayOf() })
    val rowList = arrayListOf<Int>()
    for (i in 0 until r) {
        val elements = readLine()?.split(" ")?.map { it.toInt() }?.toMutableList() ?: return
        rowList.add(elements.sum())
        matrix[i] = elements.toIntArray()
    }
    val builder = StringBuilder()
    val colSumList = arrayListOf<Int>()
    for ((i, row) in matrix.withIndex()) {
        row.forEachIndexed { j, element ->
            if (colSumList.size < c) {
                colSumList.add(element)
            } else {
                colSumList[j] += element
            }
        }
        builder.append(row.joinToString(" "))
                .append(" ${rowList.get(i)}")
                .appendln()
    }
    builder.append(colSumList.joinToString(" "))
            .append(" ${colSumList.filterNotNull().sum()}")
            .appendln()
    print(builder.toString())

}
