import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] dij = [(0, 1), (1, 0), (1, 1), (1, -1)] inf = 10**16 # md = 998244353 md = 10**9+7 n=II() aa=LLI1(n) ii=[[] for _ in range(n)] jj=[[] for _ in range(n)] for i,row in enumerate(aa): for j,a in enumerate(row): ii[a].append(i) jj[a].append(j) ans=0 for a in range(n): cs=[[0]*(n+3) for _ in range(3)] for i,j in zip(ii[a],jj[a]): ans+=j l,r=i-j,i+j+1 if l>0: cs[0][0]+=l cs[0][1]-=l+1 cs[0][l+1]+=1 if r