import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    } main {
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    } 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[][] su = new int[r+1][c+1]; su new int[r+1][c+1] r+1 r 1 c+1 c 1 for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        } {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        } for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                su[i][j] = sc.nextInt();
            } {
                su[i][j] = sc.nextInt();
            } su[i][j] = sc.nextInt() su[i][j] su[i] su i j sc.nextInt() sc.nextInt sc for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        } {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        } int tmp = 0; tmp 0 for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            } int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            } {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            } if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                } j != c j c {
                    tmp += su[i][j];        
                } tmp += su[i][j] tmp su[i][j] su[i] su i j {
                    su[i][j] = tmp;
                } su[i][j] = tmp su[i][j] su[i] su i j tmp for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        } int i = 0; int i = 0; i 0 i < c + 1 i c + 1 c 1 i++ i++ i {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        } {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        } int tmp = 0; tmp 0 for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            } int j = 0; int j = 0; j 0 j < r + 1 j r + 1 r 1 j++ j++ j {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            } {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            } if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                } j != r j r {
                    tmp += su[j][i];
                } tmp += su[j][i] tmp su[j][i] su[j] su j i {
                    su[j][i] = tmp;
                } su[j][i] = tmp su[j][i] su[j] su j i tmp for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        } 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++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        } {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        } for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            } int j = 0; int j = 0; j 0 j < c + 1 j c + 1 c 1 j++ j++ j {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            } {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            } if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                } j != c j c {
                    System.out.printf("%d ", su[i][j]);
                } System.out.printf("%d ", su[i][j]) System.out.printf System.out System System.out "%d " su[i][j] su[i] su i j {
                    System.out.printf("%d\n", su[i][j]);
                } System.out.printf("%d\n", su[i][j]) System.out.printf System.out System System.out "%d\n" su[i][j] su[i] su i j String[] args args public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    }
} Main
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt();
        int c = sc.nextInt();
        
        int[][] su = new int[r+1][c+1];
        
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                su[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0; i < r; i++) {
            int tmp = 0;
            
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    tmp += su[i][j];        
                }
                else {
                    su[i][j] = tmp;
                }
            }
        }
        
        for (int i = 0; i < c + 1; i++) {
            int tmp = 0;
            
            for (int j = 0; j < r + 1; j++) {
                if (j != r) {
                    tmp += su[j][i];
                }
                else {
                    su[j][i] = tmp;
                }
            }
        }
        
        for (int i = 0; i < r + 1; i++) {
            for (int j = 0; j < c + 1; j++) {
                if (j != c) {
                    System.out.printf("%d ", su[i][j]);
                }
                else {
                    System.out.printf("%d\n", su[i][j]);
                }
            }
        }
    }
}
