#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n = 0, tmp0 = 0, tmp1 = 0;
  int tmp2 = 0, tmp3, out1 = 0, out2 = 0;
  int a[100];
  scanf("%d", &n);
  while (n) {
    for (tmp3=0;tmp3<100;tmp3++) a[tmp3] = 0;
    tmp0 = 0, out2 = 0;
    while (tmp0 < n) {
      tmp1 = 0, out1 = 0;
      while (tmp1 < n) {
        scanf("%d", &tmp2);
        a[tmp1] += tmp2;
        printf("%5d", tmp2);
        out1 += tmp2;
        tmp1++;
      }
      printf("%5d\n", out1);
      a[tmp1] += out1;
      tmp0++;
    }
    tmp0 = 0;
    while (tmp0 < n) {
      printf("%5d", a[tmp0]);
      tmp0++;
    }
    printf("%5d\n", a[tmp0]);
    scanf("%d", &n);
  }
  return 0;
}
#include <stack>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <utility>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

int main() {
    
    int n;
    while(cin >> n && n != 0)
    {
        int a[11][11];
        for(int i = 0;i < n;i++)
        {
            for(int j = 0; j<n;j++)
            {
                cin >> a[i][j];
            }
        }
        for(int i = 0; i<n;i++)
        {
            a[i][n] = 0;
            a[n][i] = 0;
            for(int j = 0;j < n;j++)
            {
                a[i][n] += a[i][j];
                a[n][i] += a[j][i];
            }
        }
        a[n][n] = 0;
        for(int i = 0;i<n;i++)
        {
            a[n][n] += a[i][n];
        }
        for(int i = 0; i< n+1;i++)
        {
            for(int j = 0; j<n+1;j++)
            {
                cout << setw(5) << right << a[i][j];
            }
            cout <<endl;
        }
    }
    return 0;
}