#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n,a,sum,hoge,piyo;
    sum=0;
    hoge=0;
    piyo=0;
    while (cin>>n) {
        if (n==0) {
            break;
        } else {
            std::vector<vector<int>> vec(n+1,vector<int>(n+1));
            for (int i=0;i<n;i++) {
                for (int j=0;j<n;j++) {
                    cin>>a;
                    printf("%5d",a);
                    vec[i][j]=a;
                    sum=sum+a;
                    if (j==n-1) {
                        printf("%5d",sum);
                        cout<<endl;
                        sum=0;
                    }
                }
            }
            for (int k=0;k<n;k++) {
                for (int l=0;l<n;l++) {
                    hoge=hoge+vec[l][k];
                }
                printf("%5d",hoge);
                piyo=piyo+hoge;
                hoge=0;
            }
            printf("%5d",piyo);
            cout<<endl;
            piyo=0;
        }
    }
}

