import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	}
} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	} main {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	} 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 [][] cell = new int[r+1][c+1]; cell 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++) {
				cell[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++) {
				cell[i][j] = sc.nextInt();
			}
		} {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		} for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				cell[i][j] = sc.nextInt();
			} {
				cell[i][j] = sc.nextInt();
			} cell[i][j] = sc.nextInt() cell[i][j] cell[i] cell i j sc.nextInt() sc.nextInt sc for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		} {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		} for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			} {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			} cell[i][c] += cell[i][j] cell[i][c] cell[i] cell i c cell[i][j] cell[i] cell i j cell[r][j] += cell[i][j] cell[r][j] cell[r] cell r j cell[i][j] cell[i] cell i j System.out.printf("%d ",cell[i][j]) System.out.printf System.out System System.out "%d " cell[i][j] cell[i] cell i j System.out.println(cell[i][c]) System.out.println System.out System System.out cell[i][c] cell[i] cell i c cell[r][c] += cell[i][c] cell[r][c] cell[r] cell r c cell[i][c] cell[i] cell i c for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		} int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		} {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		} System.out.printf("%d ",cell[r][i]) System.out.printf System.out System System.out "%d " cell[r][i] cell[r] cell r i System.out.println(cell[r][c]) System.out.println System.out System System.out cell[r][c] cell[r] cell r c String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	}
} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int r = sc.nextInt();
		int c = sc.nextInt();
		int [][] cell = new int[r+1][c+1];

		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < r; i++) {
			for(int j = 0; j < c; j++) {
				cell[i][c] += cell[i][j];
				cell[r][j] += cell[i][j];
				System.out.printf("%d ",cell[i][j]);
			}
			System.out.println(cell[i][c]);
			cell[r][c] += cell[i][c];
		}
		for(int i = 0; i < c; i++) {
			System.out.printf("%d ",cell[r][i]);
//			cell[r][c] += cell[r][i];
		}
		
		System.out.println(cell[r][c]);
		

	}
}

