import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.Collections;

public class Main {
    public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    }
} import java.util.HashMap; HashMap java.util import java.util.ArrayList; ArrayList java.util import java.util.Scanner; Scanner java.util import java.util.regex.Pattern; Pattern java.util.regex import java.util.Collections; Collections java.util public class Main {
    public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    }
} Main public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    } 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    } 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 < 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][j] ary[i][c] ary[i] ary i c ary[i][j] ary[i] ary i j for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        } int i = 0; int i = 0; i 0 i < c+1 i c+1 c 1 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[j][i] ary[r][i] ary[r] ary r i ary[j][i] ary[j] ary j i 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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        } 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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        } {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < c+1; j++) {
                sb.append(ary[i][j]).append(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        } 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 " " sb.delete(sb.length()-1, sb.length()) sb.delete sb sb.length()-1 sb.length() sb.length sb 1 sb.length() sb.length sb System.out.println(sb) System.out.println System.out System System.out sb String[] args args public class Main {
    public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    }
} public class Main {
    public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    }
} Main
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.Collections;

public class Main {
    public 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 < r; i++) {
            for (int j = 0; j < c; j++) {
                ary[i][c] += ary[i][j];
            }
        }

        for (int i = 0; i < c+1; i++) {
            for (int j = 0; j < r; j++) {
                ary[r][i] += ary[j][i];
            }
        }
        
        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(" ");
            }
            sb.delete(sb.length()-1, sb.length());
            System.out.println(sb);
        }

    }
}