import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	}
} Main public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringBuffer sb = new StringBuffer(); sb new StringBuffer() String[] input = br.readLine().split(" "); input br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(input[0]); r Integer.parseInt(input[0]) Integer.parseInt Integer input[0] input 0 int c = Integer.parseInt(input[1]); c Integer.parseInt(input[1]) Integer.parseInt Integer input[1] input 1 int i = 0; i 0 int j = 0; j 0 int a = 0; a 0 int b = 0; b 0 int matrix[][] = new int[r][c]; matrix new int[r][c] r c for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		} i=0 ; i=0 i 0 i<r i r i++ i++ i {
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		} {
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		} input = br.readLine().split(" ") input br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			} j=0 ; j=0 j 0 j<c j c j++ j++ j {
				matrix[i][j] = Integer.parseInt(input[j]);
			} {
				matrix[i][j] = Integer.parseInt(input[j]);
			} matrix[i][j] = Integer.parseInt(input[j]) matrix[i][j] matrix[i] matrix i j Integer.parseInt(input[j]) Integer.parseInt Integer input[j] input j for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		} i=0 ; i=0 i 0 i<r i r i++ i++ i {
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		} {
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		} for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			} j=0 ; j=0 j 0 j<c j c j++ j++ j {
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			} {
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			} a = matrix[i][j] + a a matrix[i][j] + a matrix[i][j] matrix[i] matrix i j a sb.append(Integer.toString(matrix[i][j])).append(" ") sb.append(Integer.toString(matrix[i][j])).append sb.append(Integer.toString(matrix[i][j])) sb.append sb Integer.toString(matrix[i][j]) Integer.toString Integer matrix[i][j] matrix[i] matrix i j " " sb.append(Integer.toString(a)).append("\n") sb.append(Integer.toString(a)).append sb.append(Integer.toString(a)) sb.append sb Integer.toString(a) Integer.toString Integer a "\n" a = 0 a 0 for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		} i=0 ; i=0 i 0 i<c i c i++ i++ i {
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		} {
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		} for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			} j=0 ; j=0 j 0 j<r j r j++ j++ j {
				a = a + matrix[j][i];
			} {
				a = a + matrix[j][i];
			} a = a + matrix[j][i] a a + matrix[j][i] a matrix[j][i] matrix[j] matrix j i sb.append(Integer.toString(a)).append(" ") sb.append(Integer.toString(a)).append sb.append(Integer.toString(a)) sb.append sb Integer.toString(a) Integer.toString Integer a " " b = b + a b b + a b a a = 0 a 0 sb.append(Integer.toString(b)).append("\n") sb.append(Integer.toString(b)).append sb.append(Integer.toString(b)) sb.append sb Integer.toString(b) Integer.toString Integer b "\n" System.out.print(sb) System.out.print System.out System System.out sb String[] age age public class Main {
	public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	}
} public class Main {
	public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] age)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb = new StringBuffer();

		String[] input = br.readLine().split(" ");
		int r = Integer.parseInt(input[0]);
		int c = Integer.parseInt(input[1]);
		int i = 0;
		int j = 0;
		int a = 0;
		int b = 0;
		int matrix[][] = new int[r][c];

		for( i=0 ; i<r ; i++ ){
			input = br.readLine().split(" ");
			for( j=0 ; j<c ; j++ ){
				matrix[i][j] = Integer.parseInt(input[j]);
			}
		}

		for( i=0 ; i<r ; i++){
			for( j=0 ; j<c ; j++ ){
				a = matrix[i][j] + a;
				sb.append(Integer.toString(matrix[i][j])).append(" ");
			}
			sb.append(Integer.toString(a)).append("\n");
			a = 0;
		}

		for( i=0 ; i<c ; i++ ){
			for( j=0 ; j<r ; j++ ){
				a = a + matrix[j][i];
			}
			sb.append(Integer.toString(a)).append(" ");
			b = b + a;
			a = 0;
		}

		sb.append(Integer.toString(b)).append("\n");
		System.out.print(sb);
	}
}