import java.util.Scanner;

public class Main{

	static Scanner s=new Scanner(System.in);

	public static void main(String __[]){
		input();
		solve();
	}

	static int r, c, sheet[][];

	private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	}

	private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	}
} import java.util.Scanner; Scanner java.util public class Main{

	static Scanner s=new Scanner(System.in);

	public static void main(String __[]){
		input();
		solve();
	}

	static int r, c, sheet[][];

	private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	}

	private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	}
} Main static Scanner s=new Scanner(System.in); s new Scanner(System.in) static int r r c c sheet[][]; sheet public static void main(String __[]){
		input();
		solve();
	} main {
		input();
		solve();
	} input() input solve() solve String __[] __ private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	} input {
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	} r=s.nextInt() r s.nextInt() s.nextInt s c=s.nextInt() c s.nextInt() s.nextInt s sheet=new int[r+1][c+1] sheet new int[r+1][c+1] r+1 r 1 c+1 c 1 private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	} solve {
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	} for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		} {
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		} int buf; buf for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			} {
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			} buf=s.nextInt() buf s.nextInt() s.nextInt s sheet[i][c]+=buf sheet[i][c] sheet[i] sheet i c buf sheet[r][j]+=buf sheet[r][j] sheet[r] sheet r j buf System.out.printf("%d ",buf) System.out.printf System.out System System.out "%d " buf System.out.println(sheet[i][c]) System.out.println System.out System System.out sheet[i][c] sheet[i] sheet i c for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		} int j=0; int j=0; j 0 j<c j c j++ j++ j {
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		} {
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		} sheet[r][c]+=sheet[r][j] sheet[r][c] sheet[r] sheet r c sheet[r][j] sheet[r] sheet r j System.out.printf("%d ",sheet[r][j]) System.out.printf System.out System System.out "%d " sheet[r][j] sheet[r] sheet r j System.out.println(sheet[r][c]) System.out.println System.out System System.out sheet[r][c] sheet[r] sheet r c public class Main{

	static Scanner s=new Scanner(System.in);

	public static void main(String __[]){
		input();
		solve();
	}

	static int r, c, sheet[][];

	private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	}

	private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	}
} public class Main{

	static Scanner s=new Scanner(System.in);

	public static void main(String __[]){
		input();
		solve();
	}

	static int r, c, sheet[][];

	private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	}

	private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	}
} Main
import java.util.Scanner;

public class Main{

	static Scanner s=new Scanner(System.in);

	public static void main(String __[]){
		input();
		solve();
	}

	static int r, c, sheet[][];

	private static void input(){
		r=s.nextInt();
		c=s.nextInt();
		sheet=new int[r+1][c+1];
	}

	private static void solve(){
		for(int i=0; i<r; i++){
			int buf;
			for(int j=0; j<c; j++){
				buf=s.nextInt();
				sheet[i][c]+=buf;
				sheet[r][j]+=buf;
				System.out.printf("%d ",buf);
			}
			System.out.println(sheet[i][c]);
		}
		for(int j=0; j<c; j++){
			sheet[r][c]+=sheet[r][j];
			System.out.printf("%d ",sheet[r][j]);
		}
		System.out.println(sheet[r][c]);
	}
}