import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    }
} import java.util.*; util.* java public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    }
} Main public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    } main {
        Main main = new Main();
        main.solv();
    } Main main = new Main(); main new Main() main.solv() main.solv main String[] args args private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    } solv {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) int r = input.nextInt(); r input.nextInt() input.nextInt input int c = input.nextInt(); c input.nextInt() input.nextInt input int[][] matrix = new int[r+1][c+1]; matrix new int[r+1][c+1] r+1 r 1 c+1 c 1 for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        } {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        } int sum = 0; sum 0 for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            } {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            } matrix[i][j] = input.nextInt() matrix[i][j] matrix[i] matrix i j input.nextInt() input.nextInt input sum += matrix[i][j] sum matrix[i][j] matrix[i] matrix i j matrix[i][c] = sum matrix[i][c] matrix[i] matrix i c sum for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        } int i = 0; int i = 0; i 0 i < c + 1 i c + 1 c 1 i++ i++ i {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        } {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        } int 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 matrix[r][i] = sum matrix[r][i] matrix[r] matrix r i sum for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        } int i = 0; int i = 0; i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        } {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        } StringBuilder sb = new StringBuilder(); sb new StringBuilder() for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            } int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
                sb.append(matrix[i][j]).append(' ');
            } {
                sb.append(matrix[i][j]).append(' ');
            } sb.append(matrix[i][j]).append(' ') sb.append(matrix[i][j]).append sb.append(matrix[i][j]) sb.append sb matrix[i][j] matrix[i] matrix i j ' ' System.out.println(sb.toString().trim()) System.out.println System.out System System.out sb.toString().trim() sb.toString().trim sb.toString() sb.toString sb public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    }
} public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    }
} Main
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        int r = input.nextInt();
        int c = input.nextInt();
        int[][] matrix = new int[r+1][c+1];
        for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                matrix[i][j] = input.nextInt();
                sum += matrix[i][j];
            }
            matrix[i][c] = sum;
        }
        for (int i = 0; i < c + 1; i++) {
            int sum = 0;
            for (int j = 0; j < r; j++) {
                sum += matrix[j][i];
            }
            matrix[r][i] = sum;
        }
        for (int i = 0; i < r + 1; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c + 1; j++) {
                sb.append(matrix[i][j]).append(' ');
            }
            System.out.println(sb.toString().trim());
        }
    }
}