import java.util.Scanner;
 
class Main {
    Scanner sc = new Scanner(System.in);
 
    public void run() {
        int r = sc.nextInt(), c = sc.nextInt();
 
        int[][] A = new int[r + 1][c + 1];
 
        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++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        }
        for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        }
 
        for (int i = 0; i <= r; i++)
            ln(join(A[i], " "));
    }
 
    public static void main(String[] args) {
        new Main().run();
    }
 
    public static void pr(Object o) {
        System.out.print(o);
    }
 
    public static void ln(Object o) {
        System.out.println(o);
    }
 
    public static void ln() {
        System.out.println();
    }
 
    public static String join(int[] array, String separator) {
        StringBuilder str = new StringBuilder(array[0] + "");
        for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        }
        return str.toString();
    }
} import java.util.Scanner; Scanner java.util class Main {
    Scanner sc = new Scanner(System.in);
 
    public void run() {
        int r = sc.nextInt(), c = sc.nextInt();
 
        int[][] A = new int[r + 1][c + 1];
 
        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++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        }
        for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        }
 
        for (int i = 0; i <= r; i++)
            ln(join(A[i], " "));
    }
 
    public static void main(String[] args) {
        new Main().run();
    }
 
    public static void pr(Object o) {
        System.out.print(o);
    }
 
    public static void ln(Object o) {
        System.out.println(o);
    }
 
    public static void ln() {
        System.out.println();
    }
 
    public static String join(int[] array, String separator) {
        StringBuilder str = new StringBuilder(array[0] + "");
        for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        }
        return str.toString();
    }
} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public void run() {
        int r = sc.nextInt(), c = sc.nextInt();
 
        int[][] A = new int[r + 1][c + 1];
 
        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++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        }
        for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        }
 
        for (int i = 0; i <= r; i++)
            ln(join(A[i], " "));
    } run {
        int r = sc.nextInt(), c = sc.nextInt();
 
        int[][] A = new int[r + 1][c + 1];
 
        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++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        }
        for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        }
 
        for (int i = 0; i <= r; i++)
            ln(join(A[i], " "));
    } int r = sc.nextInt() r sc.nextInt() sc.nextInt sc c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int[][] A = new int[r + 1][c + 1]; A new int[r + 1][c + 1] r + 1 r 1 c + 1 c 1 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();
            } 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] = sc.nextInt() A[i][j] A[i] A i j sc.nextInt() sc.nextInt sc for (int i = 0; i < r; i++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        } {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        } int sum = 0; sum 0 for (int j = 0; j < c; j++) {
                sum += A[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                sum += A[i][j];
            } {
                sum += A[i][j];
            } sum += A[i][j] sum A[i][j] A[i] A i j A[i][c] = sum A[i][c] A[i] A i c sum for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        } int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        } {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        } int sum = 0; sum 0 for (int i = 0; i < r; i++) {
                sum += A[i][j];
            } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
                sum += A[i][j];
            } {
                sum += A[i][j];
            } sum += A[i][j] sum A[i][j] A[i] A i j A[r][j] = sum A[r][j] A[r] A r j sum for (int i = 0; i <= r; i++)
            ln(join(A[i], " ")); int i = 0; int i = 0; i 0 i <= r i r i++ i++ i ln(join(A[i], " ")); ln(join(A[i], " ")) ln join(A[i], " ") join A[i] A i " " public static void main(String[] args) {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args public static void pr(Object o) {
        System.out.print(o);
    } pr {
        System.out.print(o);
    } System.out.print(o) System.out.print System.out System System.out o Object o o public static void ln(Object o) {
        System.out.println(o);
    } ln {
        System.out.println(o);
    } System.out.println(o) System.out.println System.out System System.out o Object o o public static void ln() {
        System.out.println();
    } ln {
        System.out.println();
    } System.out.println() System.out.println System.out System System.out public static String join(int[] array, String separator) {
        StringBuilder str = new StringBuilder(array[0] + "");
        for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        }
        return str.toString();
    } join {
        StringBuilder str = new StringBuilder(array[0] + "");
        for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        }
        return str.toString();
    } StringBuilder str = new StringBuilder(array[0] + ""); str new StringBuilder(array[0] + "") for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        } int i = 1; int i = 1; i 1 i < array.length i array.length array array.length i++ i++ i {
            str.append(separator).append(array[i] + "");
        } {
            str.append(separator).append(array[i] + "");
        } str.append(separator).append(array[i] + "") str.append(separator).append str.append(separator) str.append str separator array[i] + "" array[i] array i "" return str.toString(); str.toString() str.toString str int[] array array String separator separator
import java.util.Scanner;
 
class Main {
    Scanner sc = new Scanner(System.in);
 
    public void run() {
        int r = sc.nextInt(), c = sc.nextInt();
 
        int[][] A = new int[r + 1][c + 1];
 
        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++) {
            int sum = 0;
            for (int j = 0; j < c; j++) {
                sum += A[i][j];
            }
            A[i][c] = sum;
        }
        for (int j = 0; j <= c; j++) {
            int sum = 0;
            for (int i = 0; i < r; i++) {
                sum += A[i][j];
            }
            A[r][j] = sum;
        }
 
        for (int i = 0; i <= r; i++)
            ln(join(A[i], " "));
    }
 
    public static void main(String[] args) {
        new Main().run();
    }
 
    public static void pr(Object o) {
        System.out.print(o);
    }
 
    public static void ln(Object o) {
        System.out.println(o);
    }
 
    public static void ln() {
        System.out.println();
    }
 
    public static String join(int[] array, String separator) {
        StringBuilder str = new StringBuilder(array[0] + "");
        for (int i = 1; i < array.length; i++) {
            str.append(separator).append(array[i] + "");
        }
        return str.toString();
    }
}
