import scala.io.StdIn.readLine

object Main extends App {

  val Array(r, c) = readLine().split(" ").map { _.toInt }

  val lines = Array.fill(r)(readLine())

  val xss = for {
    line <- lines
    xs    = line.split(" ").map { _.toInt }
  } yield xs

  val colSum = xss.transpose.map { _.sum }

  for (xs <- xss) {
    println(display(xs))
  }
  println(display(colSum))

  def display(xs: Seq[Int]): String = xs.mkString("", " ", " " + xs.sum)
}

