#!/usr/bin/env python3



n =  int(input())
numbers = input().split(" ")
r = 0

p = []
hate = [-1] * (n*n)
is_seated = [1] * (n*n)

def is_area(x, y):
  if x  >= 0 and x < n and y >= 0 and y < n:
    return True
  else:
    return False
    
def rewrite(x, y):
  global hate
  global is_seated
  global n
  
  index = (x) * n + (y)

  
  points = [[x, y-1], [x, y+1], [x+1, y], [x-1, y]]
  
  flag = 0
  for point in points:
    if is_area(point[0], point[1]):
      new_index = point[0] * n + point[1]
      if hate[new_index] == -1 or hate[new_index] > is_seated[index] + hate[index]:
        hate[new_index]  = is_seated[index] + hate[index]
        rewrite(point[0], point[1])
  return


for x in numbers:
  p.append(int(x))
  

for i in range(n):
  hate[i] = 0
  rewrite(0, 0)
  
  hate[n * n - n + i] = 0
  rewrite(n - 1, i)
  
  hate[i * n] = 0
  rewrite(i, 0)
  
  hate[i * n + n - 1] = 0
  rewrite(i, n - 1)
  
for k in p:
  for i in range(n):
    for j in range(n):
      rewrite(i, j)
      
  for ii in range(n):
    for jj in range(n):
      print(hate[ii * n + jj], end = "")
    print(" ", end = "")
    for jj in range(n):
      if k - 1 !=  ii * n + jj:
        print(is_seated[ii * n + jj], end = "")
      else:
         print("-", end = "")
    print()
  print()
  
  r += hate[k - 1]
  is_seated[k - 1] = 0
  
  
  
  
  
          
#for i in range(n):
#  for j in range(n):
#    print(hate[i * n + j], end = "")
#  print()

print(r)
