import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
}
 import java.util.*; util.* java public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
} Main private static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    } main {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    } int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int[][] ary = new int[r+1][c+1]; ary 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++) {
                ary[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++) {
                ary[i][j] = sc.nextInt();
            }
        } {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        } for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            } int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
                ary[i][j] = sc.nextInt();
            } {
                ary[i][j] = sc.nextInt();
            } ary[i][j] = sc.nextInt() ary[i][j] ary[i] ary i j sc.nextInt() sc.nextInt sc for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        } int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        } {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        } for (int j = 0;j < r;j++) ary[r][i] += ary[j][i]; int j = 0; int j = 0; j 0 j < r j r j++ j++ j ary[r][i] += ary[j][i]; ary[r][i] += ary[j][i] ary[r][i] ary[r] ary r i ary[j][i] ary[j] ary j i for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        } int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        } {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        } for (int j = 0;j < c;j++) ary[i][c] += ary[i][j]; int j = 0; int j = 0; j 0 j < c j c j++ j++ j ary[i][c] += ary[i][j]; ary[i][c] += ary[i][j] ary[i][c] ary[i] ary i c ary[i][j] ary[i] ary i j for (int i = 0;i < r;i++) ary[r][c] += ary[i][c]; int i = 0; int i = 0; i 0 i < r i r i++ i++ i ary[r][c] += ary[i][c]; ary[r][c] += ary[i][c] ary[r][c] ary[r] ary r c ary[i][c] ary[i] ary i c for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        } int i = 0; int i = 0; i 0 i < r+1 i r+1 r 1 i++ i++ i {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        } {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        } StringBuilder sb = new StringBuilder(); sb new StringBuilder() for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            } int j = 0; int j = 0; j 0 j < c+1 j c+1 c 1 j++ j++ j {
                sb.append(ary[i][j]).append(" ");
            } {
                sb.append(ary[i][j]).append(" ");
            } sb.append(ary[i][j]).append(" ") sb.append(ary[i][j]).append sb.append(ary[i][j]) sb.append sb ary[i][j] ary[i] ary i j " " System.out.println(sb.substring(0, sb.length()-1)) System.out.println System.out System System.out sb.substring(0, sb.length()-1) sb.substring sb 0 sb.length()-1 sb.length() sb.length sb 1 String[] args args public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
} public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
} Main
import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] ary = new int[r+1][c+1];
        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) {
                ary[i][j] = sc.nextInt();
            }
        }
        
        for (int i = 0;i < c;i++) {
            for (int j = 0;j < r;j++) ary[r][i] += ary[j][i];
        }

        for (int i = 0;i < r;i++) {
            for (int j = 0;j < c;j++) ary[i][c] += ary[i][j];
        }
        
        for (int i = 0;i < r;i++) ary[r][c] += ary[i][c];
        
        for (int i = 0;i < r+1;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < c+1;j++) {
                sb.append(ary[i][j]).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
}

