Buggy python submission (verdict: Time Limit Exceeded):
from numba import njit
import numpy as np
import sys
input = sys.stdin.buffer.readline

@njit('i4, i4, i4, i4[:,:], i4[:,:]')
def dfs(N, h, w, minDist, isFilled):
    dist = minDist[h][w] + isFilled[h][w]

    if h + 1 < N and minDist[h + 1][w] > dist:
        minDist[h + 1][w] = dist
        dfs(N, h + 1, w, minDist, isFilled)
    if h - 1 >= 0 and minDist[h - 1][w] > dist:
        minDist[h - 1][w] = dist
        dfs(N, h - 1, w, minDist, isFilled)
    if w + 1 < N and minDist[h][w + 1] > dist:
        minDist[h][w + 1] = dist
        dfs(N, h, w + 1, minDist, isFilled)
    if w - 1 < N and minDist[h][w - 1] > dist:
        minDist[h][w - 1] = dist
        dfs(N, h, w - 1, minDist, isFilled)

@njit('i4(i4, i4[:])')
def solve(N, P):
    minDist = np.ones((N, N), dtype=np.int32)
    isFilled = np.ones((N, N), dtype=np.int32)

    for h in range(N):
        for w in range(N):
            minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

    ans = 0
    for p in P:
        h, w = divmod(p, N)
        ans += minDist[h][w]
        isFilled[h][w] = False
        dfs(N, h, w, minDist, isFilled)
    return ans

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [[h, w]]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append([h + 1, w])
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append([h - 1, w])
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append([h, w + 1])
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append([h, w - 1])
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('dfs', '(i4, i4, i4, i4[:,:], i4[:,:])')
    def dfs(N, h, w, minDist, isFilled):
        dist = minDist[h][w] + isFilled[h][w]

        if h + 1 < N and minDist[h + 1][w] > dist:
            minDist[h + 1][w] = dist
            dfs(N, h + 1, w, minDist, isFilled)
        if h - 1 >= 0 and minDist[h - 1][w] > dist:
            minDist[h - 1][w] = dist
            dfs(N, h - 1, w, minDist, isFilled)
        if w + 1 < N and minDist[h][w + 1] > dist:
            minDist[h][w + 1] = dist
            dfs(N, h, w + 1, minDist, isFilled)
        if w - 1 < N and minDist[h][w - 1] > dist:
            minDist[h][w - 1] = dist
            dfs(N, h, w - 1, minDist, isFilled)

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False
            dfs(N, h, w, minDist, isFilled)
        return ans

    cc.compile()
    exit(0)

from my_module import solve, dfs
N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from numba import njit
import numpy as np

@njit('i4, i4, i4, i4[:,:], i4[:,:]')
def dfs(N, h, w, minDist, isFilled):
    dist = minDist[h][w] + isFilled[h][w]

    if h + 1 < N and minDist[h + 1][w] > dist:
        minDist[h + 1][w] = dist
        dfs(N, h + 1, w, minDist, isFilled)
    if h - 1 >= 0 and minDist[h - 1][w] > dist:
        minDist[h - 1][w] = dist
        dfs(N, h - 1, w, minDist, isFilled)
    if w + 1 < N and minDist[h][w + 1] > dist:
        minDist[h][w + 1] = dist
        dfs(N, h, w + 1, minDist, isFilled)
    if w - 1 < N and minDist[h][w - 1] > dist:
        minDist[h][w - 1] = dist
        dfs(N, h, w - 1, minDist, isFilled)

@njit('i4(i4, i4[:])')
def solve(N, P):
    minDist = np.ones((N, N), dtype=np.int32)
    isFilled = np.ones((N, N), dtype=np.int32)

    for h in range(N):
        for w in range(N):
            minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

    ans = 0
    for p in P:
        h, w = divmod(p, N)
        ans += minDist[h][w]
        isFilled[h][w] = False
        dfs(N, h, w, minDist, isFilled)
    return ans

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Runtime Error):
from numba import njit
import numpy as np

@njit('i4, i4, i4, i4[:,:], i4[:,:]', cache=True)
def dfs(N, h, w, minDist, isFilled):
    dist = minDist[h][w] + isFilled[h][w]

    if h + 1 < N and minDist[h + 1][w] > dist:
        minDist[h + 1][w] = dist
        dfs(N, h + 1, w, minDist, isFilled)
    if h - 1 >= 0 and minDist[h - 1][w] > dist:
        minDist[h - 1][w] = dist
        dfs(N, h - 1, w, minDist, isFilled)
    if w + 1 < N and minDist[h][w + 1] > dist:
        minDist[h][w + 1] = dist
        dfs(N, h, w + 1, minDist, isFilled)
    if w - 1 < N and minDist[h][w - 1] > dist:
        minDist[h][w - 1] = dist
        dfs(N, h, w - 1, minDist, isFilled)

@njit('i4(i4, i4[:])', cache=True)
def main(N, P):
    minDist = np.ones((N, N), dtype=np.int32)
    isFilled = np.ones((N, N), dtype=np.int32)

    for h in range(N):
        for w in range(N):
            minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

    ans = 0
    for p in P:
        h, w = divmod(p, N)
        ans += minDist[h][w]
        isFilled[h][w] = False
        dfs(N, h, w, minDist, isFilled)
    return ans

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(main(N, P))


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))
INF = 10**18

minDist = [[INF] * N for _ in range(N)]
isFilled = [[True] * N for _ in range(N)]

def dfs(h, w):
    st = [(h, w)]
    while st:
        h, w = st.pop()
        dist = minDist[h][w] + (1 if isFilled[h][w] else 0)
        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if 0 <= toH < N and 0 <= toW < N:
                if minDist[toH][toW] > dist:
                    minDist[toH][toW] = dist
                    st.append((toH, toW))

for h in range(N):
    for w in range(N):
        minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

ans = 0
for h, w in P:
    ans += minDist[h][w]
    isFilled[h][w] = False
    dfs(h, w)
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))
INF = 10**18

minDist = [[INF] * N for _ in range(N)]
isFilled = [[True] * N for _ in range(N)]


def dfs(h, w):
    st = [(h, w)]
    while st:
        h, w = st.pop()
        dist = minDist[h][w] + (1 if isFilled[h][w] else 0)
        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if 0 <= toH < N and 0 <= toW < N:
                if minDist[toH][toW] > dist:
                    minDist[toH][toW] = dist
                    st.append((toH, toW))

for i in range(N):
    minDist[i][0] = 0
    minDist[0][i] = 0
    minDist[i][N - 1] = 0
    minDist[N - 1][i] = 0

for i in range(N):
    dfs(i, 0)
    dfs(0, i)
    dfs(i, N - 1)
    dfs(N - 1, i)

ans = 0
for h, w in P:
    ans += minDist[h][w]
    isFilled[h][w] = False
    dfs(h, w)
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
sys.setrecursionlimit(10**7)

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))
INF = 10**18

minDist = [[INF] * N for _ in range(N)]
isFilled = [[True] * N for _ in range(N)]


def dfs(h, w):
    dist = minDist[h][w] + (1 if isFilled[h][w] else 0)
    for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
        if 0 <= toH < N and 0 <= toW < N:
            if minDist[toH][toW] > dist:
                minDist[toH][toW] = dist
                dfs(toH, toW)

for i in range(N):
    minDist[i][0] = 0
    minDist[0][i] = 0
    minDist[i][N - 1] = 0
    minDist[N - 1][i] = 0
for i in range(N):
    dfs(i, 0)
    dfs(0, i)
    dfs(i, N - 1)
    dfs(N - 1, i)

ans = 0
for h, w in P:
    ans += minDist[h][w]
    isFilled[h][w] = False
    dfs(h, w)
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
sys.setrecursionlimit(10**7)

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))
INF = 10**18

minDist = [[INF] * N for _ in range(N)]
isFilled = [[True] * N for _ in range(N)]


def dfs(h, w):
    dist = minDist[h][w] + (1 if isFilled[h][w] else 0)
    for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
        if 0 <= toH < N and 0 <= toW < N:
            if minDist[toH][toW] > dist:
                minDist[toH][toW] = dist
                dfs(toH, toW)

for i in range(N):
    minDist[i][0] = 0
    minDist[0][i] = 0
    minDist[i][N - 1] = 0
    minDist[N - 1][i] = 0
for i in range(N):
    dfs(i, 0)
    dfs(0, i)
    dfs(i, N - 1)
    dfs(N - 1, i)

ans = 0
for h, w in P:
    ans += minDist[h][w]
    isFilled[h][w] = False
    dfs(h, w)
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))

isFilled = [[False] * N for _ in range(N)]

def calcDist():
    minDist = [[10**18] * N for _ in range(N)]
    que = deque([])
    for h in range(N):
        if isFilled[h][0]:
            que.append((h, 0, 1))
        else:
            que.appendleft((h, 0, 0))
        if isFilled[h][N - 1]:
            que.append((h, N - 1, 1))
        else:
            que.appendleft((h, N - 1, 0))
    for w in range(1, N - 1):
        if isFilled[0][w]:
            que.append((0, w, 1))
        else:
            que.appendleft((0, w, 0))
        if isFilled[N - 1][w]:
            que.append((N - 1, w, 1))
        else:
            que.appendleft((N - 1, w, 0))

    while que:
        h, w, dist = que.popleft()

        if minDist[h][w] < dist:
            continue
        minDist[h][w] = dist

        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if not (0 <= toH < N and 0 <= toW < N):
                continue

            d = dist
            if isFilled[toH][toW]:
                d += 1

            if minDist[toH][toW] > d:
                minDist[toH][toW] = d
                if isFilled[toH][toW]:
                    que.append((toH, toW, d))
                else:
                    que.appendleft((toH, toW, d))

    return minDist

ans = 0
for h, w in P[::-1]:
    ans += calcDist()[h][w]
    isFilled[h][w] = True
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))

isFilled = [[False] * N for _ in range(N)]

def calcDist():
    minDist = [[10**18] * N for _ in range(N)]
    que = deque([])
    for h in range(N):
        if isFilled[h][0]:
            que.append((h, 0, 1))
        else:
            que.appendleft((h, 0, 0))
        if isFilled[h][N - 1]:
            que.append((h, N - 1, 1))
        else:
            que.appendleft((h, N - 1, 0))
    for w in range(1, N - 1):
        if isFilled[0][w]:
            que.append((0, w, 1))
        else:
            que.appendleft((0, w, 0))
        if isFilled[N - 1][w]:
            que.append((N - 1, w, 1))
        else:
            que.appendleft((N - 1, w, 0))

    while que:
        h, w, dist = que.popleft()

        if minDist[h][w] <= dist:
            continue
        minDist[h][w] = dist

        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if not (0 <= toH < N and 0 <= toW < N):
                continue

            if isFilled[toH][toW]:
                que.append((toH, toW, dist + 1))
            else:
                que.appendleft((toH, toW, dist))
    return minDist

ans = 0
for h, w in P[::-1]:
    ans += calcDist()[h][w]
    isFilled[h][w] = True
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))

isFilled = [[False] * N for _ in range(N)]

def calcDist():
    minDist = [[10**18] * N for _ in range(N)]
    que = deque([])
    for h in range(N):
        if isFilled[h][0]:
            que.append((h, 0, 1))
        else:
            que.appendleft((h, 0, 0))
        if isFilled[h][N - 1]:
            que.append((h, N - 1, 1))
        else:
            que.appendleft((h, N - 1, 0))
    for w in range(1, N - 1):
        if isFilled[0][w]:
            que.append((0, w, 1))
        else:
            que.appendleft((0, w, 0))
        if isFilled[N - 1][w]:
            que.append((N - 1, w, 1))
        else:
            que.appendleft((N - 1, w, 0))

    while que:
        h, w, dist = que.popleft()

        if minDist[h][w] <= dist:
            continue
        minDist[h][w] = dist

        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if not (0 <= toH < N and 0 <= toW < N):
                continue

            if isFilled[toH][toW]:
                que.append((toH, toW, dist + 1))
            else:
                que.append((toH, toW, dist))
    return minDist

ans = 0
for h, w in P[::-1]:
    ans += calcDist()[h][w]
    isFilled[h][w] = True
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

def pToHW(p):
    p = int(p) - 1
    return divmod(p, N)

P = list(map(pToHW, input().split()))

isFilled = [[False] * N for _ in range(N)]

def calcDist():
    minDist = [[10**18] * N for _ in range(N)]
    que = deque([])
    for h in range(N):
        if isFilled[h][0]:
            que.append((h, 0, 1))
        else:
            que.appendleft((h, 0, 0))
        if isFilled[h][N - 1]:
            que.append((h, N - 1, 1))
        else:
            que.appendleft((h, N - 1, 0))
    for w in range(1, N - 1):
        if isFilled[0][w]:
            que.append((0, w, 1))
        else:
            que.appendleft((0, w, 0))
        if isFilled[N - 1][w]:
            que.append((N - 1, w, 1))
        else:
            que.appendleft((N - 1, w, 0))

    while que:
        h, w, dist = que.popleft()

        if minDist[h][w] <= dist:
            continue
        minDist[h][w] = dist

        for toH, toW in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if not (0 <= toH < N and 0 <= toW < N):
                continue

            if isFilled[toH][toW]:
                que.append((toH, toW, dist + 1))
            else:
                que.append((toH, toW, dist))
    return minDist

ans = 0
for h, w in P[::-1]:
    ans += calcDist()[h][w]
    isFilled[h][w] = True
print(ans)


Fixed submission:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('solve', 'i4(i4, i4[:])')
    def solve(N, P):
        minDist = np.ones((N, N), dtype=np.int32)
        isFilled = np.ones((N, N), dtype=np.int32)

        for h in range(N):
            for w in range(N):
                minDist[h][w] = min(h, N - h - 1, w, N - w - 1)

        ans = 0
        for p in P:
            h, w = divmod(p, N)
            ans += minDist[h][w]
            isFilled[h][w] = False

            st = [(h, w)]
            while st:
                h, w = st.pop()
                dist = minDist[h][w] + isFilled[h][w]

                if h + 1 < N and minDist[h + 1][w] > dist:
                    minDist[h + 1][w] = dist
                    st.append((h + 1, w))
                if h - 1 >= 0 and minDist[h - 1][w] > dist:
                    minDist[h - 1][w] = dist
                    st.append((h - 1, w))
                if w + 1 < N and minDist[h][w + 1] > dist:
                    minDist[h][w + 1] = dist
                    st.append((h, w + 1))
                if w - 1 < N and minDist[h][w - 1] > dist:
                    minDist[h][w - 1] = dist
                    st.append((h, w - 1))
        return ans

    cc.compile()
    exit(0)

from my_module import solve

N = int(input())
P = np.asarray(list(map(lambda a: int(a) - 1, input().split())), dtype=np.int32)

print(solve(N, P))