import scala.io.StdIn.readLine
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  val Array(r,c) = readLine.split(" ").map(_.toInt)
  val matrix = ArrayBuffer.empty[Array[Int]]
  val row = Array.fill[Int](c+1)(0)

  for( i <- 0 until r) {
    val n = readLine.split(" ").map(_.toInt)
    val a = n :+ n.sum
    matrix += a
    (0 until c).foreach( j=> row(j) += n(j) );row(c) += n.sum
  }
  matrix += row
  matrix.toArray.foreach( row => println(row.mkString(" ")) )
}