import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	}
} Main public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	} main {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int r = stdIn.nextInt(); r stdIn.nextInt() stdIn.nextInt stdIn int c = stdIn.nextInt(); c stdIn.nextInt() stdIn.nextInt stdIn int[][] h = new int[r+1][c+1]; h new int[r+1][c+1] r+1 r 1 c+1 c 1 for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		} int i = 0; int i = 0; i 0 i < r i r i++ i++ i {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		} {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		} int rsum = 0; rsum 0 for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			} {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			} int tmp = stdIn.nextInt(); tmp stdIn.nextInt() stdIn.nextInt stdIn rsum += tmp rsum tmp h[i][j] = tmp h[i][j] h[i] h i j tmp h[i][c] = rsum h[i][c] h[i] h i c rsum for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		} int i = 0; int i = 0; i 0 i < c+1 i c+1 c 1 i++ i++ i {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		} {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		} int rsum = 0; rsum 0 for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			} int j = 0; int j = 0; j 0 j < r j r j++ j++ j {
				rsum += h[j][i];
			} {
				rsum += h[j][i];
			} rsum += h[j][i] rsum h[j][i] h[j] h j i h[r][i] = rsum h[r][i] h[r] h r i rsum for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		} int i = 0; int i = 0; i 0 i < r+1 i r+1 r 1 i++ i++ i {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		} {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		} for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			} int j = 0; int j = 0; j 0 j < c j c j++ j++ j {
				System.out.print(h[i][j] + " ");
			} {
				System.out.print(h[i][j] + " ");
			} System.out.print(h[i][j] + " ") System.out.print System.out System System.out h[i][j] + " " h[i][j] h[i] h i j " " System.out.println(h[i][c]) System.out.println System.out System System.out h[i][c] h[i] h i c String[] args args public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		int[][] h = new int[r+1][c+1];
		
		for(int i = 0; i < r; i++) {
			int rsum = 0;
			for(int j = 0; j < c; j++) {
				int tmp = stdIn.nextInt();
				rsum += tmp;
				h[i][j] = tmp;
			}
			h[i][c] = rsum;
		}
		
		for(int i = 0; i < c+1; i++) {
			int rsum = 0;
			for(int j = 0; j < r; j++) {
				rsum += h[j][i];
			}
			h[r][i] = rsum;
		}
		
		for(int i = 0; i < r+1; i++) {
			for(int j = 0; j < c; j++) {
				System.out.print(h[i][j] + " ");
			}
			System.out.println(h[i][c]);
		}
	}
}