import java.util.Scanner;

public class Main {

	private static Scanner sc;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	}
} import java.util.Scanner; Scanner java.util public class Main {

	private static Scanner sc;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	}
} Main private static Scanner sc; sc public static void main(String[] args){
		sc = new Scanner(System.in);
		int r=sc.nextInt();
		int c=sc.nextInt();
		int m[][]=new int[r+1][c+1];
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	} main {
		sc = new Scanner(System.in);
		int r=sc.nextInt();
		int c=sc.nextInt();
		int m[][]=new int[r+1][c+1];
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	} 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 m[][]=new int[r+1][c+1]; m new int[r+1][c+1] r+1 r 1 c+1 c 1 for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			}
		} {
			for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			}
		} for(int j=0;j<c;j++){
				m[i][j]=sc.nextInt();
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				m[i][j]=sc.nextInt();
			} {
				m[i][j]=sc.nextInt();
			} m[i][j]=sc.nextInt() m[i][j] m[i] m i j sc.nextInt() sc.nextInt sc for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		} 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(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		} {
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		} for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			} {
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			} System.out.print(m[i][j]+" ") System.out.print System.out System System.out m[i][j]+" " m[i][j] m[i] m i j " " m[i][c]+=m[i][j] m[i][c] m[i] m i c m[i][j] m[i] m i j System.out.println(m[i][c]) System.out.println System.out System System.out m[i][c] m[i] m i c for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][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 {
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		} {
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		} for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			} int i=0; int i=0; i 0 i<r i r i++ i++ i {
				m[r][j]+=m[i][j];
			} {
				m[r][j]+=m[i][j];
			} m[r][j]+=m[i][j] m[r][j] m[r] m r j m[i][j] m[i] m i j System.out.print(m[r][j]) System.out.print System.out System System.out m[r][j] m[r] m r j if(j!=c)System.out.print(" "); j!=c j c System.out.print(" ") System.out.print System.out System System.out " " System.out.println() System.out.println System.out System System.out String[] args args public class Main {

	private static Scanner sc;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	}
} public class Main {

	private static Scanner sc;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				System.out.print(m[i][j]+" ");
				m[i][c]+=m[i][j];
			}
			System.out.println(m[i][c]);
		}
		for(int j=0;j<c+1;j++){
			for(int i=0;i<r;i++){
				m[r][j]+=m[i][j];
			}
			System.out.print(m[r][j]);
			if(j!=c)System.out.print(" ");
		}
		System.out.println();
	}
} Main
import java.util.Scanner;

public class Main {

	private static Scanner sc;

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

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