import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	}

	private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

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

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	}

	private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	}
} Main public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	} main {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	} @SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int[] rSum = new int[c]; rSum new int[c] c int cSum = 0 cSum 0 gSum = 0; gSum 0 for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		} {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		} for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			} {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			} int cell = scan.nextInt(); cell scan.nextInt() scan.nextInt scan cSum += cell cSum cell rSum[j] += cell rSum[j] rSum j cell System.out.print(cell + " ") System.out.print System.out System System.out cell + " " cell " " gSum += cSum gSum cSum System.out.println(cSum) System.out.println System.out System System.out cSum cSum = 0 cSum 0 for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " "); int i = 0; int i = 0; i 0 i < c i c i++ i++ i System.out.print(rSum[i] + " "); System.out.print(rSum[i] + " ") System.out.print System.out System System.out rSum[i] + " " rSum[i] rSum i " " System.out.println(gSum) System.out.println System.out System System.out gSum System.exit(0) System.exit System 0 String[] args args private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	} run {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	} int result = 0; result 0 for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result; int a = 1; int a = 1; a 1 a <= n a n a++ a++ a for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result; for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result; int b = a + 1; int b = a + 1; b a + 1 a 1 b <= n b n b++ b++ b for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result; for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result; int c = b + 1; int c = b + 1; c b + 1 b 1 c <= n c n c++ c++ c if (a + b + c == x)
						++result; if (a + b + c == x)
						++result; a + b + c == x a + b + c a + b + c a b c x ++result result return (result); (result) int n n int x x public class Main {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	}

	private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	}
} public class Main {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	}

	private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();

		int[] rSum = new int[c];
		int cSum = 0, gSum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				int cell = scan.nextInt();
				cSum += cell;
				rSum[j] += cell;
				System.out.print(cell + " ");
			}
			gSum += cSum;
			System.out.println(cSum);
			cSum = 0;
		}
		for (int i = 0; i < c; i++)
			System.out.print(rSum[i] + " ");
		System.out.println(gSum);

		System.exit(0);
	}

	private static int run(int n, int x) {
		int result = 0;
		for (int a = 1; a <= n; a++)
			for (int b = a + 1; b <= n; b++)
				for (int c = b + 1; c <= n; c++)
					if (a + b + c == x)
						++result;
		return (result);

	}
}