Language: C++

/*
Author: Qaseem Hasan alias Mikepayne14(Codeforces,Atcoder) 
                           qaseem_hasan_ (Codechef,HackerRank,HackerEarth)
*/


//---------------------------------------------------------
//#pragma GCC optimize "trapv"

#include <bits/stdc++.h>
using namespace std;
//---------------------------------------------------------

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<vector<pair<int,int>>> vvpii;
typedef set<int> seti;
typedef unordered_set<int> us;
typedef unordered_set<int,greater<int>> usrev;
typedef priority_queue <int, vector<int>, greater<int>> revpq;

const int INF = (int)1e9;
const int EPS = 1e-8;
const int MOD  = 1000000007;
const ld PI = 3.14159265359;
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = {-1, 0, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

#define acc accumulate
#define eb emplace_back
#define mii map <int,int>
#define F first
#define S second
#define MP make_pair
#define mid(s,e) (s+(e-s)/2)
#define fil(v,x) fill(all(v),x)
#define filcon(v,x) iota(all(v),x)
#define repi(n) for(int i=0;i<n;++i)
#define repj(n) for(int j=0;j<n;++j)
#define repr(n) for(int i=n-1;i>=0;--i)
#define erase(v,e) v.erase(std::remove(v.begin(), v.end(), e), v.end())
#define sqr(x) ((x)*(x))
#define MEM(a,b) memset(a, b, sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define inrange(i,a,b) ((i>=min(a,b)) && (i<=max(a,b)))
#define FLSH fflush(stdout)
// #define min_all(a,b,c,d,...) min({a,b,c,d,...})
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr);
#define make_unique(v) sort(all(v)); v.erase(unique(all(v)), v.end());
#define readfull(s) getline(cin,s)
#define printv(v) for(auto i:v) cout << i << " "
#define trace(x) cout<<#x<<" = { "<<x<<" }\n";
#define khatam '\n'
#define test int t; cin >> t; repi(t) {solve(); cout << khatam;}
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for ( \
    auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
    blockTime.second; \
    debug("%s: %lld ms\n", d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false \
)

//--------------------------------------------------------
template<typename T>
T lcm(T a,T b)
{
    return (a*b)/__gcd(a,b);
}

inline bool grid_inside(int x, int y, int n, int m) {return x >= 1 && x <= n && y >= 1 && y <= m;}

template<typename ForwardIterator,typename T>
T first_val(ForwardIterator first,ForwardIterator last,T value)
{
    auto it = std::lower_bound(first,last,value);
    return (it!=last)?(it - first) : -1;
}

template<typename ForwardIterator,typename T>
T last_val(ForwardIterator first,ForwardIterator last,T value)
{
    auto it = std::upper_bound(first,last,value);
    return ((it!=last)?((it - first)-1) : -1);
}

template <typename T> T Dis(T x1,T y1,T x2, T y2)
{return sqrt(sqr(x1-x2) + sqr(y1-y2));}

template <typename T> T Angle(T x1,T y1,T x2, T y2)
{return atan((db)(y1-y2) / (db)(x1-x2));}

int Set(int N,int pos){ return N=N | (1<<pos);}
int Reset(int N,int pos){return N= N & ~(1<<pos);}
bool Check(int N,int pos){return (bool)(N & (1<<pos));}
template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i));}

//--------------------------------------------------------

bool compare(const pair<int, int>&i, const pair<int, int>&j)
{
    return i.F < j.F;
}

int doubleCompare(double x, double y) {
    if (fabs(x-y) <= EPS)
        return 0;
    if(x < y)
        return -1;
    return 1;
}

LSOne(int x)
{
    return ((x)&(-x));
}

int countSetBits(int n)
{
    int count=0;
    while(n>0)
    {
        count += (n&1);
        n = n>>1;
    }
    return count;
}

int getPosBit(int n,int k)
{
    return (n&(1<<k))!=0?1:0;
}

void setPosBit(int &n,int k)
{
    n = n|(1<<k);
}

ll fast_power(int a,int x)
{
    if(x==0)
        return 1;
    else if(x==1)
        return a;
    else
    {
        ll R = fast_power(a,x>>1);
        if(!(x&1))
            return R*R;
        else
            return R*a*R;
    }
}

ll fast_power_unlocked(ll a,ll n,ll mod)
{
    ll res = 1;
    while(n > 0)
    {
        if(n&1)
        {
            res = (res*a)%mod;
        }
        a = (a*a)%mod;
        n = n >> 1;
    }
    return res;
}

ll modInverse(ll A,ll M)
{
    return fast_power_unlocked(A,M-2,M);
}

/* DSU
vi par(MAX_N,0);
vi rnk(MAX_N,0);

int find(int x)
{
    if(x==par[x])
        return x;
    return par[x] = find(par[x]);
}

void Union(int x,int y)
{
    int par_x = find(x);
    int par_y = find(y);

    if(par_x == par_y) return;

    if(rnk[par_x] < rnk[par_y])
        swap(par_x,par_y)
    par[par_y] = par_x;

    if(rnk[par_x] == rnk[par_y])
        rnk[par_x]++;
    }
}
*/

bool checkPrime(int n)
{
    int cnt=0;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i == 0)
        {
            if(n/i == i)
                cnt++;
            else
                cnt += 2;
        }
    }
    if(cnt == 2)
        return true;
    else
        return false;
}

/*
const int mx = 1e5;
bool isPrime[mx];
void createSieve()
{
    MEM(isPrime,true);
    for(int i=2;i*i<=mx;i++)
    {
        if(isPrime[i])
        {
            for(int j=i*i;j<=mx;j+=i)
            {
                isPrime[j] = false;
            }
        }
    }
}*/

/*vll f;
vll invf;
void init(){
    f.resize(MAX_N, 1);
    invf.resize(MAX_N, 1);
    repi(MAX_N) f[i] = (f[i-1]*i)%MOD;
    repi(MAX_N) invf[i] = modInverse(f[i],MOD);
}

ll ncr(ll n, ll r){
    return invf[n-r] * ( f[n] * invf[r] % MOD ) % MOD;
}*/

const int MAX_N=501;
int c[MAX_N][MAX_N];
int n;
int vis[MAX_N][MAX_N];
pair<int,bool> min_dist;
pair<int,bool> check(int x,int y)
{
    vis[x][y]=1;
    //cout << x  << " "<< y << khatam;
    min_dist.F = min(min_dist.F,min({x,y,n-1-x,n-1-y}));
    if(x==0 || x==n-1 || y==0 || y == n-1){
        min_dist.S = true;
        return min_dist;
    }

    if(c[x-1][y]==0 && !vis[x-1][y])
        min_dist = MP(min(min_dist.F, check(x-1,y).F),min_dist.S);
    if(c[x][y+1]==0 && !vis[x][y+1])
        min_dist = MP(min(min_dist.F, check(x,y+1).F),min_dist.S);
    if(c[x+1][y]==0 && !vis[x+1][y])
        min_dist = MP(min(min_dist.F, check(x+1,y).F),min_dist.S);
    if(c[x][y-1]==0 && !vis[x][y-1])
        min_dist = MP(min(min_dist.F, check(x,y-1).F),min_dist.S);

    return min_dist;
}

void solve()
{
    cin >> n;
    int sz = n*n;
    int f=1;
    int cnt=0,val;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
            c[i][j] = f;
            f++;
        }
    }
    for(int i=0;i<sz;i++){
        cin >> val;
        int x = ((val-1)/n);
        int y = ((val-1)%n);
        //cout << x << " " << y << khatam;
        min_dist = MP(10000,false);
        MEM(vis,0);
        auto it = check(x,y);
        if(it.S){
            c[x][y] = 0;
        }
        else {cnt+=it.F; c[x][y]=0;}
    }
    cout << cnt << khatam;
}

int main()
{
    FAST_IO;
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
//-------------------------------------------------------
        time__("Time Taken"){
        solve();
    }
}

Compiler output:
a.cc:125:1: error: ISO C++ forbids declaration of 'LSOne' with no type [-fpermissive]
  125 | LSOne(int x)
      | ^~~~~