import java.util.*;
import java.text.*;

class Main {
    public static void main(String[]args)throws Exception {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int c = sc.nextInt();
        int [][] a = new int[r][c];
        int [] rs = new int[c];
        int [] cs = new int[r];
        int s = 0;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        }
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        }
        for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        }
        System.out.println(s);
    }
} import java.util.*; util.* java import java.text.*; text.* java class Main {
    public static void main(String[]args)throws Exception {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int c = sc.nextInt();
        int [][] a = new int[r][c];
        int [] rs = new int[c];
        int [] cs = new int[r];
        int s = 0;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        }
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        }
        for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        }
        System.out.println(s);
    }
} Main public static void main(String[]args)throws Exception {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int c = sc.nextInt();
        int [][] a = new int[r][c];
        int [] rs = new int[c];
        int [] cs = new int[r];
        int s = 0;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        }
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        }
        for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        }
        System.out.println(s);
    } main {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int c = sc.nextInt();
        int [][] a = new int[r][c];
        int [] rs = new int[c];
        int [] cs = new int[r];
        int s = 0;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        }
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        }
        for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        }
        System.out.println(s);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int [][] a = new int[r][c]; a new int[r][c] r c int [] rs = new int[c]; rs new int[c] c int [] cs = new int[r]; cs new int[r] r int s = 0; s 0 for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        } {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        } for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            } {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            } a[i][j] = sc.nextInt() a[i][j] a[i] a i j sc.nextInt() sc.nextInt sc cs[i] += a[i][j] cs[i] cs i a[i][j] a[i] a i j rs[j] += a[i][j] rs[j] rs j a[i][j] a[i] a i j s += a[i][j] s a[i][j] a[i] a i j for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        } {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        } for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                System.out.print("" + a[i][j] + " ");
            } {
                System.out.print("" + a[i][j] + " ");
            } System.out.print("" + a[i][j] + " ") System.out.print System.out System System.out "" + a[i][j] + " " "" + a[i][j] + " " "" a[i][j] a[i] a i j " " System.out.println(cs[i]) System.out.println System.out System System.out cs[i] cs i for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
            System.out.print("" + rs[j] + " ");
        } {
            System.out.print("" + rs[j] + " ");
        } System.out.print("" + rs[j] + " ") System.out.print System.out System System.out "" + rs[j] + " " "" + rs[j] + " " "" rs[j] rs j " " System.out.println(s) System.out.println System.out System System.out s String[]args args
import java.util.*;
import java.text.*;

class Main {
    public static void main(String[]args)throws Exception {
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int c = sc.nextInt();
        int [][] a = new int[r][c];
        int [] rs = new int[c];
        int [] cs = new int[r];
        int s = 0;
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
                cs[i] += a[i][j];
                rs[j] += a[i][j];
                s += a[i][j];
            }
        }
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                System.out.print("" + a[i][j] + " ");
            }
            System.out.println(cs[i]);
        }
        for(int j = 0; j < c; j++) {
            System.out.print("" + rs[j] + " ");
        }
        System.out.println(s);
    }
}