#include <iostream>
#include <iomanip>
#include <vector>

void process_table(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    if (n <= 0)
        return;
    std::vector<int> col_sums(n, 0);
    for (int i = 0; i < n; ++i) {
        int row_sum = 0;
        for (int j = 0; j < n; ++j) {
            int x;
            in >> x;
            col_sums[j] += x;
            row_sum += x;
            out << std::setw(5) << x;
        }
        out << std::setw(5) << row_sum << '\n';
    }
    int sum = 0;
    for (auto const& col_sum: col_sums) {
        out << std::setw(5) << col_sum;
        sum += col_sum;
    }
    out << std::setw(5) << sum << '\n';
}

int main() {
    while (std::cin) {
        process_table(std::cin, std::cout);
    }
}

