import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    } main {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int r = Integer.parseInt(sc.next()); r Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int c = Integer.parseInt(sc.next()); c Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[][] M = new int[r+1][c+1]; M 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++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        } {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        } for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                M[i][j] = Integer.parseInt(sc.next());
            } {
                M[i][j] = Integer.parseInt(sc.next());
            } M[i][j] = Integer.parseInt(sc.next()) M[i][j] M[i] M i j Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        } {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        } for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            } {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            } M[r][j] += M[i][j] M[r][j] M[r] M r j M[i][j] M[i] M i j M[i][c] += M[i][j] M[i][c] M[i] M i c M[i][j] M[i] M i j M[r][c] += M[i][c] M[r][c] M[r] M r c M[i][c] M[i] M i c for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        } int i = 0; int i = 0; i 0 i < r + 1 i r + 1 r 1 i++ i++ i {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        } {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        } for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            } int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            } {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            } if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                } j == c j c {
                    System.out.printf("%d\n", M[i][j]);
                } System.out.printf("%d\n", M[i][j]) System.out.printf System.out System System.out "%d\n" M[i][j] M[i] M i j {
                    System.out.printf("%d ", M[i][j]);
                } System.out.printf("%d ", M[i][j]) System.out.printf System.out System System.out "%d " M[i][j] M[i] M i j String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int r = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());

        int[][] M = new int[r+1][c+1];

        for (int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                M[i][j] = Integer.parseInt(sc.next());
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                M[r][j] += M[i][j];
                M[i][c] += M[i][j];
            }
            M[r][c] += M[i][c];
        }

        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j == c ) {
                    System.out.printf("%d\n", M[i][j]);
                } else {
                    System.out.printf("%d ", M[i][j]);
                }
            }
        }
    }
}