import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    } main {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int sumH = 0; sumH 0 int sumV = 0; sumV 0 int[][] l = new int[r + 1][c + 1]; l new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        } int v = 0; int v = 0; v 0 v < r v r v++ v++ v {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        } {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        } for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            } int h = 0; int h = 0; h 0 h < c h c h++ h++ h {
                l[v][h] = scan.nextInt();
            } {
                l[v][h] = scan.nextInt();
            } l[v][h] = scan.nextInt() l[v][h] l[v] l v h scan.nextInt() scan.nextInt scan for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        } int v = 0; int v = 0; v 0 v < r v r v++ v++ v {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        } {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        } for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            } int h = 0; int h = 0; h 0 h < c h c h++ h++ h {
                sumH += l[v][h];
            } {
                sumH += l[v][h];
            } sumH += l[v][h] sumH l[v][h] l[v] l v h l[v][c] = sumH l[v][c] l[v] l v c sumH sumH = 0 sumH 0 for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        } int h = 0; int h = 0; h 0 h < c +1 h c +1 c 1 h++ h++ h {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        } {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        } for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            } int v = 0; int v = 0; v 0 v < r +1 v r +1 r 1 v++ v++ v {
                sumV += l[v][h];
            } {
                sumV += l[v][h];
            } sumV += l[v][h] sumV l[v][h] l[v] l v h l[r][h] = sumV l[r][h] l[r] l r h sumV sumV = 0 sumV 0 for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        } int v = 0; int v = 0; v 0 v < r + 1 v r + 1 r 1 v++ v++ v {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        } {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        } for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            } int h = 0; int h = 0; h 0 h < c + 1 h c + 1 c 1 h++ h++ h {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            } {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            } if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                } h == c h c {
                    System.out.print(l[v][h]);
                } System.out.print(l[v][h]) System.out.print System.out System System.out l[v][h] l[v] l v h {
                    System.out.print(l[v][h] + " ");
                } System.out.print(l[v][h] + " ") System.out.print System.out System System.out l[v][h] + " " l[v][h] l[v] l v h " " System.out.println() System.out.println System.out System System.out String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int r = scan.nextInt();
        int c = scan.nextInt();
        int sumH = 0;
        int sumV = 0;
        int[][] l = new int[r + 1][c + 1];

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                l[v][h] = scan.nextInt();
            }
        }

        for (int v = 0; v < r; v++) {
            for (int h = 0; h < c; h++) {
                sumH += l[v][h];
            }
            l[v][c] = sumH;
            sumH = 0;
        }


        for (int h = 0; h < c +1; h++) {
            for (int v = 0; v < r +1; v++) {
                sumV += l[v][h];
            }
            l[r][h] = sumV;
            sumV = 0;
        }

        for (int v = 0; v < r + 1; v++) {
            for (int h = 0; h < c + 1; h++) {
                if (h == c) {
                    System.out.print(l[v][h]);
                } else {
                    System.out.print(l[v][h] + " ");
                }
            }
            System.out.println();
        }
    }
}