#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define AB(A) for(A=0;A<100;A++)a[A]=0

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) {
    AB(tmp3);
    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<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int matrix[11][11] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	cin >>matrix[i][j];
      }
    }
    int suma = 0;
    for(int i=0,sumc,sumr; i<n; i++){
      sumc = 0;
      sumr = 0;
      for(int j=0; j<n; j++){
	sumc+=matrix[i][j];
	sumr+=matrix[j][i];
      }
      suma+=sumc;
      matrix[i][n] = sumc;
      matrix[n][i] = sumr;
    }
    matrix[n][n] = suma;
    for(int i=0; i<=n; i++){
      for(int j=0; j<=n; j++){
	printf("%5d",matrix[i][j]);
      }
      cout <<endl;
    }
  }
  return 0;
}