
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	}
	public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		}


} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	}
	public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		}


} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	} main {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int m=sc.nextInt(); m sc.nextInt() sc.nextInt sc int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int[][] a=new int[m+1][n+1]; a new int[m+1][n+1] m+1 m 1 n+1 n 1 for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		} int i=0; int i=0; i 0 i<m i m i++ i++ i {
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		} {
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		} for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			} int j=0; int j=0; j 0 j<n j n j++ j++ j {
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			} {
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			} a[i][j]=sc.nextInt() a[i][j] a[i] a i j sc.nextInt() sc.nextInt sc a[i][n]+=a[i][j] a[i][n] a[i] a i n a[i][j] a[i] a i j for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		} int j=0; int j=0; j 0 j<=n j n j++ j++ j {
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		} {
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		} for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			} int i=0; int i=0; i 0 i<m i m i++ i++ i {
				a[m][j]+=a[i][j];
			} {
				a[m][j]+=a[i][j];
			} a[m][j]+=a[i][j] a[m][j] a[m] a m j a[i][j] a[i] a i j printintMat(a) printintMat a String[] args args public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		} printintMat {
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		} for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	} int i=0; int i=0; i 0 i<A.length i A.length A A.length i++ i++ i {
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	} {
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	} for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			} int j=0; int j=0; j 0 j<A[0].length-1 j A[0].length-1 A[0].length A[0] A 0 A[0].length 1 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.print(A[i][A[0].length-1]) System.out.print System.out System System.out A[i][A[0].length-1] A[i] A i A[0].length-1 A[0].length A[0] A 0 A[0].length 1 System.out.println() System.out.println System.out System System.out int A[][] A public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	}
	public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		}


} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	}
	public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		}


} Main

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][] a=new int[m+1][n+1];
		for(int i=0; i<m; i++){
			for(int j=0; j<n; j++){
				a[i][j]=sc.nextInt();
				a[i][n]+=a[i][j];

			}
		}
		for(int j=0; j<=n; j++){
			for(int i=0; i<m; i++){
				a[m][j]+=a[i][j];
			}
		}
		printintMat(a);



	}
	public static void printintMat(int A[][]){
		//行列のプリントアウト
		for(int i=0; i<A.length; i++){
			for(int j=0; j<A[0].length-1; j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.print(A[i][A[0].length-1]);
			System.out.println();
	}

		}


}
