Problem:
Matrix-like Computation

Your task is to develop a tiny little part of spreadsheet software.

Write a program which adds up columns and rows of given table as shown in the following figure:

Input

The input consists of several datasets. Each dataset consists of:

n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
nth row of the table

The input ends with a line consisting of a single 0.

Output

For each dataset, print the table with sums of rows and columns. Each item of the table should be aligned to the right with a margin for five digits. Please see the sample output for details.

Sample Input

4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0

Output for the Sample Input

52 96 15 20 183
86 22 35 45 188
45 78 54 36 213
16 86 74 55 231
199 282 178 156 815
52 96 15 20 183
86 22 35 45 188
45 78 54 36 213
16 86 74 55 231
199 282 178 156 815

Solution in JAVA (judge verdict: Wrong Answer)


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

public class Main {

    public static String _5keta(int n){
        if(n/10000>1){return ""+n;}
        if(n/1000>1){return " "+n;}
        if(n/100>1){return "  "+n;}
        if(n/10>1){return "   "+n;}
        return "    "+n;
    }
    public static void main(String[] args)  {

    	try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while((line=br.readLine())!=null){if(line.isEmpty())break;
	        	String[] spl = line.split(" ");
	            int n = Integer.parseInt(spl[0]);
	            if(n==0){break;}
                    int[][] matrix = new int[n][n];
                    for(int i=0; i<n;i++){
                        line=br.readLine();
                        spl=line.split(" ");
                        for(int j=0; j<n;j++){
                            matrix[i][j]=Integer.parseInt(spl[j]);   
                        }
                    }
                    for(int i=0; i<n;i++){
                        int sum=0;
                        for(int j=0;j<n;j++){
                            sum+=matrix[i][j];
                            System.out.print(_5keta(matrix[i][j]));
                        }
                        System.out.println(_5keta(sum));
                    }
                    for(int j=0; j<n;j++){
                        int sum=0;
                        for(int i=0; i<n; i++){
                            sum+=matrix[i][j];
                        }
                        System.out.print(_5keta(sum));
                    }
                    System.out.println("");
            }//End WHILE


        }catch(Exception e){e.printStackTrace();}

    }


}