Buggy python submission (verdict: Time Limit Exceeded):
def main(N,P):
    A=[-1]*4*N*N
    ad=(1,N,-1,-N)
    for I in range(4*N*N):
        i,j=I//4,I%4
        if 0<=i+ad[j]<N*N:
            A[i*4+j]=i+ad[j]
    X=[0]*N*N
    for i in range(N*N):
        X[i]=min(i//N,N-1-i//N,i%N,N-1-i%N)
    E=[1]*N*N
    a=0
    q=[0]*N*N*N
    l,r=0,0
    for p in P:
        p-=1
        a+=X[p]
        E[p]=0
        q[r]=p
        r+=1
        while l<r:
            i=q[l]
            l+=1
            for j in A[4*i:4*(i+1)]:
                if 0<=j and X[i]+E[i]<X[j]:
                    X[j]=X[i]+E[i]
                    q[r]=j
                    r+=1
    return a
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)
    main=cc_export(main,(i8,i8[:],))
    cc.compile()
    exit()
N=int(readline())
P=np.array(readline().split(),dtype=np.int64)
from my_module import main
print(main(N,P))


Fixed submission:
def main(N,P):
    ad=(1,N,-1,-N)
    X=[0]*N*N
    for i in range(N*N):
        X[i]=min(i//N,N-1-i//N,i%N,N-1-i%N)
    E=[1]*N*N
    a=0
    q=[0]*N*N*N
    l,r=0,0
    for p in P:
        p-=1
        a+=X[p]
        E[p]=0
        q[r]=p
        r+=1
        while l<r:
            i=q[l]
            l+=1
            for j in ad:
                if 0<=i+j<N*N and X[i]+E[i]<X[i+j]:
                    X[i+j]=X[i]+E[i]
                    q[r]=i+j
                    r+=1
    return a
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)
    main=cc_export(main,(i8,i8[:],))
    cc.compile()
    exit()
N=int(readline())
P=np.array(readline().split(),dtype=np.int64)
from my_module import main
print(main(N,P))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
P=list(map(int,input().split()))
A=[[x if 0<=x<N*N else -1 for x in (i+1,i+N,i-1,i-N)] for i in range(N*N)]
X=[min(i//N,N-1-i//N,i%N,N-1-i%N) for i in range(N*N)]
E=[1]*N*N
r=0
from collections import deque
q=deque()
for p in P:
    p-=1
    r+=X[p]
    E[p]=0
    q.append(p)
    while q:
        i=q.popleft()
        for j in A[i]:
            if 0<=j and X[i]+E[i]<X[j]:
                X[j]=X[i]+E[i]
                q.append(j)
print(r)

Fixed submission:
def main(N,P):
    ad=(1,N,-1,-N)
    X=[0]*N*N
    for i in range(N*N):
        X[i]=min(i//N,N-1-i//N,i%N,N-1-i%N)
    E=[1]*N*N
    a=0
    q=[0]*N*N*N
    l,r=0,0
    for p in P:
        p-=1
        a+=X[p]
        E[p]=0
        q[r]=p
        r+=1
        while l<r:
            i=q[l]
            l+=1
            for j in ad:
                if 0<=i+j<N*N and X[i]+E[i]<X[i+j]:
                    X[i+j]=X[i]+E[i]
                    q[r]=i+j
                    r+=1
    return a
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)
    main=cc_export(main,(i8,i8[:],))
    cc.compile()
    exit()
N=int(readline())
P=np.array(readline().split(),dtype=np.int64)
from my_module import main
print(main(N,P))