import 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[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    } main {
        Scanner sc = new Scanner(System.in);

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

        int[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    } 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[][] t = new int[r+1][c+1]; t new int[r+1][c+1] r+1 r 1 c+1 c 1 for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt(); int row = 0; int row = 0; row 0 row < r row r row++ row++ row for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt(); for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt(); int col = 0; int col = 0; col 0 col < c col c col++ col++ col t[row][col] = sc.nextInt(); t[row][col] = sc.nextInt() t[row][col] t[row] t row col sc.nextInt() sc.nextInt sc for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        } int row = 0; int row = 0; row 0 row < r row r row++ row++ row {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        } {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        } int sum = 0; sum 0 for (int col = 0; col < c; col++)
                sum += t[row][col]; int col = 0; int col = 0; col 0 col < c col c col++ col++ col sum += t[row][col]; sum += t[row][col] sum t[row][col] t[row] t row col t[row][c] = sum t[row][c] t[row] t row c sum for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        } int col = 0; int col = 0; col 0 col <= c col c col++ col++ col {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        } {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        } int sum = 0; sum 0 for (int row = 0; row < r; row++)
                sum += t[row][col]; int row = 0; int row = 0; row 0 row < r row r row++ row++ row sum += t[row][col]; sum += t[row][col] sum t[row][col] t[row] t row col t[r][col] = sum t[r][col] t[r] t r col sum for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        } int row = 0; int row = 0; row 0 row <= r row r row++ row++ row {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        } {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        } for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]); int col = 0; int col = 0; col 0 col <= c col c col++ col++ col System.out.print((col == 0) ? t[row][col] : " " + t[row][col]); System.out.print((col == 0) ? t[row][col] : " " + t[row][col]) System.out.print System.out System System.out (col == 0) ? t[row][col] : " " + t[row][col] (col == 0) col 0 t[row][col] t[row] t row col " " + t[row][col] " " t[row][col] t[row] t row col System.out.println() System.out.println System.out System System.out 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[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    }
} Main
import 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[][] t = new int[r+1][c+1];
        
        for (int row = 0; row < r; row++)
            for (int col = 0; col < c; col++)
                t[row][col] = sc.nextInt();

        for (int row = 0; row < r; row++) {
            int sum = 0;
            for (int col = 0; col < c; col++)
                sum += t[row][col];
            t[row][c] = sum;
        }

        for (int col = 0; col <= c; col++) {
            int sum = 0;
            for (int row = 0; row < r; row++)
                sum += t[row][col];
            t[r][col] = sum;
        }

        for (int row = 0; row <= r; row++) {
            for (int col = 0; col <= c; col++)
                System.out.print((col == 0) ? t[row][col] : " " + t[row][col]);
            System.out.println();
        }
    }
}
