import java.util.Scanner;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util import java.util.Arrays; Arrays java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int row = sc.nextInt(); row sc.nextInt() sc.nextInt sc int col = sc.nextInt(); col sc.nextInt() sc.nextInt sc int[][] table = new int[row][col]; table new int[row][col] row col for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		} int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		} {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		} for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < col j col j++ j++ j {
				table[i][j] = sc.nextInt();
			} {
				table[i][j] = sc.nextInt();
			} table[i][j] = sc.nextInt() table[i][j] table[i] table i j sc.nextInt() sc.nextInt sc int[] rowSum = new int[row]; rowSum new int[row] row int[] colSum = new int[col]; colSum new int[col] col int sum = 0; sum 0 Arrays.fill(rowSum, 0) Arrays.fill Arrays rowSum 0 Arrays.fill(colSum, 0) Arrays.fill Arrays colSum 0 for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		} int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		} {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		} for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			} int j = 0; int j = 0; j 0 j < col j col j++ j++ j {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			} {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			} sum += table[i][j] sum table[i][j] table[i] table i j rowSum[i] += table[i][j] rowSum[i] rowSum i table[i][j] table[i] table i j colSum[j] += table[i][j] colSum[j] colSum j table[i][j] table[i] table i j for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		} int i = 0; int i = 0; i 0 i < row i row i++ i++ i {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		} {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		} for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			} int j = 0; int j = 0; j 0 j < col j col j++ j++ j {
				System.out.print("" + table[i][j] + " ");
			} {
				System.out.print("" + table[i][j] + " ");
			} System.out.print("" + table[i][j] + " ") System.out.print System.out System System.out "" + table[i][j] + " " "" + table[i][j] + " " "" table[i][j] table[i] table i j " " System.out.println("" + rowSum[i]) System.out.println System.out System System.out "" + rowSum[i] "" rowSum[i] rowSum i for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		} int j = 0; int j = 0; j 0 j < col j col j++ j++ j {
			System.out.print("" + colSum[j] + " ");
		} {
			System.out.print("" + colSum[j] + " ");
		} System.out.print("" + colSum[j] + " ") System.out.print System.out System System.out "" + colSum[j] + " " "" + colSum[j] + " " "" colSum[j] colSum j " " System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	}
} Main
import java.util.Scanner;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		int col = sc.nextInt();
		int[][] table = new int[row][col];
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				table[i][j] = sc.nextInt();
			}
		}
		int[] rowSum = new int[row];
		int[] colSum = new int[col];
		int sum = 0;
		Arrays.fill(rowSum, 0);
		Arrays.fill(colSum, 0);
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				sum += table[i][j];
				rowSum[i] += table[i][j];
				colSum[j] += table[i][j];
			}
		}
		for(int i = 0; i < row; i++) {
			for(int j = 0; j < col; j++) {
				System.out.print("" + table[i][j] + " ");
			}
			System.out.println("" + rowSum[i]);
		}
		for(int j = 0; j < col; j++) {
			System.out.print("" + colSum[j] + " ");
		}
		System.out.println(sum);
	}
}