import scala.io.StdIn.readLine

object Main extends App {
  val Array(r, c) = readLine.split(" ").map(_.toInt)

  val records = for (e <- 1 to r) yield {
    val record = readLine().split(" ").map(_.toInt).toBuffer
    record :+ record.sum
  }

  val lastRecord = scala.collection.mutable.ArrayBuffer[Int]()

  for (i <- 0 to c) {
    var sum = 0
    for (j <- 0 until r) {
      sum += records(j)(i)
    }
    lastRecord += sum
  }

  for (e <- records) println(e.mkString(" "))
  println(lastRecord.mkString(" "))
}
