object Main extends App {
  val scanner = new java.util.Scanner(System.in)

  val r = scanner.nextInt()
  val c = scanner.nextInt()

  val inputs = (1 to r).map {
    _ => (1 to c).map(_ => scanner.nextInt())
  }

  val row_sum = inputs.map { row =>
    row :+ row.sum
  }
  val column_sum = row_sum.foldLeft(Vector.fill(c+1)(0)) { (acc, row) =>
    (acc zip row).map(t => t._1 + t._2)
  }

  val outputs = row_sum :+ column_sum

  outputs.map(_.mkString(" ")).foreach(println)
}