import java.util.Scanner;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		}
}




//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Scanner;
//
//public class Main {
//	public static void main(String[] args) {
//		Scanner scan = new Scanner(System.in);
//		List<Integer> n = new ArrayList<Integer>();
//		List<Integer> x = new ArrayList<Integer>();
//		List<Integer> count = new ArrayList<Integer>();
//
//		 for(int i=0;;i++){
//			 n.add(i, scan.nextInt());
//			 x.add(i, scan.nextInt());
//			 count.add(i, 0);
//			 if(n.get(i)==0 && x.get(i)==0) break;
//		 }
//
//
//
//   for(int h=0;;h++){
//		 if(n.get(h)==0 && x.get(h)==0) break;
//		 for(int i=1;i<=n.get(h);i++){
//			 for(int j=i;j<=n.get(h);j++){
//				 if(i==j)continue;
//				 for(int k=j;k <= n.get(h);k++){
//					 if(i==k || j==k)continue;
//					 if(i+j+k == x.get(h)) count.set(h, count.get(h)+1 );
//				 }
//			 }
//		 }
//
//			System.out.println(count.get(h));
//   }
//	}
//
//}
//
// import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] a = new int[r][c+1];
		int[] sum = new int[c+1];
		
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] a = new int[r][c+1];
		int[] sum = new int[c+1];
		
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		} main {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] a = new int[r][c+1];
		int[] sum = new int[c+1];
		
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int r = scan.nextInt(); r scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int[][] a = new int[r][c+1]; a new int[r][c+1] r c+1 c 1 int[] sum = new int[c+1]; sum new int[c+1] c+1 c 1 for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		} {
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		} for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				a[i][j] = scan.nextInt();
			} {
				a[i][j] = scan.nextInt();
			} a[i][j] = scan.nextInt() a[i][j] a[i] a i j scan.nextInt() scan.nextInt scan for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		} {
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		} for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				a[i][c]= a[i][c]+a[i][j];
			} {
				a[i][c]= a[i][c]+a[i][j];
			} a[i][c]= a[i][c]+a[i][j] a[i][c] a[i] a i c a[i][c]+a[i][j] a[i][c] a[i] a i c a[i][j] a[i] a i j for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[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++){
				sum[i]= sum[i]+a[j][i];
			}
		} {
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		} for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			} int j=0; int j=0; j 0 j<r j r j++ j++ j {
				sum[i]= sum[i]+a[j][i];
			} {
				sum[i]= sum[i]+a[j][i];
			} sum[i]= sum[i]+a[j][i] sum[i] sum i sum[i]+a[j][i] sum[i] sum i a[j][i] a[j] a j i for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		} {
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		} for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			} {
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			} if(j!=0)System.out.print(" "); j!=0 j 0 System.out.print(" ") System.out.print System.out System System.out " " System.out.print(a[i][j]) System.out.print System.out System System.out a[i][j] a[i] a i j System.out.println() System.out.println System.out System System.out for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			} int j=0; int j=0; j 0 j<c+1 j c+1 c 1 j++ j++ j {
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			} {
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			} if(j!=0)System.out.print(" "); j!=0 j 0 System.out.print(" ") System.out.print System.out System System.out " " System.out.print(sum[j]) System.out.print System.out System System.out sum[j] sum j System.out.println() System.out.println System.out System System.out String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] a = new int[r][c+1];
		int[] sum = new int[c+1];
		
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] a = new int[r][c+1];
		int[] sum = new int[c+1];
		
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][j] = scan.nextInt();
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		}
} Main
import java.util.Scanner;

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

		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				a[i][c]= a[i][c]+a[i][j];
			}
		}
		
		for(int i=0;i<c+1;i++){
			for(int j=0;j<r;j++){
				sum[i]= sum[i]+a[j][i];
			}
		}

		for(int i=0;i<r;i++){
			for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(a[i][j]);
			}
			    System.out.println();
		}
		for(int j=0;j<c+1;j++){
				if(j!=0)System.out.print(" ");
				System.out.print(sum[j]);
			}
			    System.out.println();
		}
}




//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Scanner;
//
//public class Main {
//	public static void main(String[] args) {
//		Scanner scan = new Scanner(System.in);
//		List<Integer> n = new ArrayList<Integer>();
//		List<Integer> x = new ArrayList<Integer>();
//		List<Integer> count = new ArrayList<Integer>();
//
//		 for(int i=0;;i++){
//			 n.add(i, scan.nextInt());
//			 x.add(i, scan.nextInt());
//			 count.add(i, 0);
//			 if(n.get(i)==0 && x.get(i)==0) break;
//		 }
//
//
//
//   for(int h=0;;h++){
//		 if(n.get(h)==0 && x.get(h)==0) break;
//		 for(int i=1;i<=n.get(h);i++){
//			 for(int j=i;j<=n.get(h);j++){
//				 if(i==j)continue;
//				 for(int k=j;k <= n.get(h);k++){
//					 if(i==k || j==k)continue;
//					 if(i+j+k == x.get(h)) count.set(h, count.get(h)+1 );
//				 }
//			 }
//		 }
//
//			System.out.println(count.get(h));
//   }
//	}
//
//}
//
//