#include <iostream>
#include <iomanip>
using namespace std;

int n;

int solve(int n){
        int format=0,table[1000][1000]={};
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                        cin >> table[i][j];
                }
        }
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                        table[n][j] += table[i][j];
                        table[i][n] += table[i][j];
                }
        }
        for(int i=0;i<n;i++){
                table[n][n] += table[i][n];
                table[n][n] += table[n][i];
        }
        table[n][n] /= 2;
        if(table[n][n] < 10){
                format=1;
        }else if(table[n][n] < 100){
                format=2;
        }else if(table[n][n] < 1000){
                format=3;
        }else if(table[n][n] < 10000){
                format=4;
        }
        for(int i=0;i<n+1;i++){
                for(int j=0;j<n+1;j++){
                        cout << setw(format) << table[i][j];
                        cout << " ";
                }
                cout << "" << endl;
        }
}

int main(){
        while(1){
                cin >> n;
                if(n == 0){
                        break;
                }
                solve(n);
        }
}