while true
  n = gets.chomp.to_i
  break if n == 0

  mat = []
  vsum = (0..n).map{0}

  n.times do
    cs = gets.chomp.split(" ").map{|s| s.to_i}

    hsum = cs.inject{|sum, c|  sum + c}
    cs.push hsum
    mat.push cs

    (0..n).each{|i| vsum[i] += cs[i]}
  end
  mat.push vsum

  mat.each do |hl|
    hl.each{|c| printf("%5d", c)}
    puts
  end
end