loop {
    n = gets.to_i
    break if n == 0
    a = (1..n).map{ gets.split.map(&:to_i) }
    format = "%5d" * (n+1) + "\n"
    a.map!{|row| row + [row.inject(:+)]}
    a = a.transpose
    a.map!{|row| row + [row.inject(:+)]}
    a = a.transpose
    a.each{|row| printf(format, *row)}
}