n = $stdin.gets.to_i
data = Array.new(n + 1).map{Array.new(n + 1)}

n.times do |i|
  num = $stdin.gets.chomp.split(" ").map(&:to_i)
  num.size.times do |j|
    data[i][j] = num[j]
  end
end

total = 0
n.times do |i|
  sumV = 0
  sumH = 0
  n.times do |j|
    sumV += data[j][i]
    sumH += data[i][j]
  end
  data[n][i] = sumV
  data[i][n] = sumH
  total += sumH
end
data[n][n] = total

(n+1).times do |i|
  (n+1).times do |j|
    printf "%5d", data[i][j]
  end
  puts ""
end