object Main {
  def main(args: Array[String]): Unit = {
    val Array(r, c, _*) = io.StdIn.readLine().split(" ").map(_.toInt)

    val sheet = Array.ofDim[Int](r + 1, c + 1)
    var sum = 0
    for {
      y <- 0 until r
    } {
      io.StdIn.readLine().split(" ")
        .map(_.toInt)
        .zipWithIndex
        .foreach { case (v, x) =>
          sheet(y)(x) = v
          sheet(y)(c) += v
          sheet(r)(x) += v
          sum += v
        }
    }
    sheet(r)(c) = sum
    sheet.map(_.mkString(" ")).foreach(println)
  }
}