import 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 sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	}
} import java.util.*; util.* java public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	} main {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	} 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 sum = 0; sum 0 int sumsum = 0; sumsum 0 int[][] matrix = new int[r][c]; matrix new int[r][c] r c for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[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++) {
				matrix[i][j] = sc.nextInt();
			}
		} {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		} for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				matrix[i][j] = sc.nextInt();
			} {
				matrix[i][j] = sc.nextInt();
			} matrix[i][j] = sc.nextInt() matrix[i][j] matrix[i] matrix i j sc.nextInt() sc.nextInt sc for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		} {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		} sum = 0 sum 0 for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			} {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			} System.out.print(matrix[i][j] + " ") System.out.print System.out System System.out matrix[i][j] + " " matrix[i][j] matrix[i] matrix i j " " sum += matrix[i][j] sum matrix[i][j] matrix[i] matrix i j if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				} j == c - 1 j c - 1 c 1 {
					System.out.print(sum);
					sumsum += sum;
				} System.out.print(sum) System.out.print System.out System System.out sum sumsum += sum sumsum sum System.out.println("") System.out.println System.out System System.out "" for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		} int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		} {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		} sum = 0 sum 0 for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			} int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
				sum += matrix[j][i];
			} {
				sum += matrix[j][i];
			} sum += matrix[j][i] sum matrix[j][i] matrix[j] matrix j i System.out.print(sum + " ") System.out.print System.out System System.out sum + " " sum " " System.out.println(sumsum) System.out.println System.out System System.out sumsum 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 sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	}
} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();
		int c = sc.nextInt();
		int sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	}
} Main
import 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 sum = 0;
		int sumsum = 0;
		int[][] matrix = new int[r][c];
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(matrix[i][j] + " ");
				sum += matrix[i][j];
				if (j == c - 1) {
					System.out.print(sum);
					sumsum += sum;
				}
			}
			System.out.println("");
		}
		for (int i = 0; i < c; i++) {
			sum = 0;
			for (int j = 0; j < r; j++) {
				sum += matrix[j][i];
			}
			System.out.print(sum + " ");
		}
		System.out.println(sumsum);
	}
}