import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		
		int[][] cell = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		}
		
		int[] csum = new int[c];
		
		for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		}
		System.out.println(sum);

	}

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

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		
		int[][] cell = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		}
		
		int[] csum = new int[c];
		
		for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		}
		System.out.println(sum);

	}

} Main public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		
		int[][] cell = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		}
		
		int[] csum = new int[c];
		
		for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		}
		System.out.println(sum);

	} main {
		Scanner scanner = new Scanner(System.in);
		
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		
		int[][] cell = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		}
		
		int[] csum = new int[c];
		
		for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		}
		System.out.println(sum);

	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int r = scanner.nextInt(); r scanner.nextInt() scanner.nextInt scanner int c = scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner int[][] cell = new int[r][c]; cell new int[r][c] r c for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		} {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		} for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt(); int j = 0; int j = 0; j 0 j < c j c j++ j++ j cell[i][j] = scanner.nextInt(); cell[i][j] = scanner.nextInt() cell[i][j] cell[i] cell i j scanner.nextInt() scanner.nextInt scanner int[] csum = new int[c]; csum new int[c] c for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		} {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		} int rsum = 0; rsum 0 for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			} {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			} rsum += cell[i][j] rsum cell[i][j] cell[i] cell i j csum[j] += cell[i][j] csum[j] csum j cell[i][j] cell[i] cell i j System.out.print(cell[i][j] + " ") System.out.print System.out System System.out cell[i][j] + " " cell[i][j] cell[i] cell i j " " System.out.println(rsum) System.out.println System.out System System.out rsum int sum = 0; sum 0 for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		} int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		} {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		} sum += csum[i] sum csum[i] csum i System.out.print(csum[i] + " ") System.out.print System.out System System.out csum[i] + " " csum[i] csum i " " System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int r = scanner.nextInt();
		int c = scanner.nextInt();
		
		int[][] cell = new int[r][c];
		
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++)
				cell[i][j] = scanner.nextInt();
		}
		
		int[] csum = new int[c];
		
		for (int i = 0; i < r; i++) {
			int rsum = 0;
			for (int j = 0; j < c; j++) {
				rsum += cell[i][j];
				csum[j] += cell[i][j];
				System.out.print(cell[i][j] + " ");
			}
			System.out.println(rsum);
		}
		int sum = 0;
		for (int i = 0; i < c; i++) {
			sum += csum[i];
			System.out.print(csum[i] + " ");
		}
		System.out.println(sum);

	}

}

