import java.io.*;
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  }
}
 import java.io.*; io.* java import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  }
} Main public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  } main {
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int r=sc.nextInt(); r sc.nextInt() sc.nextInt sc int c=sc.nextInt(); c sc.nextInt() sc.nextInt sc int sumr[]; sumr int sumc[]; sumc int a[][]; a int sum=0; sum 0 sumr=new int[r] sumr new int[r] r sumc=new int[c] sumc new int[c] c a=new int[r][c] a new int[r][c] r c for(int i=0;i<r;i++) sumr[i]=0; int i=0; int i=0; i 0 i<r i r i++ i++ i sumr[i]=0; sumr[i]=0 sumr[i] sumr i 0 for(int j=0;j<c;j++) sumc[j]=0; int j=0; int j=0; j 0 j<c j c j++ j++ j sumc[j]=0; sumc[j]=0 sumc[j] sumc j 0 for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    } int i=0; int i=0; i 0 i<r i r i++ i++ i {
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    } {
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    } for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      } int j=0; int j=0; j 0 j<c j c j++ j++ j {
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      } {
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      } a[i][j]=sc.nextInt() a[i][j] a[i] a i j sc.nextInt() sc.nextInt sc sumr[i]+=a[i][j] sumr[i] sumr i a[i][j] a[i] a i j sumc[j]+=a[i][j] sumc[j] sumc j a[i][j] a[i] a i j sum+=a[i][j] sum a[i][j] a[i] a i j for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    } int i=0; int i=0; i 0 i<r i r i++ i++ i {
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    } {
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    } for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      } int j=0; int j=0; j 0 j<c j c j++ j++ j {
        System.out.print(a[i][j]+" ");
      } {
        System.out.print(a[i][j]+" ");
      } System.out.print(a[i][j]+" ") System.out.print System.out System System.out a[i][j]+" " a[i][j] a[i] a i j " " System.out.println(sumr[i]) System.out.println System.out System System.out sumr[i] sumr i for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    } int j=0; int j=0; j 0 j<c j c j++ j++ j {
      System.out.print(sumc[j]+" ");
    } {
      System.out.print(sumc[j]+" ");
    } System.out.print(sumc[j]+" ") System.out.print System.out System System.out sumc[j]+" " sumc[j] sumc j " " System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  }
} Main
import java.io.*;
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int r=sc.nextInt();
    int c=sc.nextInt();
    int sumr[];
    int sumc[];
    int a[][];
    int sum=0;
    sumr=new int[r];
    sumc=new int[c];
    a=new int[r][c];
    for(int i=0;i<r;i++) sumr[i]=0;
    for(int j=0;j<c;j++) sumc[j]=0;
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        a[i][j]=sc.nextInt();
        sumr[i]+=a[i][j];
        sumc[j]+=a[i][j];
        sum+=a[i][j];
      }
    }
    for(int i=0;i<r;i++){
      for(int j=0;j<c;j++){
        System.out.print(a[i][j]+" ");
      }
      System.out.println(sumr[i]);
    }
    for(int j=0;j<c;j++){
      System.out.print(sumc[j]+" ");
    }
    System.out.println(sum);
  }
}

