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


def cc_export():
    @njit('(i4, i4, i4[:], i4[:], i4[:], i4)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in list([-w, w, -1, 1]):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i4, i4[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int32)
        there = np.ones(w * w, dtype=np.int32)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int32)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int32)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i4, i4, i4[:], i4[:], i4[:], i4)')(update)
    cc.export('main', '(i4, i4[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int32)
    print(main(n, p))


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def cc_export():
    @njit('(i4, i4[:, :], b1[:], i4[:], i4)')
    def update(n, que, there, cost, pos):
        l = 0
        i = 0

        que[l, 0] = pos
        que[l, 1] = cost[pos] - 1
        # que[l] = pos, cost[pos] - 1
        l += 1

        while i < l:
            pos, dist = que[i]
            i += 1

            if cost[pos] <= dist:
                continue

            y, x = divmod(pos, n)
            cost[pos] = dist

            if y >= 1:
                pp = (y - 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if y <= n - 2:
                pp = (y + 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x >= 1:
                pp = y * n + (x - 1)
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x <= n - 2:
                pp = y * n + (x + 1)
                cc = int(there[pp])
                que[l] = (pp, dist + cc)
                l += 1

    @njit('(i4, i4[:])')
    def main(n, p):
        que = np.zeros((4 * n * n, 2), dtype=np.int32)
        there = np.full(n * n, True)
        cost = np.array([
            min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)
        ],
                        dtype=np.int32)

        ans = 0
        for pos in p:
            ans += cost[pos]
            there[pos] = False
            update(n, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i4, i4[:, :], b1[:], i4[:], i4)')(update)
    cc.export('main', '(i4, i4[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(lambda x: int(x) - 1,
                          input().split())),
                 dtype=np.int32)
    print(main(n, p))


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def cc_export():
    @njit('(i8, i8[:, :], b1[:], i8[:], i8)')
    def update(n, que, there, cost, pos):
        l = 0
        i = 0

        que[l, 0] = pos
        que[l, 1] = cost[pos] - 1
        # que[l] = pos, cost[pos] - 1
        l += 1

        while i < l:
            pos, dist = que[i]
            i += 1

            if cost[pos] <= dist:
                continue

            y, x = pos // n, pos % n
            cost[pos] = dist

            if y >= 1:
                pp = (y - 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if y <= n - 2:
                pp = (y + 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x >= 1:
                pp = y * n + (x - 1)
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x <= n - 2:
                pp = y * n + (x + 1)
                cc = int(there[pp])
                que[l] = (pp, dist + cc)
                l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        que = np.zeros((4 * n * n, 2), dtype=np.int64)
        there = np.full(n * n, True)
        cost = np.array([
            min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            ans += cost[pos]
            there[pos] = False
            update(n, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8[:, :], b1[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(lambda x: int(x) - 1,
                          input().split())),
                 dtype=np.int64)
    print(main(n, p))


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def cc_export():
    @njit('(i8, i8[:, :], b1[:], i8[:], i8)')
    def update(n, que, there, cost, pos):
        l = 0
        i = 0

        que[l, 0] = pos
        que[l, 1] = cost[pos] - 1
        # que[l] = pos, cost[pos] - 1
        l += 1

        while i < l:
            pos, dist = que[i]
            i += 1

            if cost[pos] <= dist:
                continue

            y, x = pos // n, pos % n
            cost[pos] = dist

            if y >= 1:
                pp = (y - 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if y <= n - 2:
                pp = (y + 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x >= 1:
                pp = y * n + (x - 1)
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x <= n - 2:
                pp = y * n + (x + 1)
                cc = int(there[pp])
                que[l] = (pp, dist + cc)
                l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        que = np.zeros((10 * n * n, 2), dtype=np.int64)
        there = np.full(n * n, True)
        cost = np.array([
            min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            ans += cost[pos]
            there[pos] = False
            update(n, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8[:, :], b1[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(lambda x: int(x) - 1,
                          input().split())),
                 dtype=np.int64)
    print(main(n, p))


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def cc_export():
    @njit('(i8, i8[:, :], b1[:], i8[:], i8)')
    def update(n, que, there, cost, pos):
        l = 0
        i = 0

        que[l, 0] = pos
        que[l, 1] = cost[pos] - 1
        # que[l] = pos, cost[pos] - 1
        l += 1

        while i < l:
            pos, dist = que[i]
            i += 1

            if cost[pos] <= dist:
                continue

            y, x = pos // n, pos % n
            cost[pos] = dist

            if y >= 1:
                pp = (y - 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if y <= n - 2:
                pp = (y + 1) * n + x
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x >= 1:
                pp = y * n + (x - 1)
                cc = int(there[pp])
                #que.append((pp, dist + cc))
                que[l] = (pp, dist + cc)
                l += 1
            if x <= n - 2:
                pp = y * n + (x + 1)
                cc = int(there[pp])
                que[l] = (pp, dist + cc)
                l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        que = np.zeros((n * n, 2), dtype=np.int64)
        there = np.full(n * n, True)
        cost = np.array([
            min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            ans += cost[pos]
            there[pos] = False
            update(n, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8[:, :], b1[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(lambda x: int(x) - 1,
                          input().split())),
                 dtype=np.int64)
    print(main(n, p))


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def to_pos(y, x):
    return y * n + x


def to_coord(pos):
    return pos // n, pos % n


def update(pos):
    que = deque()
    que.append((pos, cost[pos] - 1))
    while que:
        pos, dist = que.popleft()
        if cost[pos] <= dist:
            continue

        y, x = to_coord(pos)
        cost[pos] = dist

        if y >= 1:
            pp = to_pos(y - 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if y <= n - 2:
            pp = to_pos(y + 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x >= 1:
            pp = to_pos(y, x - 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x <= n - 2:
            pp = to_pos(y, x + 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))


n = int(input())
p = map(lambda x: int(x) - 1, input().split())

there = [True] * (n * n)
cost = [min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)]

ans = 0
for pos in p:
    ans += cost[pos]
    there[pos] = False
    update(pos)

print(ans)


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))

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


def to_pos(y, x):
    return y * n + x


def to_coord(pos):
    return pos // n, pos % n


def update(pos):
    que = deque()
    que.append((pos, cost[pos] - 1))
    while que:
        pos, dist = que.popleft()
        if cost[pos] <= dist:
            continue

        y, x = to_coord(pos)
        cost[pos] = dist

        if y >= 1:
            pp = to_pos(y - 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if y <= n - 2:
            pp = to_pos(y + 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x >= 1:
            pp = to_pos(y, x - 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x <= n - 2:
            pp = to_pos(y, x + 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))


n = int(input())
p = map(lambda x: int(x) - 1, input().split())

there = [True] * (n * n)
cost = [min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)]

ans = 0
for pos in p:
    ans += cost[pos]
    there[pos] = False
    update(pos)

print(ans)


Fixed submission:
from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))