import java.io.BufferedReader;
import java.io.IOException;
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();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException 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();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }
} Main public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    } main {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String input = br.readLine(); input br.readLine() br.readLine br String[] strArray = input.split(" "); strArray input.split(" ") input.split input " " int r = Integer.parseInt(strArray[0]); r Integer.parseInt(strArray[0]) Integer.parseInt Integer strArray[0] strArray 0 int c = Integer.parseInt(strArray[1]); c Integer.parseInt(strArray[1]) Integer.parseInt Integer strArray[1] strArray 1 int[][] table = new int[r+1][c+1]; table new int[r+1][c+1] r+1 r 1 c+1 c 1 for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        } {
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        } input = br.readLine() input br.readLine() br.readLine br strArray = input.split(" ") strArray input.split(" ") input.split input " " for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                table[i][j] = Integer.parseInt(strArray[j]);
            } {
                table[i][j] = Integer.parseInt(strArray[j]);
            } table[i][j] = Integer.parseInt(strArray[j]) table[i][j] table[i] table i j Integer.parseInt(strArray[j]) Integer.parseInt Integer strArray[j] strArray j for(int i = 0; i < r; i++){
            int sum = 0;

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

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

            for(int j = 0; j < c; j++){
                sum += table[i][j];
            }
            table[i][c] = sum;
        } int sum = 0; sum 0 for(int j = 0; j < c; j++){
                sum += table[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                sum += table[i][j];
            } {
                sum += table[i][j];
            } sum += table[i][j] sum table[i][j] table[i] table i j table[i][c] = sum table[i][c] table[i] table i c sum for(int i = 0; i < c+1; i++){
            int sum = 0;

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

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

            for(int j = 0; j < r; j++){
                sum += table[j][i];
            }
            table[r][i] = sum;
        } int sum = 0; sum 0 for(int j = 0; j < r; j++){
                sum += table[j][i];
            } int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
                sum += table[j][i];
            } {
                sum += table[j][i];
            } sum += table[j][i] sum table[j][i] table[j] table j i table[r][i] = sum table[r][i] table[r] table r i sum for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        } 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+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        } {

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        } for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            } int j = 0; int j = 0; j 0 j < c+1 j c+1 c 1 j++ j++ j {
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            } {
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            } System.out.print(table[i][j]) System.out.print System.out System System.out table[i][j] table[i] table i j if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                } j != c j c {
                    System.out.print(" ");
                } System.out.print(" ") System.out.print System.out System System.out " " {
                    System.out.print("\n");
                } System.out.print("\n") System.out.print System.out System System.out "\n" 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();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }
} public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
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();
        String[] strArray = input.split(" ");

        int r = Integer.parseInt(strArray[0]);  //???r
        int c = Integer.parseInt(strArray[1]);  //???c

        int[][] table = new int[r+1][c+1];

//      ??????A???????????????
        for(int i = 0; i < r; i++){
            input = br.readLine();
            strArray = input.split(" ");

            for(int j = 0; j < c; j++){
                table[i][j] = Integer.parseInt(strArray[j]);
            }
        }

//      ???????????????????????¨??????????????????????¨????????±???????
        for(int i = 0; i < r; i++){
            int sum = 0;

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

//      ???????????????????????¨??????????????????????¨????????±???????
//      ?????????????¨??????§?????????????????????????¨??????????????????§???
//      i????????????c+1??¨?????????
        for(int i = 0; i < c+1; i++){
            int sum = 0;

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

//      (r+1) ?? (c+1) ?????°????????¨?????????
        for(int i = 0; i < r+1; i++){

            for(int j = 0; j < c+1; j++){
                System.out.print(table[i][j]);

                if(j != c){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }
}