import java.util.Scanner;

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

        int r = in.nextInt();
        int c = in.nextInt();

        int[] rSum = new int[c + 1];

        for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        }
        for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        }

        System.out.print(sb.toString());
    }
} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();

        int r = in.nextInt();
        int c = in.nextInt();

        int[] rSum = new int[c + 1];

        for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        }
        for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        }

        System.out.print(sb.toString());
    }
} Main public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();

        int r = in.nextInt();
        int c = in.nextInt();

        int[] rSum = new int[c + 1];

        for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        }
        for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        }

        System.out.print(sb.toString());
    } main {
        Scanner in = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();

        int r = in.nextInt();
        int c = in.nextInt();

        int[] rSum = new int[c + 1];

        for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        }
        for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        }

        System.out.print(sb.toString());
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) StringBuilder sb = new StringBuilder(); sb new StringBuilder() int r = in.nextInt(); r in.nextInt() in.nextInt in int c = in.nextInt(); c in.nextInt() in.nextInt in int[] rSum = new int[c + 1]; rSum new int[c + 1] c + 1 c 1 for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        } {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        } int cSum = 0; cSum 0 for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            } {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            } int num = in.nextInt(); num in.nextInt() in.nextInt in cSum += num cSum num rSum[j] += num rSum[j] rSum j num rSum[c] += num rSum[c] rSum c num sb.append(num).append(" ") sb.append(num).append sb.append(num) sb.append sb num " " sb.append(cSum).append("\n") sb.append(cSum).append sb.append(cSum) sb.append sb cSum "\n" for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        } int i = 0; int i = 0; i 0 i < rSum.length i rSum.length rSum rSum.length i++ i++ i {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        } {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        } sb.append(rSum[i]) sb.append sb rSum[i] rSum i sb.append(i == c ? "\n" : " ") sb.append sb i == c ? "\n" : " " i == c i c "\n" " " System.out.print(sb.toString()) System.out.print System.out System System.out sb.toString() sb.toString sb String[] args args
import java.util.Scanner;

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

        int r = in.nextInt();
        int c = in.nextInt();

        int[] rSum = new int[c + 1];

        for (int i = 0; i < r; i++) {
            int cSum = 0;
            for (int j = 0; j < c; j++) {
                int num = in.nextInt();
                cSum += num;
                rSum[j] += num;
                rSum[c] += num;
                sb.append(num).append(" ");
            }
            sb.append(cSum).append("\n");
        }
        for (int i = 0; i < rSum.length; i++) {
            sb.append(rSum[i]);
            sb.append(i == c ? "\n" : " ");
        }

        System.out.print(sb.toString());
    }
}