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

class Main{
  public static void main(String[] args) throws IOException{
    Scanner scan=new Scanner(System.in);
    int r=Integer.parseInt(scan.next());
    int c=Integer.parseInt(scan.next());
    int[][] array1=new int[r][c];
    for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    }

    int[][] array2=new int[r+1][c+1];
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    }
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    }
    for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    }

    for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    }
  }
} import java.io.*; io.* java import java.util.*; util.* java import java.lang.*; lang.* java class Main{
  public static void main(String[] args) throws IOException{
    Scanner scan=new Scanner(System.in);
    int r=Integer.parseInt(scan.next());
    int c=Integer.parseInt(scan.next());
    int[][] array1=new int[r][c];
    for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    }

    int[][] array2=new int[r+1][c+1];
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    }
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    }
    for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    }

    for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    }
  }
} Main public static void main(String[] args) throws IOException{
    Scanner scan=new Scanner(System.in);
    int r=Integer.parseInt(scan.next());
    int c=Integer.parseInt(scan.next());
    int[][] array1=new int[r][c];
    for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    }

    int[][] array2=new int[r+1][c+1];
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    }
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    }
    for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    }

    for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    }
  } main {
    Scanner scan=new Scanner(System.in);
    int r=Integer.parseInt(scan.next());
    int c=Integer.parseInt(scan.next());
    int[][] array1=new int[r][c];
    for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    }

    int[][] array2=new int[r+1][c+1];
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    }
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    }
    for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    }

    for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    }
  } Scanner scan=new Scanner(System.in); scan new Scanner(System.in) int r=Integer.parseInt(scan.next()); r Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int c=Integer.parseInt(scan.next()); c Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int[][] array1=new int[r][c]; array1 new int[r][c] r c for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    } int i=0; int i=0; i 0 i<array1.length i array1.length array1 array1.length i++ i++ i {
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    } {
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    } for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      } int j=0; int j=0; j 0 j<array1[i].length j array1[i].length array1[i] array1 i array1[i].length j++ j++ j {
        array1[i][j]=Integer.parseInt(scan.next());
      } {
        array1[i][j]=Integer.parseInt(scan.next());
      } array1[i][j]=Integer.parseInt(scan.next()) array1[i][j] array1[i] array1 i j Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int[][] array2=new int[r+1][c+1]; array2 new int[r+1][c+1] r+1 r 1 c+1 c 1 for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    } int i=0; int i=0; i 0 i<array2.length-1 i array2.length-1 array2.length array2 array2.length 1 i++ i++ i {
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    } {
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    } for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      } int j=0; int j=0; j 0 j<array2[i].length-1 j array2[i].length-1 array2[i].length array2[i] array2 i array2[i].length 1 j++ j++ j {
        array2[i][j]=array1[i][j];
      } {
        array2[i][j]=array1[i][j];
      } array2[i][j]=array1[i][j] array2[i][j] array2[i] array2 i j array1[i][j] array1[i] array1 i j for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    } int i=0; int i=0; i 0 i<array2.length-1 i array2.length-1 array2.length array2 array2.length 1 i++ i++ i {
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    } {
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    } for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      } int j=0; int j=0; j 0 j<array2[i].length-1 j array2[i].length-1 array2[i].length array2[i] array2 i array2[i].length 1 j++ j++ j {
        array2[i][c]+=array2[i][j];
      } {
        array2[i][c]+=array2[i][j];
      } array2[i][c]+=array2[i][j] array2[i][c] array2[i] array2 i c array2[i][j] array2[i] array2 i j for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    } int i=0; int i=0; i 0 i<c+1 i c+1 c 1 i++ i++ i {
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    } {
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    } for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      } int j=0; int j=0; j 0 j<r j r j++ j++ j {
        array2[r][i]+=array2[j][i];
      } {
        array2[r][i]+=array2[j][i];
      } array2[r][i]+=array2[j][i] array2[r][i] array2[r] array2 r i array2[j][i] array2[j] array2 j i for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    } int i=0; int i=0; i 0 i<r+1 i r+1 r 1 i++ i++ i {
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    } {
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    } for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      } int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      } {
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      } if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        } j<c j c {
          System.out.print(array2[i][j]+" ");
        } System.out.print(array2[i][j]+" ") System.out.print System.out System System.out array2[i][j]+" " array2[i][j] array2[i] array2 i j " " {
          System.out.println(array2[i][j]);
        } System.out.println(array2[i][j]) System.out.println System.out System System.out array2[i][j] array2[i] array2 i j String[] args args
import java.io.*;
import java.util.*;
import java.lang.*;

class Main{
  public static void main(String[] args) throws IOException{
    Scanner scan=new Scanner(System.in);
    int r=Integer.parseInt(scan.next());
    int c=Integer.parseInt(scan.next());
    int[][] array1=new int[r][c];
    for(int i=0;i<array1.length;i++){
      for(int j=0;j<array1[i].length;j++){
        array1[i][j]=Integer.parseInt(scan.next());
      }
    }

    int[][] array2=new int[r+1][c+1];
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][j]=array1[i][j];
      }
    }
    for(int i=0;i<array2.length-1;i++){
      for(int j=0;j<array2[i].length-1;j++){
        array2[i][c]+=array2[i][j];
      }
    }
    for(int i=0;i<c+1;i++){
      for(int j=0;j<r;j++){
        array2[r][i]+=array2[j][i];
      }
    }

    for(int i=0;i<r+1;i++){
      for(int j=0;j<c+1;j++){
        if(j<c){
          System.out.print(array2[i][j]+" ");
        }else{
          System.out.println(array2[i][j]);
        }
      }
    }
  }
}
