import java.util.ArrayList;
import java.util.Scanner;

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

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    }

    public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    }
}
 import java.util.ArrayList; ArrayList java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    }

    public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    }
} Main public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    } main {
        Scanner inp = new Scanner(System.in);

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    } Scanner inp = new Scanner(System.in); inp new Scanner(System.in) ArrayList<Integer> input = new ArrayList<>(); input new ArrayList<>() int rows = inp.nextInt(); rows inp.nextInt() inp.nextInt inp int columns = inp.nextInt(); columns inp.nextInt() inp.nextInt inp ArrayList<ArrayList<Integer>> numbers = new ArrayList<>(); numbers new ArrayList<>() for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        } int i = 0; int i = 0; i 0 i < rows i rows i++ i++ i {
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        } {
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        } ArrayList<Integer> numberRow = new ArrayList<>(); numberRow new ArrayList<>() for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            } int j = 0; int j = 0; j 0 j < columns j columns j++ j++ j {
                numberRow.add(inp.nextInt());
            } {
                numberRow.add(inp.nextInt());
            } numberRow.add(inp.nextInt()) numberRow.add numberRow inp.nextInt() inp.nextInt inp numbers.add(numberRow) numbers.add numbers numberRow output(numbers, rows, columns) output numbers rows columns String[] args args public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    } output {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    } int totalTotal = 0; totalTotal 0 ArrayList<Integer> totalCol = new ArrayList<>(); totalCol new ArrayList<>() for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        } int i = 0; int i = 0; i 0 i < columns i columns i++ i++ i {
            totalCol.add(0);
        } {
            totalCol.add(0);
        } totalCol.add(0) totalCol.add totalCol 0 for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        } int i = 0; int i = 0; i 0 i < rows i rows i++ i++ i {
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        } {
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        } int total = 0; total 0 for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            } int j = 0; int j = 0; j 0 j < columns j columns j++ j++ j {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            } {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            } if (j != 0) {
                    System.out.print(" ");
                } j != 0 j 0 {
                    System.out.print(" ");
                } System.out.print(" ") System.out.print System.out System System.out " " int n = numbers.get(i).get(j); n numbers.get(i).get(j) numbers.get(i).get numbers.get(i) numbers.get numbers i j System.out.print(n) System.out.print System.out System System.out n total += n total n int colTol = totalCol.get(j); colTol totalCol.get(j) totalCol.get totalCol j totalCol.remove(j) totalCol.remove totalCol j totalCol.add(j, colTol+n) totalCol.add totalCol j colTol+n colTol n System.out.print(" " + total) System.out.print System.out System System.out " " + total " " total totalTotal += total totalTotal total total = 0 total 0 System.out.print("\n") System.out.print System.out System System.out "\n" for (int i : totalCol) {
            System.out.print(i + " ");
        } int i totalCol {
            System.out.print(i + " ");
        } {
            System.out.print(i + " ");
        } System.out.print(i + " ") System.out.print System.out System System.out i + " " i " " System.out.print(totalTotal + "\n") System.out.print System.out System System.out totalTotal + "\n" totalTotal "\n" ArrayList<ArrayList<Integer>> numbers numbers int rows rows int columns columns public class Main {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    }

    public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    }
} public class Main {
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    }

    public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    }
} Main
import java.util.ArrayList;
import java.util.Scanner;

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

        ArrayList<Integer> input = new ArrayList<>();

        int rows = inp.nextInt();
        int columns = inp.nextInt();

        ArrayList<ArrayList<Integer>> numbers = new ArrayList<>();

        for (int i = 0; i < rows; i++){
            ArrayList<Integer> numberRow = new ArrayList<>();
            for (int j = 0; j < columns; j++) {
                numberRow.add(inp.nextInt());
            }
            numbers.add(numberRow);
        }

        output(numbers, rows, columns);
    }

    public static void output (ArrayList<ArrayList<Integer>> numbers, int rows, int columns) {
        int totalTotal = 0;

        ArrayList<Integer> totalCol = new ArrayList<>();

        for (int i = 0; i < columns; i++) {
            totalCol.add(0);
        }

        for (int i = 0; i < rows; i++){
            int total = 0;
            for (int j = 0; j < columns; j++) {
                if (j != 0) {
                    System.out.print(" ");
                }

                int n = numbers.get(i).get(j);
                System.out.print(n);

                total += n;

                int colTol = totalCol.get(j);
                totalCol.remove(j);
                totalCol.add(j, colTol+n);
            }

            System.out.print(" " + total);

            totalTotal += total;
            total = 0;

            System.out.print("\n");
        }

        for (int i : totalCol) {
            System.out.print(i + " ");
        }

        System.out.print(totalTotal + "\n");
    }
}

