
object Main {
  def main(args: Array[String]): Unit = {
    val in = new java.util.Scanner(System.in)
    val r = in.nextInt()
    val c = in.nextInt()
    val array = Array.ofDim[Int](r+1,c+1)
    for( i <- 0 to r-1){
      for(j <- 0 to c-1){
        array(i)(j) = in.nextInt()
        array(i)(c) += array(i)(j)
        array(r)(j) += array(i)(j)
        array(r)(c) += array(i)(j)
      }
    }
   
    printArray(array)
  }
  def printArray(array : Array[Array[Int]]) :Unit ={
    for(i <- 0 to array.length-1){
      for(j <- 0 to array(i).length-1){
        print(array(i)(j))
        if(j != array(i).length-1){
          print(" ")
        }
      }
      println("")
    }
  }
}