import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    } main {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int m = scan.nextInt(); m scan.nextInt() scan.nextInt scan int [] [] a = new int[n + 1][m + 1]; a new int[n + 1][m + 1] n + 1 n 1 m + 1 m 1 for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        } int x = 0; int x = 0; x 0 x < n x n x++ x++ x {
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        } {
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        } for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            } int y = 0; int y = 0; y 0 y < m y m y++ y++ y {
                a[x][y] = scan.nextInt();
            } {
                a[x][y] = scan.nextInt();
            } a[x][y] = scan.nextInt() a[x][y] a[x] a x y scan.nextInt() scan.nextInt scan int sum = 0; sum 0 for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        } int x = 0; int x = 0; x 0 x < n x n x++ x++ x {
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        } {
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        } for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            } int y = 0; int y = 0; y 0 y < m y m y++ y++ y {
                sum = sum + a[x][y];
            } {
                sum = sum + a[x][y];
            } sum = sum + a[x][y] sum sum + a[x][y] sum a[x][y] a[x] a x y a[x] [m] = sum a[x] [m] a[x] a x m sum sum = 0 sum 0 for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        } int x = 0; int x = 0; x 0 x < m x m x++ x++ x {
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        } {
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        } for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            } int y = 0; int y = 0; y 0 y < n y n y++ y++ y {
                sum = sum + a[y] [x];
            } {
                sum = sum + a[y] [x];
            } sum = sum + a[y] [x] sum sum + a[y] [x] sum a[y] [x] a[y] a y x a[n] [x] = sum a[n] [x] a[n] a n x sum sum = 0 sum 0 for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        } int x = 0; int x = 0; x 0 x < m x m x++ x++ x {
            sum = sum + a[n] [x];
        } {
            sum = sum + a[n] [x];
        } sum = sum + a[n] [x] sum sum + a[n] [x] sum a[n] [x] a[n] a n x a[n] [m] = sum a[n] [m] a[n] a n m sum for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        } int x = 0; int x = 0; x 0 x <= n x n x++ x++ x {
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        } {
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        } for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            } int y = 0; int y = 0; y 0 y <= m y m y++ y++ y {
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            } {
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            } if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                } y == m y m {
                    System.out.println(a[x] [m]);
                } System.out.println(a[x] [m]) System.out.println System.out System System.out a[x] [m] a[x] a x m {
                    System.out.print(a[x] [y] + " ");
                } System.out.print(a[x] [y] + " ") System.out.print System.out System System.out a[x] [y] + " " a[x] [y] a[x] a x y " " String[] args args public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    }
} public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int [] [] a = new int[n + 1][m + 1];
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                a[x][y] = scan.nextInt();
            }

        }
        int sum = 0;
        for (int x = 0; x < n; x++){
            for(int y = 0; y < m;y++){
                sum = sum + a[x][y];
            }
            a[x] [m] = sum;
            sum = 0;
        }
        for (int x = 0; x < m; x++){
            for(int y = 0; y < n;y++){
                sum = sum + a[y] [x];
            }
            a[n] [x] = sum;
            sum = 0;
        }
        for(int x = 0; x < m; x++){
            sum = sum + a[n] [x];
        }
        a[n] [m] = sum;
        for (int x = 0; x <= n; x++){
            for(int y = 0; y <= m;y++){
                if(y == m){
                    System.out.println(a[x] [m]);
                }
                else{
                    System.out.print(a[x] [y] + " ");
                }
            }
        }
    }
}
