import java.util.Scanner;

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

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] a = new int[r+1][c+1];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		}
		for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		}

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] a = new int[r+1][c+1];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		}
		for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		}

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	} main {
		Scanner sc = new Scanner(System.in);
		
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] a = new int[r+1][c+1];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		}
		for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		}

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	} Scanner 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[][] a = new int[r+1][c+1]; a 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++) {
				a[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++) {
				a[i][j] = sc.nextInt();
			}
		} {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		} for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				a[i][j] = sc.nextInt();
			} {
				a[i][j] = sc.nextInt();
			} a[i][j] = sc.nextInt() a[i][j] a[i] a i j sc.nextInt() sc.nextInt sc for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		} {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		} for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				a[i][c] += a[i][j];
			} {
				a[i][c] += a[i][j];
			} a[i][c] += a[i][j] a[i][c] a[i] a i c a[i][j] a[i] a i j for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		} int j=0; int j=0; j 0 j<=c j c j++ j++ j {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		} {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		} for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			} int i=0; int i=0; i 0 i<r i r i++ i++ i {
				a[r][j] += a[i][j];
			} {
				a[r][j] += a[i][j];
			} a[r][j] += a[i][j] a[r][j] a[r] a r j a[i][j] a[i] a i j for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		} int i=0; int i=0; i 0 i<= r i r i++ i++ i {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		} {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		} for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			} int j=0; int j=0; j 0 j<=c j c j++ j++ j {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			} {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			} if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]); j==0 j 0 System.out.printf("%d", a[i][j]) System.out.printf System.out System System.out "%d" a[i][j] a[i] a i j System.out.printf(" %d",a[i][j]) System.out.printf System.out System System.out " %d" a[i][j] a[i] a i j System.out.printf("\n") System.out.printf System.out System System.out "\n" String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] a = new int[r+1][c+1];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		}
		for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		}

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int r = sc.nextInt();
		int c = sc.nextInt();
		int[][] a = new int[r+1][c+1];
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		for(int i=0; i<r; i++) {
			for(int j=0; j<c; j++) {
				a[i][c] += a[i][j];
			}
		}
		for(int j=0; j<=c; j++) {
			for(int i=0; i<r; i++) {
				a[r][j] += a[i][j];
			}
		}

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	}
} Main
import java.util.Scanner;

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

		for(int i=0; i<= r; i++) {
			for(int j=0; j<=c; j++) {
				if(j==0)	System.out.printf("%d", a[i][j]);
				else		System.out.printf(" %d",a[i][j]);
			}
			System.out.printf("\n");
		}
	}
}
