import java.util.Scanner;

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

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

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

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

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

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int[][] A = new int[r][c]; A new int[r][c] r c int[][] n = new int[r + 1][c + 1]; n new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 for(int i = 0; i < r; i++)
            n[i][c] = 0; int i = 0; int i = 0; i 0 i < r i r i++ i++ i n[i][c] = 0; n[i][c] = 0 n[i][c] n[i] n i c 0 for(int i = 0; i < c; i++)
            n[r][i] = 0; int i = 0; int i = 0; i 0 i < c i c i++ i++ i n[r][i] = 0; n[r][i] = 0 n[r][i] n[r] n r i 0 for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        } {
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        } for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt(); int j = 0; int j = 0; j 0 j < c j c j++ j++ j A[i][j] = sc.nextInt(); A[i][j] = sc.nextInt() A[i][j] A[i] A i j sc.nextInt() sc.nextInt sc for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                n[i][j] = A[i][j];
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++)
                n[i][j] = A[i][j];
        } {
            for(int j = 0; j < c; j++)
                n[i][j] = A[i][j];
        } for(int j = 0; j < c; j++)
                n[i][j] = A[i][j]; int j = 0; int j = 0; j 0 j < c j c j++ j++ j n[i][j] = A[i][j]; n[i][j] = A[i][j] n[i][j] n[i] n i j A[i][j] A[i] A i j for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                n[i][c] += A[i][j];
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++)
                n[i][c] += A[i][j];
        } {
            for(int j = 0; j < c; j++)
                n[i][c] += A[i][j];
        } for(int j = 0; j < c; j++)
                n[i][c] += A[i][j]; int j = 0; int j = 0; j 0 j < c j c j++ j++ j n[i][c] += A[i][j]; n[i][c] += A[i][j] n[i][c] n[i] n i c A[i][j] A[i] A i j for(int i = 0; i < c; i++){
            for(int j = 0; j < r; j++)
                n[r][i] += A[j][i];
        } int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
            for(int j = 0; j < r; j++)
                n[r][i] += A[j][i];
        } {
            for(int j = 0; j < r; j++)
                n[r][i] += A[j][i];
        } for(int j = 0; j < r; j++)
                n[r][i] += A[j][i]; int j = 0; int j = 0; j 0 j < r j r j++ j++ j n[r][i] += A[j][i]; n[r][i] += A[j][i] n[r][i] n[r] n r i A[j][i] A[j] A j i n[r][c] = 0 n[r][c] n[r] n r c 0 for(int i = 0; i< r; i++){
            for(int j = 0; j < c; j++)
                n[r][c] += A[i][j];
        } int i = 0; int i = 0; i 0 i< r i r i++ i++ i {
            for(int j = 0; j < c; j++)
                n[r][c] += A[i][j];
        } {
            for(int j = 0; j < c; j++)
                n[r][c] += A[i][j];
        } for(int j = 0; j < c; j++)
                n[r][c] += A[i][j]; int j = 0; int j = 0; j 0 j < c j c j++ j++ j n[r][c] += A[i][j]; n[r][c] += A[i][j] n[r][c] n[r] n r c A[i][j] A[i] A i j for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        } int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        } {
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        } System.out.print(n[i][0]) System.out.print System.out System System.out n[i][0] n[i] n i 0 for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]); int j = 1; int j = 1; j 1 j <= c j c j++ j++ j System.out.print(" " + n[i][j]); System.out.print(" " + n[i][j]) System.out.print System.out System System.out " " + n[i][j] " " n[i][j] n[i] n i j System.out.println("") System.out.println System.out System System.out "" sc.close() sc.close sc String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

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

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

        sc.close();
    }
} Main
import java.util.Scanner;

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

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

        int[][] A = new int[r][c];
        int[][] n = new int[r + 1][c + 1];

        for(int i = 0; i < r; i++)
            n[i][c] = 0;

        for(int i = 0; i < c; i++)
            n[r][i] = 0;

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++)
                A[i][j] = sc.nextInt();
        }

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

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

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

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

        for(int i = 0; i <= r; i++){
            System.out.print(n[i][0]);
            for(int j = 1; j <= c; j++)
                System.out.print(" " + n[i][j]);
            System.out.println("");
        }

        sc.close();
    }
}
