class MatrixLikeComputation
  def execute
    n=gets.to_i
    str=calculate_matrix_total(n)
    puts str
  end

  def calculate_matrix_total(n)
    return "" if n==0
    ans_matrix=""
    rows = n.times.map{gets.chomp.split.map(&:to_i)}
    rows.each do |row|    # 行ごとに数字の配列を格納
      ans_matrix += (row << row.sum.to_s).map{|i| "%5d" % i}.join + "\n"
    end
    column_sums = n.times.map{|i| rows.inject(0){|column_sum,row| column_sum+row[i]}}   # 列ごとの合計を格納する用の配列
    ans_matrix += (column_sums << column_sums.sum.to_s).map{|i| "%5d" % i}.join + "\n" + calculate_matrix_total(gets.to_i)
  end
end

mlc = MatrixLikeComputation.new
mlc.execute
