import java.util.*;

public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    }
    
    private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    }
    
    private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    }
} import java.util.*; util.* java public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    }
    
    private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    }
    
    private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    }
} Main private static final Scanner scn = new Scanner(System.in); scn new Scanner(System.in) public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    } main {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    } int r = scn.nextInt() r scn.nextInt() scn.nextInt scn c = scn.nextInt(); c scn.nextInt() scn.nextInt scn showTable(createTable(r, c)) showTable createTable(r, c) createTable r c String[] args args private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    } createTable {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    } int[][] tbl = new int[r + 1][c + 1]; tbl 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++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        } {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        } for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            } {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            } int v = scn.nextInt(); v scn.nextInt() scn.nextInt scn tbl[i][j] += v tbl[i][j] tbl[i] tbl i j v tbl[i][c] += v tbl[i][c] tbl[i] tbl i c v tbl[r][j] += v tbl[r][j] tbl[r] tbl r j v tbl[r][c] += v tbl[r][c] tbl[r] tbl r c v return tbl; tbl int r r int c c private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    } showTable {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    } for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        } int[] ary tbl {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        } {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        } StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(int elm : ary) {
                sb.append(elm + " ");
            } int elm ary {
                sb.append(elm + " ");
            } {
                sb.append(elm + " ");
            } sb.append(elm + " ") sb.append sb elm + " " elm " " System.out.println(sb.toString().trim()) System.out.println System.out System System.out sb.toString().trim() sb.toString().trim sb.toString() sb.toString sb int[][] tbl tbl public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    }
    
    private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    }
    
    private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    }
} public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    }
    
    private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    }
    
    private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    }
} Main
import java.util.*;

public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int r = scn.nextInt(), c = scn.nextInt();
        showTable(createTable(r, c));
    }
    
    private static int[][] createTable(int r, int c) {
        int[][] tbl = new int[r + 1][c + 1];
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                int v = scn.nextInt();
                tbl[i][j] += v;
                tbl[i][c] += v;
                tbl[r][j] += v;
                tbl[r][c] += v;
            }
        }
        return tbl;
    }
    
    private static void showTable(int[][] tbl) {
        for(int[] ary : tbl) {
            StringBuilder sb = new StringBuilder();
            for(int elm : ary) {
                sb.append(elm + " ");
            }
            System.out.println(sb.toString().trim());
        }
    }
}