#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
	const int MAX_N = 10;
	int N;
	int table[MAX_N][MAX_N];
	int keta[MAX_N][MAX_N];
	int max_keta;
	int sum[MAX_N], retu;
	int res;

	while (1) {
		cin >> N;
		if (N == 0)break;
		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++) {
				keta[i][j] = (int)log10(table[i][j]) + 1;
			}
		}

		for (int i = 0;i < N;i++) {
			sum[i] = 0;
			for (int j = 0;j < N;j++) {
				for (int k = 0;k < 5 - keta[i][j];k++) {
					cout << " ";
				}
				cout << table[i][j];
				sum[i] += table[i][j];
			}
			for (int k = 0;k < 5 - (log10(sum[i]) + 1);k++) {
				cout << " ";
			}
			cout << sum[i] << endl;
		}
		//last
		for (int j = 0;j < N;j++) {
			retu = 0;
			for (int i = 0;i < N;i++) {
				retu += table[i][j];
			}
			for (int k = 0;k < 5 - (log10(retu) + 1);k++) {
				cout << " ";
			}
			cout << retu;
		}
		res = 0;
		for (int i = 0;i < N;i++) {
			res += sum[i];
		}
		for (int k = 0;k < 5 - (log10(res) + 1);k++) {
			cout << " ";
		}
		cout << res << endl;
	}

	return 0;
}