import std.stdio;
import std.conv;
import std.string;

int i, j, n;

void main() {
    while(1) {
        readf("%d\n", &n);
        if(n == 0) break;
        int[500][500] matrix;
        int[500] sumr;
        int[500] sumc;

        for(i = 0; i < n; i++) {
            string[] str = split(readln());
            for(j = 0; j < n; j++) {
                matrix[i][j] = to!int(str[j]);
                sumc[j] += matrix[i][j];
                sumr[i] += matrix[i][j];
                if(j == n - 1) {
                    matrix[i][j+1] = sumr[i];
                    sumr[j+1] += matrix[i][j+1];
                }
                if(i == n - 1) {
                    matrix[i+1][j] = sumc[j];
                    sumc[j] += matrix[i+1][j];
                }
            }
        }
        matrix[n][n] = sumr[n] + sumc[n];
        for(i = 0; i <= n; i++) {
            for(j = 0; j <= n; j++) {
                writef("%5d", matrix[i][j]);
            }
            writef("\n");
        }
    }
}