import java.util.Arrays;
import java.util.Scanner;



public class Main{
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
}


} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
}


} Main public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
} main {
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
} Scanner scan=new Scanner(System.in); scan new Scanner(System.in) int r=scan.nextInt(); r scan.nextInt() scan.nextInt scan int c=scan.nextInt(); c scan.nextInt() scan.nextInt scan int[][] matrix_rc=new int[r+1][c+1]; matrix_rc new int[r+1][c+1] r+1 r 1 c+1 c 1 int sum_r; sum_r int[] sum_c=new int[c+1]; sum_c new int[c+1] c+1 c 1 Arrays.fill(sum_c, 0) Arrays.fill Arrays sum_c 0 for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		} {
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		} sum_r=0 sum_r 0 for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			} {
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			} if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§?????????????????? j!=c j c {
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];} matrix_rc[i][j]=scan.nextInt() matrix_rc[i][j] matrix_rc[i] matrix_rc i j scan.nextInt() scan.nextInt scan sum_r+=matrix_rc[i][j] sum_r matrix_rc[i][j] matrix_rc[i] matrix_rc i j if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§?????????????????? j==c j c {
					matrix_rc[i][j]=sum_r;
				} matrix_rc[i][j]=sum_r matrix_rc[i][j] matrix_rc[i] matrix_rc i j sum_r sum_c[j]+=matrix_rc[i][j] sum_c[j] sum_c j matrix_rc[i][j] matrix_rc[i] matrix_rc i j for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\ int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\ {
			matrix_rc[r][j]=sum_c[j];
		} matrix_rc[r][j]=sum_c[j] matrix_rc[r][j] matrix_rc[r] matrix_rc r j sum_c[j] sum_c j for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		} 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++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		} {
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		} for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			} {
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			} System.out.print(matrix_rc[i][j]) System.out.print System.out System System.out matrix_rc[i][j] matrix_rc[i] matrix_rc i j if(j<c){System.out.print(" ");} j<c j c {System.out.print(" ");} System.out.print(" ") System.out.print System.out System System.out " " System.out.println() System.out.println System.out System System.out scan.close() scan.close scan String[] args args public class Main{
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
}


} public class Main{
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
}


} Main
import java.util.Arrays;
import java.util.Scanner;



public class Main{
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);
		int r=scan.nextInt();
		int c=scan.nextInt();
		int[][] matrix_rc=new int[r+1][c+1];
		int sum_r;
		int[] sum_c=new int[c+1];
		Arrays.fill(sum_c, 0);
		for(int i=0;i<r;i++){
			sum_r=0;//??????????????§?????????
			for(int j=0;j<c+1;j++){
				if(j!=c){
				matrix_rc[i][j]=scan.nextInt();
				sum_r+=matrix_rc[i][j];}
				else if(j==c){
					matrix_rc[i][j]=sum_r;
				}//c+1????????§??????????????????
				sum_c[j]+=matrix_rc[i][j];//j????????????????¶???????????????????????????£??\
			}
		}
		for(int j=0;j<c+1;j++){
			matrix_rc[r][j]=sum_c[j];
		}//r+1?????????????????£??\
		for(int i=0;i<r+1;i++){
			for(int j=0;j<c+1;j++){
				System.out.print(matrix_rc[i][j]);
				if(j<c){System.out.print(" ");}
			}System.out.println();
		}
		scan.close();
}


}