import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
} import java.util.*; util.* java public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	} main {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	} int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int m = sc.nextInt(); m sc.nextInt() sc.nextInt sc int[][] ints = new int[n][m]; ints new int[n][m] n m int[] line = new int[n]; line new int[n] n int[] row = new int[m]; row new int[m] m int sum = 0; sum 0 for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		} {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		} for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			} int j=0; int j=0; j 0 j<m j m j++ j++ j {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			} {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			} int temp = Integer.parseInt(sc.next()); temp Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc ints[i][j] = temp ints[i][j] ints[i] ints i j temp line[i] += temp line[i] line i temp row[j] += temp row[j] row j temp sum += line[i] sum line[i] line i for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		} int i=0; int i=0; i 0 i<n+1 i n+1 n 1 i++ i++ i {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		} {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		} for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			} int j=0; int j=0; j 0 j<m+1 j m+1 m 1 j++ j++ j {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			} {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			} if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);} i==n && j==m i==n i n j==m j m {System.out.print(sum);} System.out.print(sum) System.out.print System.out System System.out sum if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);} j==m j m {System.out.print(line[i]);} System.out.print(line[i]) System.out.print System.out System System.out line[i] line i if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);} i==n i n {System.out.print(row[j]);} System.out.print(row[j]) System.out.print System.out System System.out row[j] row j {System.out.print(ints[i][j]);} System.out.print(ints[i][j]) System.out.print System.out System System.out ints[i][j] ints[i] ints i j if (j!=m) {System.out.print(" ");} j!=m j m {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 String[] args args public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
} public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
} Main
import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] ints = new int[n][m];
		int[] line = new int[n];
		int[] row = new int[m]; 
		int sum = 0;
		for (int i=0; i<n; i++) {
			for (int j=0; j<m; j++) {
				 int temp = Integer.parseInt(sc.next());
				 ints[i][j] = temp;
				 line[i] += temp;
				 row[j] += temp;
			}
			sum += line[i];
		}
		
		for (int i=0; i<n+1; i++) {
			for (int j=0; j<m+1; j++) {
				if (i==n && j==m) {System.out.print(sum);}
				else if (j==m) {System.out.print(line[i]);}
				else if (i==n) {System.out.print(row[j]);}
				else {System.out.print(ints[i][j]);}
				if (j!=m) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
}
