object Main {
  def main(args: Array[String]): Unit = {
    val scanner = new java.util.Scanner(System.in)

    val r, c = scanner.nextInt
    val sum_c = Array.fill(c)(0)
    val mat = Array.fill(r)(new Array[Int](c))
    for(i <- 0 until r) {
      for(j <- 0 until c) {
        val n = scanner.nextInt
        mat(i)(j) = n
        sum_c(j) = sum_c(j) + n
      }
    }

    for(i <- 0 until r) {
      print(mat(i).mkString(" "))
      print(" ")
      println(mat(i).sum)
    }
    print(sum_c.mkString(" "))
    print(" ")
    println(sum_c.sum)
  }
}

