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 matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    }
}
 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 matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int r=sc.nextInt();
        int c=sc.nextInt();
        int matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    } main {
        Scanner sc = new Scanner(System.in);
        int r=sc.nextInt();
        int c=sc.nextInt();
        int matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    } 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 matrix[][]=new int[r][c]; matrix new int[r][c] r c for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[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++){
                matrix[i][j]=sc.nextInt();
            }
        } {
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        } for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            } int j=0; int j=0; j 0 j<c j c j++ j++ j {
                matrix[i][j]=sc.nextInt();
            } {
                matrix[i][j]=sc.nextInt();
            } matrix[i][j]=sc.nextInt() matrix[i][j] matrix[i] matrix i j sc.nextInt() sc.nextInt sc int rc_sum=0; rc_sum 0 for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        } int i=0; int i=0; i 0 i<r i r i++ i++ i {
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        } {
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        } int c_sum=0; c_sum 0 for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            } int j=0; int j=0; j 0 j<c j c j++ j++ j {
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            } {
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            } System.out.print(matrix[i][j]+" ") System.out.print System.out System System.out matrix[i][j]+" " matrix[i][j] matrix[i] matrix i j " " c_sum+=matrix[i][j] c_sum matrix[i][j] matrix[i] matrix i j if(i!=0)matrix[i][j]+=matrix[i-1][j]; i!=0 i 0 matrix[i][j]+=matrix[i-1][j] matrix[i][j] matrix[i] matrix i j matrix[i-1][j] matrix[i-1] matrix i-1 i 1 j System.out.println(c_sum) System.out.println System.out System System.out c_sum rc_sum+=c_sum rc_sum c_sum for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        } int j=0; int j=0; j 0 j<c j c j++ j++ j {
            System.out.print(matrix[r-1][j]+" ");
        } {
            System.out.print(matrix[r-1][j]+" ");
        } System.out.print(matrix[r-1][j]+" ") System.out.print System.out System System.out matrix[r-1][j]+" " matrix[r-1][j] matrix[r-1] matrix r-1 r 1 j " " System.out.println(rc_sum) System.out.println System.out System System.out rc_sum 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 matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int r=sc.nextInt();
        int c=sc.nextInt();
        int matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    }
} 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 matrix[][]=new int[r][c];
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        int rc_sum=0;
        for(int i=0;i<r;i++){
            int c_sum=0;
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
                c_sum+=matrix[i][j];
                if(i!=0)matrix[i][j]+=matrix[i-1][j];
            }
            System.out.println(c_sum);
            rc_sum+=c_sum;
        }
        for(int j=0;j<c;j++){
            System.out.print(matrix[r-1][j]+" ");
        }
        System.out.println(rc_sum);
    }
}

