import java.util.*;
import java.lang.StringBuilder;
public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	}
} import java.util.*; util.* java import java.lang.StringBuilder; StringBuilder java.lang public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	}
} Main public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	} main {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) StringBuilder sb = new StringBuilder(); sb new StringBuilder() int c = 0; c 0 String[] st = sc.nextLine().split(" "); st sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " int h = Integer.parseInt(st[0]); h Integer.parseInt(st[0]) Integer.parseInt Integer st[0] st 0 int w = Integer.parseInt(st[1]); w Integer.parseInt(st[1]) Integer.parseInt Integer st[1] st 1 int[] t = new int[h]; t new int[h] h int[][] n = new int [h][w]; n new int [h][w] h w for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		} int i = 0; int i = 0; i 0 i < h i h i++ i++ i {
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		} {
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		} st = sc.nextLine().split(" ") st sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			} int k = 0; int k = 0; k 0 k < w k w k++ k++ k {
				n[i][k] = Integer.parseInt(st[k]);
			} {
				n[i][k] = Integer.parseInt(st[k]);
			} n[i][k] = Integer.parseInt(st[k]) n[i][k] n[i] n i k Integer.parseInt(st[k]) Integer.parseInt Integer st[k] st k for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		} int i = 0; int i = 0; i 0 i < h i h i++ i++ i {
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		} {
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		} for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			} int k = 0; int k = 0; k 0 k < w k w k++ k++ k {
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			} {
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			} sb.append(n[i][k]+" ") sb.append sb n[i][k]+" " n[i][k] n[i] n i k " " t[i] = c += n[i][k] t[i] t i c += n[i][k] c n[i][k] n[i] n i k sb.append(c+"\n") sb.append sb c+"\n" c "\n" t[i] = c t[i] t i c c = 0 c 0 for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		} int i = 0; int i = 0; i 0 i < w i w i++ i++ i {
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		} {
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		} for(int k = 0;k < h;k++){
				c += n[k][i];
			} int k = 0; int k = 0; k 0 k < h k h k++ k++ k {
				c += n[k][i];
			} {
				c += n[k][i];
			} c += n[k][i] c n[k][i] n[k] n k i sb.append(c+" ") sb.append sb c+" " c " " c = 0 c 0 for(int i = 0;i < h;i++){
			c += t[i];
		} int i = 0; int i = 0; i 0 i < h i h i++ i++ i {
			c += t[i];
		} {
			c += t[i];
		} c += t[i] c t[i] t i sb.append(c) sb.append sb c System.out.println(sb) System.out.println System.out System System.out sb String[] args args public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	}
} public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	}
} Main
import java.util.*;
import java.lang.StringBuilder;
public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int c = 0;
		String[] st = sc.nextLine().split(" ");
		int h = Integer.parseInt(st[0]);
		int w = Integer.parseInt(st[1]);
		int[] t = new int[h];
		int[][] n = new int [h][w];
		for(int i = 0;i < h;i++){
			st = sc.nextLine().split(" ");
			for(int k = 0;k < w;k++){
				n[i][k] = Integer.parseInt(st[k]);
			}
		}
		for(int i = 0;i < h;i++){
			for(int k = 0;k < w;k++){
				sb.append(n[i][k]+" ");
				t[i] = c += n[i][k];
			}
			sb.append(c+"\n");
			t[i] = c;
			c = 0;
		}
		for(int i = 0;i < w;i++){
			for(int k = 0;k < h;k++){
				c += n[k][i];
			}
			sb.append(c+" ");
			c = 0;
		}
		for(int i = 0;i < h;i++){
			c += t[i];
		}
		sb.append(c);
		System.out.println(sb);
	}
}