import java.util.*;
public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    }
} import java.util.*; util.* java public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    }
} Main public static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    } main {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    } int i i j; j int sum = 0 sum 0 sum2 = 0; sum2 0 int r = sc.nextInt(); r sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int rc[][] = new int[r][c]; rc new int[r][c] r c for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	} i = 0; i = 0 i 0 i < r i r i++ i++ i {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	} {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	} for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		} j = 0; j = 0 j 0 j < c j c j++ j++ j {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		} {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		} rc[i][j] = sc.nextInt() rc[i][j] rc[i] rc i j sc.nextInt() sc.nextInt sc sum += rc[i][j] sum rc[i][j] rc[i] rc i j System.out.print(rc[i][j] + " ") System.out.print System.out System System.out rc[i][j] + " " rc[i][j] rc[i] rc i j " " System.out.println(sum) System.out.println System.out System System.out sum sum = 0 sum 0 for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	} j = 0; j = 0 j 0 j < c j c j++ j++ j {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	} {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	} for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		} i = 0; i = 0 i 0 i < r i r i++ i++ i {
    			sum += rc[i][j];
    		} {
    			sum += rc[i][j];
    		} sum += rc[i][j] sum rc[i][j] rc[i] rc i j System.out.print(sum + " ") System.out.print System.out System System.out sum + " " sum " " sum2 += sum sum2 sum sum = 0 sum 0 System.out.println(sum2) System.out.println System.out System System.out sum2 String[] args args public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    }
} public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    }
} Main
import java.util.*;
public class Main {
    public static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
    	int i, j;
    	int sum = 0, sum2 = 0;
    	int r = sc.nextInt();
    	int c = sc.nextInt();
    	int rc[][] = new int[r][c];
    		
    	for(i = 0; i < r; i++) {
    		for(j = 0; j < c; j++) {
    			rc[i][j] = sc.nextInt();
    			sum += rc[i][j];
    			System.out.print(rc[i][j] + " ");
    		}
    		System.out.println(sum);
    		sum = 0;
    	}
    	for(j = 0; j < c; j++) {
    		for(i = 0; i < r; i++) {
    			sum += rc[i][j];
    		}
    		System.out.print(sum + " ");
    		sum2 += sum;
    		sum = 0;
    	}
    	System.out.println(sum2);
    }
}
