import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	}

} Main public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	} main {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	} 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 A[][] = new int[r][c]; A new int[r][c] r c for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		} {
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		} for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			} int i2=0; int i2=0; i2 0 i2<c i2 c i2++ i2++ i2 {
				int x = scan.nextInt();
				A[i][i2] = x;
			} {
				int x = scan.nextInt();
				A[i][i2] = x;
			} int x = scan.nextInt(); x scan.nextInt() scan.nextInt scan A[i][i2] = x A[i][i2] A[i] A i i2 x int total = 0; total 0 for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		} {
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		} int sumr = 0; sumr 0 for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			} int i2=0; int i2=0; i2 0 i2<c i2 c i2++ i2++ i2 {
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			} {
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			} System.out.print(A[i][i2] + " ") System.out.print System.out System System.out A[i][i2] + " " A[i][i2] A[i] A i i2 " " sumr = sumr + A[i][i2] sumr sumr + A[i][i2] sumr A[i][i2] A[i] A i i2 System.out.println(sumr) System.out.println System.out System System.out sumr total = total + sumr total total + sumr total sumr for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		} int i=0; int i=0; i 0 i<c i c i++ i++ i {
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		} {
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		} int sumc = 0; sumc 0 for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			} int i2=0; int i2=0; i2 0 i2<r i2 r i2++ i2++ i2 {
				sumc = sumc + A[i2][i];
			} {
				sumc = sumc + A[i2][i];
			} sumc = sumc + A[i2][i] sumc sumc + A[i2][i] sumc A[i2][i] A[i2] A i2 i System.out.print(sumc + " ") System.out.print System.out System System.out sumc + " " sumc " " System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	}

} public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		int r = scan.nextInt();
		int c = scan.nextInt();
		
		int A[][] = new int[r][c];
		// ?????????????´?
		for(int i=0; i<r; i++){
			for(int i2=0; i2<c; i2++){
				int x = scan.nextInt();
				A[i][i2] = x;
			}
		}
		// ??????
		int total = 0;
		for(int i=0; i<r; i++){
			int sumr = 0;
			for(int i2=0; i2<c; i2++){
				System.out.print(A[i][i2] + " ");
				sumr = sumr + A[i][i2];
			}
			System.out.println(sumr);
			total = total + sumr;
		}
		for(int i=0; i<c; i++){
			int sumc = 0;
			for(int i2=0; i2<r; i2++){
				sumc = sumc + A[i2][i];
			}
			System.out.print(sumc + " ");
		}
		System.out.println(total);
			
		
	}

}