import std.stdio, std.conv, std.algorithm, std.string, std.array;

void main() {
	string[] result;
	while (true) {
		auto n = readln.chomp.to!int;
		if (n == 0) break;
		uint[][] cell = new uint[][](n, n);
		foreach (i; 0..n) {
			auto tmp = readln.split.map!(to!uint).array;	
			cell[i][] = tmp;
		}
		foreach (i; 0..n) {
			string str;
			foreach (j; 0..n) {
				str ~= format("%5d", cell[i][j]);
			}
			str ~= format("%5d", cell[i].sum);
			result ~= str;
		}	
		string str;
		uint s;
		foreach (i; 0..n) {
			uint x = 0;
			foreach (j; 0..n) {
				x += cell[j][i];	
			}
			s += x;
			str ~= format("%5d", x);	
		}
		str ~= format("%5d", s);
		result ~= str;
	}
	foreach (r;result)
		r.writeln;
}