
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    }
}


 import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    }
} Main public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    } main {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int m = sc.nextInt(); m sc.nextInt() sc.nextInt sc int[][] a = new int[n][m]; a new int[n][m] n m int[] sums = new int[m+1]; sums new int[m+1] m+1 m 1 for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        } int i=0; int i=0; i 0 i < n i n i++ i++ i {
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        } {
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        } for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            } int j=0; int j=0; j 0 j < m j m 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 int sum = 0; sum 0 int total = 0; total 0 for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        } int i=0; int i=0; i 0 i < n i n i++ i++ i {
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        } {
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        } for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            } int j=0; int j=0; j 0 j < m j m j++ j++ j {
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            } {
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            } System.out.print(a[i][j] +" ") System.out.print System.out System System.out a[i][j] +" " a[i][j] a[i] a i j " " sum += a[i][j] sum a[i][j] a[i] a i j if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                } j == m-1 j m-1 m 1 {
                    System.out.println(sum);
                    sum = 0;
                } System.out.println(sum) System.out.println System.out System System.out sum sum = 0 sum 0 sums[j] += a[i][j] sums[j] sums j a[i][j] a[i] a i j for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        } int i=0; int i=0; i 0 i<sums.length -1 i sums.length -1 sums.length sums sums.length 1 i++ i++ i {
            System.out.print(sums[i] + " ");
            total += sums[i];
        } {
            System.out.print(sums[i] + " ");
            total += sums[i];
        } System.out.print(sums[i] + " ") System.out.print System.out System System.out sums[i] + " " sums[i] sums i " " total += sums[i] total sums[i] sums i System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    }
} public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    }
} Main

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] a = new int[n][m];
        int[] sums = new int[m+1];
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                a[i][j] = sc.nextInt();
            }
        }
        
        int sum = 0;        
        int total = 0;
        
        for(int i=0; i < n; i++){
            for(int j=0; j < m; j++){
                
                System.out.print(a[i][j] +" ");
                sum += a[i][j];
                
                if(j == m-1){
                    System.out.println(sum);
                    sum = 0;
                }
                
                sums[j] += a[i][j];
             
            }
        }
        
        for(int i=0; i<sums.length -1; i++){
            System.out.print(sums[i] + " ");
            total += sums[i];
        }
        
        System.out.println(total);
        
    }
}



