
object Main extends App {
  val Seq(r, c) = readLine.split(' ').map(_.toInt).toList

  val tab = for { i <- 0 until r} yield {
    val row = readLine.split(' ').map(_.toInt).take(c)
    row :+ row.sum
  }

  for (a <- tab :+ tab.reduce(_.zip(_).map(t=>t._1 + t._2))) {
    println(a.head.toString + a.tail.map(" %d".format(_)).reduce(_+_))
  }
}
