Problem:
Score : 700 points

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2.

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat.
To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column).
While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

Constraints

2 \le N \le 500

The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\}.

Input

The input is given from Standard Input in the format

N
P_1 P_2 \cdots P_{N^2}

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

ans

Sample Input 1

3
1 3 7 9 5 4 8 6 2

Sample Output 1

1

Before the end of the movie, the viewers are arranged in the cinema as follows:

1 2 3
4 5 6
7 8 9

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).

Sample Input 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

Sample Output 2

3

Sample Input 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

Sample Output 3

11

Solution in C# (Mono-mcs 6.8.0.105) (judge verdict: Wrong Answer)

using System;
 

class GFG
{
    static  int N; // N-.max_x and max_y  
    static int[] GetCoordinate(int n, int N)
    {
        int[] res = new int[2];
        int x = n % N;
        if (x == 0)
        {
            x = N;
        }
        int y = n / N;
        if (n % N != 0)
        {
            y = y + 1;
        }
        res[0] = x - 1;
        res[1] = y - 1;
        return res;
    }
    // A structure to hold the queries  
    public class Query
    {
        public int x1, y1; // x and y co-ordinates of bottom left  
        public int x2, y2; // x and y co-ordinates of top right  

        public Query(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

    };

    // A function to update the 2D BIT  
    static void updateBIT(int[,] BIT, int x,
                        int y, int val)
    {
        int y1;
        for (; x <= N; x += (x & -x))
        {
            y1 = y;
            // This loop update all the 1D BIT inside the  
            // array of 1D BIT = BIT[x]  
            for (; y1 <= N; y1 += (y1 & -y1))
                BIT[x, y1] += val;
        }
        return;
    }

    // A function to get sum from (0, 0) to (x, y)  
    static int getSum(int[,] BIT, int x, int y)
    {
        int sum = 0;
        int y1;
        for (; x > 0; x -= x & -x)
        {
            y1 = y;
            // This loop sum through all the 1D BIT  
            // inside the array of 1D BIT = BIT[x]  
            for (; y1 > 0; y1 -= y1 & -y1)
            {
                sum += BIT[x, y1];
            }
        }
        return sum;
    }

    // A function to create an auxiliary matrix  
    // from the given input matrix  
    static void constructAux(int[,] mat, int[,] aux)
    {
        // Initialise Auxiliary array to 0  
        for (int i = 0; i <= N; i++)
            for (int j = 0; j <= N; j++)
                aux[i, j] = 0;

        // Conthe Auxiliary Matrix  
        for (int j = 1; j <= N; j++)
            for (int i = 1; i <= N; i++)
                aux[i, j] = mat[N - j, i - 1];

        return;
    }

    // A function to cona 2D BIT  
    static void construct2DBIT(int[,] mat,
                            int[,] BIT)
    {
        // Create an auxiliary matrix  
        int[,] aux = new int[N + 1, N + 1];
        constructAux(mat, aux);

        // Initialise the BIT to 0  
        for (int i = 1; i <= N; i++)
            for (int j = 1; j <= N; j++)
                BIT[i, j] = 0;

        for (int j = 1; j <= N; j++)
        {
            for (int i = 1; i <= N; i++)
            {
                // Creating a 2D-BIT using update function  
                // everytime we/ encounter a value in the  
                // input 2D-array  
                int v1 = getSum(BIT, i, j);
                int v2 = getSum(BIT, i, j - 1);
                int v3 = getSum(BIT, i - 1, j - 1);
                int v4 = getSum(BIT, i - 1, j);

                // Assigning a value to a particular element  
                // of 2D BIT  
                updateBIT(BIT, i, j, aux[i, j] -
                        (v1 - v2 - v4 + v3));
            }
        }
        return;
    }

    // A function to answer the queries  
    static int answerQueries(Query q, int[,] BIT)
    {
      
            int x1 = q.x1 + 1;
            int y1 = q.y1 + 1;
            int x2 = q.x2 + 1;
            int y2 = q.y2 + 1;

            int ans = getSum(BIT, x2, y2) -
                    getSum(BIT, x2, y1 - 1) -
                    getSum(BIT, x1 - 1, y2) +
                    getSum(BIT, x1 - 1, y1 - 1);

        return ans;
        }
    

    // Driver Code 
    public static void Main(String[] args)
    {

    int N;
    N = int.Parse(Console.ReadLine());
        GFG.N = N;
    int[,] mat = new int[N,N];
    int[] A = new int[N*N];
    string[] B = new string[N];
    B = Console.ReadLine().Split(' ');
    for (int i = 0; i < N*N; i++)
        A[i] = int.Parse(B[i]);
    int k = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
        {
            mat[i,j] = A[k];
            k++;
        }
    }

        // Create a 2D Binary Indexed Tree  
    int[,] BIT = new int[N + 1, N + 1];
    construct2DBIT(mat, BIT);
        int cnt = 0;
        Query q= new Query(0,0,0,0);
        for (int i = 0; i < N; i++) {
            int p = A[i];
            int[] coor = new int[2];
            coor = GetCoordinate(p, N);
            int x = coor[0];
            int y = coor[1];
            int ans1, ans2, ans3, ans4;
            ans1 = 0;ans2 = 0;ans3 = 0;ans4 = 0;

            if (x != 0) {
                q.x1 = 0;
                q.x2 = x - 1;
                q.y1 = y;
                q.y2 = y;
                // #print(q.x1,q.y1,q.x2,q.y2)
                ans1 = answerQueries(q, BIT);
            }
            if (x != N - 1) {
                q.x1 = x + 1;
                q.x2 = N - 1;
                q.y1 = y;
                q.y2 = y;
                //#ans2=getSum(BIT,3,y)-getSum(BIT,x,y)
                //#print(q.x1,q.y1,q.x2,q.y2)
                ans2 = answerQueries(q, BIT);
                    }
            if (y != 0) {
                q.x1 = x;
                q.x2 = x;
                q.y1 = 0;
                q.y2 = y - 1;
                //#print(q.x1,q.y1,q.x2,q.y2)
                ans3 = answerQueries(q, BIT);
                    }
            if (y != N - 1) {
                q.x1 = x;
                q.x2 = x;
                q.y1 = y + 1;
                q.y2 = N - 1;
                //#print(q.x1,q.y1,q.x2,q.y2)
                ans4 = answerQueries(q, BIT);
            }
            cnt += Math.Min(Math.Min(ans1, ans2),Math.Min(ans3, ans4));
            updateBIT(BIT, x + 1, y + 1, -1);
        }
        /* Queries of the form - x1, y1, x2, y2  
        For example the query- {1, 1, 3, 2} means the sub-matrix-  
            y  
            /\  
        3 | 1 2 3 4     Sub-matrix  
        2 | 5 3 8 1     {1,1,3,2} --.     3 8 1  
        1 | 4 6 7 5                                 6 7 5  
        0 | 2 4 8 9  
            |  
        --|------ 0 1 2 3 ---. x  
            |  

            Hence sum of the sub-matrix = 3+8+1+6+7+5 = 30  
        */
        Console.WriteLine(cnt);
    }
}