datasets = []

while true
  n = gets.to_i
  break if n <= 0
  data = [] 
  n.times do
    data << gets.chomp!.split.map(&:to_i)
  end
  datasets << data
end

datasets.each do |data|
  data.each do |row| 
    row.each {|n| printf("%5d", n)}
    printf("%5d\n", row.inject(0) {|s, x| s + x})
  end

  data.transpose.each do |col|
    printf("%5d", col.inject(0) {|s, x| s + x})
  end
  printf("%5d\n", data.flatten.inject(0) {|s, x| s + x})
end