import java.io.*;
import java.util.*;

class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int r=Integer.parseInt(st.nextToken());
        int c=Integer.parseInt(st.nextToken());
        int[][] matrix=new int[r+1][c+1];
        int sum;
        for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        }for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        }
        for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int r=Integer.parseInt(st.nextToken());
        int c=Integer.parseInt(st.nextToken());
        int[][] matrix=new int[r+1][c+1];
        int sum;
        for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        }for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        }
        for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        }
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int r=Integer.parseInt(st.nextToken());
        int c=Integer.parseInt(st.nextToken());
        int[][] matrix=new int[r+1][c+1];
        int sum;
        for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        }for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        }
        for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        }
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int r=Integer.parseInt(st.nextToken());
        int c=Integer.parseInt(st.nextToken());
        int[][] matrix=new int[r+1][c+1];
        int sum;
        for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        }for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        }
        for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int r=Integer.parseInt(st.nextToken()); r Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int c=Integer.parseInt(st.nextToken()); c Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int[][] matrix=new int[r+1][c+1]; matrix new int[r+1][c+1] r+1 r 1 c+1 c 1 int sum; sum for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        } int i=0; int i=0; i 0 i<r i r i++ i++ i {
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        } {
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        } st=new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) sum=0 sum 0 for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            } int j=0; int j=0; j 0 j<c j c j++ j++ j {
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            } {
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            } matrix[i][j]=Integer.parseInt(st.nextToken()) matrix[i][j] matrix[i] matrix i j Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st sum+=matrix[i][j] sum matrix[i][j] matrix[i] matrix i j matrix[i][c]=sum matrix[i][c] matrix[i] matrix i c sum for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        } int i=0; int i=0; i 0 i<c+1 i c+1 c 1 i++ i++ i {
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        } {
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        } sum=0 sum 0 for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            } int j=0; int j=0; j 0 j<r j r j++ j++ j {
                sum+=matrix[j][i];
            } {
                sum+=matrix[j][i];
            } sum+=matrix[j][i] sum matrix[j][i] matrix[j] matrix j i matrix[r][i]=sum matrix[r][i] matrix[r] matrix r i sum for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        } int i=0; int i=0; i 0 i<r+1 i r+1 r 1 i++ i++ i {
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        } {
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        } for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            } int j=0; int j=0; j 0 j<c j c j++ j++ j {
                System.out.print(matrix[i][j]+" ");
            } {
                System.out.print(matrix[i][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 " " System.out.println(matrix[i][c]) System.out.println System.out System System.out matrix[i][c] matrix[i] matrix i c String[] args args
import java.io.*;
import java.util.*;

class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(br.readLine());
        int r=Integer.parseInt(st.nextToken());
        int c=Integer.parseInt(st.nextToken());
        int[][] matrix=new int[r+1][c+1];
        int sum;
        for(int i=0;i<r;i++){
            st=new StringTokenizer(br.readLine());
            sum=0;
            for(int j=0;j<c;j++){
                matrix[i][j]=Integer.parseInt(st.nextToken());
                sum+=matrix[i][j];
            }matrix[i][c]=sum;
        }for(int i=0;i<c+1;i++){
            sum=0;
            for(int j=0;j<r;j++){
                sum+=matrix[j][i];
            }matrix[r][i]=sum;
        }
        for(int i=0;i<r+1;i++){
            for(int j=0;j<c;j++){
                System.out.print(matrix[i][j]+" ");
            }System.out.println(matrix[i][c]);
        }
    }
}