import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    String INPUT = "";

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    }

    void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    }
}
 import java.io.ByteArrayInputStream; ByteArrayInputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {

    String INPUT = "";

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    }

    void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    }
} Main String INPUT = ""; INPUT "" public static void main(String[] args) throws Exception {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    } run {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    } Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes())); sc INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes())) INPUT.isEmpty() INPUT.isEmpty INPUT new Scanner(System.in) new Scanner(new ByteArrayInputStream(INPUT.getBytes())) PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) solve(sc, out) solve sc out out.flush() out.flush out void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    } solve {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    } int r r c; c r = sc.nextInt() r sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc int mat[][] = new int[r][c]; mat new int[r][c] r c for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[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++) {
				mat[i][j] = sc.nextInt();
			}
		} {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		} for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				mat[i][j] = sc.nextInt();
			} {
				mat[i][j] = sc.nextInt();
			} mat[i][j] = sc.nextInt() mat[i][j] mat[i] mat i j sc.nextInt() sc.nextInt sc for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		} {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		} int sum = 0; sum 0 for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			} {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			} System.out.print(mat[i][j] + " ") System.out.print System.out System System.out mat[i][j] + " " mat[i][j] mat[i] mat i j " " sum += mat[i][j] sum mat[i][j] mat[i] mat i j System.out.println(sum) System.out.println System.out System System.out sum for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		} {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		} int sum = 0; sum 0 for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
        		sum += mat[i][j];
			} {
        		sum += mat[i][j];
			} sum += mat[i][j] sum mat[i][j] mat[i] mat i j System.out.print(sum + " ") System.out.print System.out System System.out sum + " " sum " " int sum = 0; sum 0 for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		} {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		} for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				sum += mat[i][j];
			} {
				sum += mat[i][j];
			} sum += mat[i][j] sum mat[i][j] mat[i] mat i j System.out.println(sum) System.out.println System.out System System.out sum Scanner sc sc PrintWriter out out public class Main {

    String INPUT = "";

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    }

    void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    }
} public class Main {

    String INPUT = "";

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    }

    void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    }
} Main
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    String INPUT = "";

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    void run() {
        Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
        PrintWriter out = new PrintWriter(System.out);

        solve(sc, out);
        out.flush();
    }

    void solve(Scanner sc, PrintWriter out) {
        int r,c;
        
        r = sc.nextInt();
        c = sc.nextInt();
        
        int mat[][] = new int[r][c];
        
        for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				mat[i][j] = sc.nextInt();
			}
		}
	
		for (int i = 0; i < r; i++) {
        	int sum = 0;
			for (int j = 0; j < c; j++) {
				System.out.print(mat[i][j] + " ");
				sum += mat[i][j];
			}
			System.out.println(sum);
		}
		for (int j = 0; j < c; j++) {
        	int sum = 0;
        	for (int i = 0; i < r; i++) {
        		sum += mat[i][j];
			}
			System.out.print(sum + " ");
		}
		
		int sum = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum += mat[i][j];
			}
		}
		System.out.println(sum);
    }
}

