object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val r, c = sc.nextInt()
  val in = List.fill(r, c)(sc.nextInt())
  addSumRow(in.map(addSumCol(_))).foreach(e => println(e.mkString(" ")))

  def addSumCol(list: List[Int]) = (list.sum :: list.reverse).reverse

  def addSumRow(lists: List[List[Int]]) = {
    val colSize = lists(0).size
    val sumRow = for (i <- 0 to colSize - 1) yield lists.map(_ (i)).sum
    (sumRow.toList :: lists.reverse).reverse
  }
}