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

public class Main {
 
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    }

    private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
 
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    }

    private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    }
} Main public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    } main {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] rc = br.readLine().split(" "); rc br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(rc[0]); r Integer.parseInt(rc[0]) Integer.parseInt Integer rc[0] rc 0 int c = Integer.parseInt(rc[1]); c Integer.parseInt(rc[1]) Integer.parseInt Integer rc[1] rc 1 int[][] table = new int[r + 1][c + 1]; table new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
    		table[i] = convertInt(br.readLine().split(" "));
    	} {
    		table[i] = convertInt(br.readLine().split(" "));
    	} table[i] = convertInt(br.readLine().split(" ")) table[i] table i convertInt(br.readLine().split(" ")) convertInt br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	} int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	} {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	} for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		} int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		} {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		} if (j == c) {
    				continue;
    			} j == c j c {
    				continue;
    			} continue; if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			} i != r i r {
    				table[r][j] = table[r][j] + table[i][j];
    			} table[r][j] = table[r][j] + table[i][j] table[r][j] table[r] table r j table[r][j] + table[i][j] table[r][j] table[r] table r j table[i][j] table[i] table i j table[i][c] = table[i][c] + table[i][j] table[i][c] table[i] table i c table[i][c] + table[i][j] table[i][c] table[i] table i c table[i][j] table[i] table i j for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	} int[] row table {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	} {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	} int cnt = 0; cnt 0 for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		} int n row {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		} {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		} System.out.print(n) System.out.print System.out System System.out n if (cnt != c) {
    				System.out.print(" ");
    			} cnt != c cnt c {
    				System.out.print(" ");
    			} System.out.print(" ") System.out.print System.out System System.out " " cnt++ cnt System.out.println("") System.out.println System.out System System.out "" String[] args args private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    } convertInt {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    } int len = str.length; len str.length str str.length int[] iArray = new int[len + 1]; iArray new int[len + 1] len + 1 len 1 for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	} int i = 0; int i = 0; i 0 i < len i len i++ i++ i {
    		iArray[i] = Integer.parseInt(str[i]);
    	} {
    		iArray[i] = Integer.parseInt(str[i]);
    	} iArray[i] = Integer.parseInt(str[i]) iArray[i] iArray i Integer.parseInt(str[i]) Integer.parseInt Integer str[i] str i return iArray; iArray String[] str str public class Main {
 
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    }

    private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    }
} public class Main {
 
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    }

    private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
 
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] rc = br.readLine().split(" ");
    	int r = Integer.parseInt(rc[0]);
    	int c = Integer.parseInt(rc[1]);
    	int[][] table = new int[r + 1][c + 1];
    	for (int i = 0; i < r; i++) {
    		table[i] = convertInt(br.readLine().split(" "));
    	}
    	for (int i = 0; i <= r; i++) {
    		for (int j = 0; j <= c; j++) {
    			if (j == c) {
    				continue;
    			}
    			if (i != r) {
    				table[r][j] = table[r][j] + table[i][j];
    			}
    			table[i][c] = table[i][c] + table[i][j];
    		}
    	}
    	for (int[] row : table) {
    		int cnt = 0;
    		for (int n : row) {
    			System.out.print(n);
    			if (cnt != c) {
    				System.out.print(" ");
    			}
    			cnt++;
    		}
    		System.out.println("");
    	}

    }

    private static int[] convertInt(String[] str) {
    	int len = str.length;
    	int[] iArray = new int[len + 1];
    	for (int i = 0; i < len; i++) {
    		iArray[i] = Integer.parseInt(str[i]);
    	}
    	return iArray;
    }
}