N=int(input()) A=[list(map(int,input().split())) for i in range(N)] idx=[[] for a in range(N)] for i in range(N): for j in range(N): A[i][j]-=1 idx[A[i][j]].append((i,j)) ans=N*(N-1)//2*N for a in range(N): imos=[0]*(N+2) for i,j in idx[a]: l,r=i-j,i+j if 0