import java.util.*;

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

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