import java.io.*;

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

        String[] str1 = br.readLine().split(" ");
        int r = Integer.parseInt(str1[0]);
        int c = Integer.parseInt(str1[1]);
        int[][] data = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumX = 0;

        // r x c ????????????????¨??????\???????????????????´???????
        for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        }

        // ????¨????sumR????¨??????????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        }
        // ????¨????sumC????¨??????????
        for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        }

        //??????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        }
        for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        }
        sb.append(sumX).append("\n");

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

        String[] str1 = br.readLine().split(" ");
        int r = Integer.parseInt(str1[0]);
        int c = Integer.parseInt(str1[1]);
        int[][] data = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumX = 0;

        // r x c ????????????????¨??????\???????????????????´???????
        for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        }

        // ????¨????sumR????¨??????????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        }
        // ????¨????sumC????¨??????????
        for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        }

        //??????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        }
        for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        }
        sb.append(sumX).append("\n");

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

        String[] str1 = br.readLine().split(" ");
        int r = Integer.parseInt(str1[0]);
        int c = Integer.parseInt(str1[1]);
        int[][] data = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumX = 0;

        // r x c ????????????????¨??????\???????????????????´???????
        for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        }

        // ????¨????sumR????¨??????????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        }
        // ????¨????sumC????¨??????????
        for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        }

        //??????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        }
        for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        }
        sb.append(sumX).append("\n");

        System.out.print(sb);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        String[] str1 = br.readLine().split(" ");
        int r = Integer.parseInt(str1[0]);
        int c = Integer.parseInt(str1[1]);
        int[][] data = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumX = 0;

        // r x c ????????????????¨??????\???????????????????´???????
        for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        }

        // ????¨????sumR????¨??????????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        }
        // ????¨????sumC????¨??????????
        for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        }

        //??????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        }
        for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        }
        sb.append(sumX).append("\n");

        System.out.print(sb);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringBuilder sb = new StringBuilder(); sb new StringBuilder() String[] str1 = br.readLine().split(" "); str1 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(str1[0]); r Integer.parseInt(str1[0]) Integer.parseInt Integer str1[0] str1 0 int c = Integer.parseInt(str1[1]); c Integer.parseInt(str1[1]) Integer.parseInt Integer str1[1] str1 1 int[][] data = new int[r][c]; data new int[r][c] r c int[] sumR = new int[r]; sumR new int[r] r int[] sumC = new int[c]; sumC new int[c] c int sumX = 0; sumX 0 for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        } int i=0; int i=0; i 0 i<r i r i++ i++ i {
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        } {
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        } String[] str2 = br.readLine().split(" "); str2 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            } int k=0; int k=0; k 0 k<c k c k++ k++ k {
                data[i][k] = Integer.parseInt(str2[k]);
            } {
                data[i][k] = Integer.parseInt(str2[k]);
            } data[i][k] = Integer.parseInt(str2[k]) data[i][k] data[i] data i k Integer.parseInt(str2[k]) Integer.parseInt Integer str2[k] str2 k for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        } int i=0; int i=0; i 0 i<r i r i++ i++ i {
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        } {
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        } for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            } int k=0; int k=0; k 0 k<c k c k++ k++ k {
                sumR[i] += data[i][k];
            } {
                sumR[i] += data[i][k];
            } sumR[i] += data[i][k] sumR[i] sumR i data[i][k] data[i] data i k sumX += sumR[i] sumX sumR[i] sumR i for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        } int i=0; int i=0; i 0 i<c i c i++ i++ i {
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        } {
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        } for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            } int k=0; int k=0; k 0 k<r k r k++ k++ k {
                sumC[i] += data[k][i];
            } {
                sumC[i] += data[k][i];
            } sumC[i] += data[k][i] sumC[i] sumC i data[k][i] data[k] data k i for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        } int i=0; int i=0; i 0 i<r i r i++ i++ i {
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        } {
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        } for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            } int k=0; int k=0; k 0 k<c k c k++ k++ k {
                sb.append(data[i][k]).append(" ");
            } {
                sb.append(data[i][k]).append(" ");
            } sb.append(data[i][k]).append(" ") sb.append(data[i][k]).append sb.append(data[i][k]) sb.append sb data[i][k] data[i] data i k " " sb.append(sumR[i]).append("\n") sb.append(sumR[i]).append sb.append(sumR[i]) sb.append sb sumR[i] sumR i "\n" for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        } int k=0; int k=0; k 0 k<c k c k++ k++ k {
            sb.append(sumC[k]).append(" ");
        } {
            sb.append(sumC[k]).append(" ");
        } sb.append(sumC[k]).append(" ") sb.append(sumC[k]).append sb.append(sumC[k]) sb.append sb sumC[k] sumC k " " sb.append(sumX).append("\n") sb.append(sumX).append sb.append(sumX) sb.append sb sumX "\n" System.out.print(sb) System.out.print System.out System System.out sb String[]args args
import java.io.*;

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

        String[] str1 = br.readLine().split(" ");
        int r = Integer.parseInt(str1[0]);
        int c = Integer.parseInt(str1[1]);
        int[][] data = new int[r][c];
        int[] sumR = new int[r];
        int[] sumC = new int[c];
        int sumX = 0;

        // r x c ????????????????¨??????\???????????????????´???????
        for(int i=0; i<r; i++){
            String[] str2 = br.readLine().split(" ");
            for(int k=0; k<c; k++){
                data[i][k] = Integer.parseInt(str2[k]);
            }
        }

        // ????¨????sumR????¨??????????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sumR[i] += data[i][k];
            }
            sumX += sumR[i];
        }
        // ????¨????sumC????¨??????????
        for(int i=0; i<c; i++){
            for(int k=0; k<r; k++){
                sumC[i] += data[k][i];
            }
        }

        //??????
        for(int i=0; i<r; i++){
            for(int k=0; k<c; k++){
                sb.append(data[i][k]).append(" ");
            }
            sb.append(sumR[i]).append("\n");
        }
        for(int k=0; k<c; k++){
            sb.append(sumC[k]).append(" ");
        }
        sb.append(sumX).append("\n");

        System.out.print(sb);
    }
}