import java.io.*;
import java.util.*;
 
class Main{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int r = Integer.parseInt(s[0]);
        int c = Integer.parseInt(s[1]);
        int[][] num = new int[r][c];
        String str;
        int i=0;
        int j=0;
        int sum1=0,sum2=0,sum3=0;
        while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        }
        for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        }
        for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        }
        System.out.println(sum3);
    }
} 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));
        String[] s = br.readLine().split(" ");
        int r = Integer.parseInt(s[0]);
        int c = Integer.parseInt(s[1]);
        int[][] num = new int[r][c];
        String str;
        int i=0;
        int j=0;
        int sum1=0,sum2=0,sum3=0;
        while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        }
        for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        }
        for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        }
        System.out.println(sum3);
    }
} Main public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int r = Integer.parseInt(s[0]);
        int c = Integer.parseInt(s[1]);
        int[][] num = new int[r][c];
        String str;
        int i=0;
        int j=0;
        int sum1=0,sum2=0,sum3=0;
        while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        }
        for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        }
        for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        }
        System.out.println(sum3);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int r = Integer.parseInt(s[0]);
        int c = Integer.parseInt(s[1]);
        int[][] num = new int[r][c];
        String str;
        int i=0;
        int j=0;
        int sum1=0,sum2=0,sum3=0;
        while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        }
        for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        }
        for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        }
        System.out.println(sum3);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s = br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int r = Integer.parseInt(s[0]); r Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int c = Integer.parseInt(s[1]); c Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 int[][] num = new int[r][c]; num new int[r][c] r c String str; str int i=0; i 0 int j=0; j 0 int sum1=0 sum1 0 sum2=0 sum2 0 sum3=0; sum3 0 while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        } (str=br.readLine())!=null (str=br.readLine()) str br.readLine() br.readLine br null {
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        } String[] sg = str.split(" "); sg str.split(" ") str.split str " " for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            } ; i<sg.length i sg.length sg sg.length i++ i++ i {
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            } {
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            } int a = Integer.parseInt(sg[i]); a Integer.parseInt(sg[i]) Integer.parseInt Integer sg[i] sg i num[j][i] = a num[j][i] num[j] num j i a i=0 i 0 j++ j for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        } int k=0; int k=0; k 0 k<r k r k++ k++ k {
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        } {
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        } for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            } int l=0; int l=0; l 0 l<c l c l++ l++ l {
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            } {
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            } sum1 += num[k][l] sum1 num[k][l] num[k] num k l System.out.print(num[k][l]+" ") System.out.print System.out System System.out num[k][l]+" " num[k][l] num[k] num k l " " System.out.println(sum1) System.out.println System.out System System.out sum1 sum1=0 sum1 0 for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        } int k=0; int k=0; k 0 k<c k c k++ k++ k {
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        } {
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        } for(int l=0; l<r; l++){
                sum2 += num[l][k];
            } int l=0; int l=0; l 0 l<r l r l++ l++ l {
                sum2 += num[l][k];
            } {
                sum2 += num[l][k];
            } sum2 += num[l][k] sum2 num[l][k] num[l] num l k System.out.print(sum2+ " ") System.out.print System.out System System.out sum2+ " " sum2 " " sum3 += sum2 sum3 sum2 sum2=0 sum2 0 System.out.println(sum3) System.out.println System.out System System.out sum3 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));
        String[] s = br.readLine().split(" ");
        int r = Integer.parseInt(s[0]);
        int c = Integer.parseInt(s[1]);
        int[][] num = new int[r][c];
        String str;
        int i=0;
        int j=0;
        int sum1=0,sum2=0,sum3=0;
        while((str=br.readLine())!=null){
            String[] sg = str.split(" ");
            for(; i<sg.length; i++){
                int a = Integer.parseInt(sg[i]);
                num[j][i] = a;
            }
            i=0;
            j++;
        }
        for(int k=0; k<r; k++){
            for(int l=0; l<c; l++){
                sum1 += num[k][l];
                System.out.print(num[k][l]+" ");
            }
            System.out.println(sum1);
            sum1=0;
        }
        for(int k=0; k<c; k++){
            for(int l=0; l<r; l++){
                sum2 += num[l][k];
            }
            System.out.print(sum2+ " ");
            sum3 += sum2;
            sum2=0;
        }
        System.out.println(sum3);
    }
}