
import java.util.Arrays;
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[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	}
} import java.util.Arrays; Arrays java.util 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[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	}
} Main public static void main(String[] args)
	{
		Scanner stdIn = new Scanner(System.in);
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		int[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	} main {
		Scanner stdIn = new Scanner(System.in);
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		int[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	} 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[][] number = new int[r][c]; number new int[r][c] r c int[] count_x = new int[r]; count_x new int[r] r int[] count_y = new int[c]; count_y new int[c] c int count = 0; count 0 Arrays.fill(count_x,0) Arrays.fill Arrays count_x 0 Arrays.fill(count_y,0) Arrays.fill Arrays count_y 0 for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		} {
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		} for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			} {
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			} number[i][j] = stdIn.nextInt() number[i][j] number[i] number i j stdIn.nextInt() stdIn.nextInt stdIn count_x[i]+=number[i][j] count_x[i] count_x i number[i][j] number[i] number i j count_y[j]+=number[i][j] count_y[j] count_y j number[i][j] number[i] number i j count += number[i][j] count number[i][j] number[i] number i j for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		} int i=0; int i=0; i 0 i<r i r i++ i++ i {
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		} {
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		} for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			} int j=0; int j=0; j 0 j<c j c j++ j++ j {
				System.out.print(number[i][j]+" ");
			} {
				System.out.print(number[i][j]+" ");
			} System.out.print(number[i][j]+" ") System.out.print System.out System System.out number[i][j]+" " number[i][j] number[i] number i j " " System.out.println(count_x[i]) System.out.println System.out System System.out count_x[i] count_x i for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		} int i=0; int i=0; i 0 i<c i c i++ i++ i {
			System.out.print(count_y[i]+" ");
		} {
			System.out.print(count_y[i]+" ");
		} System.out.print(count_y[i]+" ") System.out.print System.out System System.out count_y[i]+" " count_y[i] count_y i " " System.out.println(count) System.out.println System.out System System.out count 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[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	}
} public class Main {
	public static void main(String[] args)
	{
		Scanner stdIn = new Scanner(System.in);
		int r = stdIn.nextInt();
		int c = stdIn.nextInt();
		int[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	}
} Main

import java.util.Arrays;
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[][] number = new int[r][c];
		int[] count_x = new int[r];
		int[] count_y = new int[c];
		int count = 0;
		
		Arrays.fill(count_x,0);
		Arrays.fill(count_y,0);
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				number[i][j] = stdIn.nextInt();
				count_x[i]+=number[i][j];
				count_y[j]+=number[i][j];
				count += number[i][j];
			}
		}
		
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				System.out.print(number[i][j]+" ");
			}
			System.out.println(count_x[i]);
		}
		
		for(int i=0;i<c;i++)
		{
			System.out.print(count_y[i]+" ");
		}
		
		System.out.println(count);
		
	}
}