import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    }
} import java.io.IOException; IOException java.io import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    }
} Main public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] input = br.readLine().split(" "); input br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.valueOf(input[0]); r Integer.valueOf(input[0]) Integer.valueOf Integer input[0] input 0 int c = Integer.valueOf(input[1]); c Integer.valueOf(input[1]) Integer.valueOf Integer input[1] input 1 int[][] matrix = new int[r+1][c+1]; matrix new int[r+1][c+1] r+1 r 1 c+1 c 1 for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        } {
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        } String[] line = br.readLine().split(" " , c); line br.readLine().split(" " , c) br.readLine().split br.readLine() br.readLine br " " c for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                matrix[i][j] = Integer.parseInt(line[j]);
            } {
                matrix[i][j] = Integer.parseInt(line[j]);
            } matrix[i][j] = Integer.parseInt(line[j]) matrix[i][j] matrix[i] matrix i j Integer.parseInt(line[j]) Integer.parseInt Integer line[j] line j for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        } {
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        } for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                matrix[i][c] += matrix[i][j];
            } {
                matrix[i][c] += matrix[i][j];
            } matrix[i][c] += matrix[i][j] matrix[i][c] matrix[i] matrix i c matrix[i][j] matrix[i] matrix i j for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        } int i = 0; int i = 0; i 0 i < c+1 i c+1 c 1 i++ i++ i {
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        } {
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        } for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            } int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
                matrix[r][i] += matrix[j][i];
            } {
                matrix[r][i] += matrix[j][i];
            } matrix[r][i] += matrix[j][i] matrix[r][i] matrix[r] matrix r i matrix[j][i] matrix[j] matrix j i StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        } int i = 0; int i = 0; i 0 i <= r i r i++ i++ i {
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        } {
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        } for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            } int j = 0; int j = 0; j 0 j <= c j c j++ j++ j {
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            } {
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            } sb.append(matrix[i][j]) sb.append sb matrix[i][j] matrix[i] matrix i j if(j != c) sb.append(" "); j != c j c sb.append(" ") sb.append sb " " if(i != r)sb.append("\n"); i != r i r sb.append("\n") sb.append sb "\n" System.out.println(sb) System.out.println System.out System System.out sb String[] args args public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    }
} public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    }
} Main
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int r = Integer.valueOf(input[0]);
        int c = Integer.valueOf(input[1]);

        int[][] matrix = new int[r+1][c+1];
        
        //input data
        for(int i = 0; i < r; i++){
            String[] line = br.readLine().split(" " , c);
            for(int j = 0; j < c; j++){
                matrix[i][j] = Integer.parseInt(line[j]);
            }
        }

        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                matrix[i][c] += matrix[i][j];
            }
        }

        for(int i = 0; i < c+1; i++){
            for(int j = 0; j < r; j++){
                matrix[r][i] += matrix[j][i];
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i <= r; i++){
            for(int j = 0; j <= c; j++){
                sb.append(matrix[i][j]);
                if(j != c) sb.append(" ");
            }
            if(i != r)sb.append("\n");
        }
        System.out.println(sb);

    }
}