object Main{
  def main(args:Array[String]){
    val sc=new java.util.Scanner(System.in)
    val r,c=sc.nextInt
    val a=Array.fill(r,c)(sc.nextInt)
    a.foreach(b=>println(b.mkString(" ")+" "+b.sum))
    var SUM=0
    for(i<-0 until c){
      var sum=0
      for(j<-0 until r){
        sum+=a(j)(i)
        SUM+=a(j)(i)
      }
      print(sum+" ")
    }
    println(SUM)
  }
}