Problem:
Spreadsheet

Your task is to perform a simple table calculation.

Write a program which reads the number of rows r, columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column.

Input

In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters.

Output

Print the new table of (r+1) × (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table.

Constraints

1 ≤ r, c ≤ 100

0 ≤ an element of the table ≤ 100

Sample Input

4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

Sample Output

1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56

Solution in JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO 自動生成されたメソッド・スタブ
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

		// 標準入力の値を、半角スペースで分割
		String[] inputRowAndCol = (input.readLine()).split("\\s");
		int row = Integer.parseInt(inputRowAndCol[0]); // 行
		int col = Integer.parseInt(inputRowAndCol[1]); // 列

		// 表
		int[][] matrixA = new int[row +1][col +1];
		// 縦の合計用
		int[] colSum = new int[col +1];
		// 横の合計用
		int[] rowSum = new int[row +1];
		// 入力された、1行の数字を格納する用
		String[] number = null;

		// 表の行数だけ繰り返し
		for (int i = 0; i < matrixA.length; i++) {
			// 最終行以外は、入力された行の数字を処理
			if (i != matrixA.length -1) {
				number = (input.readLine()).split("\\s");
			}
			// 表の列数だけ繰り返し
			for (int j = 0; j < matrixA[i].length -1; j++) {
				// 最終行以外は、合計していく
				if (i != matrixA.length -1) {
				  rowSum[i] += Integer.parseInt(number[j]);
				  colSum[j] += Integer.parseInt(number[j]);
				}
				// 最終行の行の合計
				if (i == matrixA.length -1 && j == matrixA[i].length -2) {
					for (int m = 0; m < rowSum.length; m++) {
						matrixA[i][j +1] += rowSum[m];
					}
			    // 最終行以外の行の合計
				} else if (j == matrixA[i].length -2) {
					matrixA[i][j +1] = rowSum[i];

				}
				// 列の合計
				if (i == matrixA.length -1) {
					matrixA[i][j] = colSum[j];
				// 表に値を入れていく
				} else {
					matrixA[i][j] = Integer.parseInt(number[j]);
				}

			}
		}

		for (int k = 0; k < matrixA.length; k++) {
			for (int l = 0; l < matrixA[k].length; l++) {
				System.out.print(matrixA[k][l]);
				if (l != matrixA[k].length -1) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}


	}

}