#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,pii> dpii; int n; vector<vector<ll>> ar; int xa[]={0,0,1,-1}; int ya[]={1,-1,0,0}; ll ABS(ll x){ if(x>0)return x; return -x; } ld dij(int rad,int sx,int sy,int e){ vector<vector<ll>>dist; for(int i=0;i<rad;i++){ dist.push_back({}); for(int j=0;j<rad;j++){ if(i==sx&&j==sy)dist[i].push_back(0); else dist[i].push_back({LLONG_MAX/3}); } } priority_queue<dpii,vector<dpii>,greater<dpii>>pq; pq.push({0,{sx,sy}}); while(!pq.empty()){ auto [D,P]=pq.top(); auto [i,j]=P; pq.pop(); if(D!=dist[i][j])continue; for(int ps=0;ps<4;ps++){ int x=i+xa[ps],y=j+ya[ps]; if(x<0||x>=rad||y<0||y>=rad)continue; //cout<<i<<' '<<j<<' '<<x<<' '<<y<<endl; ll val=ar[i%n][j%n]; ll nx=ar[x%n][y%n]; if(dist[i][j]+1+ABS(val-nx)<dist[x][y]){ dist[x][y]=dist[i][j]+1+ABS(val-nx); pq.push({dist[x][y],{x,y}}); } } } /*for(auto &el:dist){ for(auto &vl:el){ cout<<<'\t'<<vl<<' '; } cout<<endl; }*/ //ll korval=dist[40*n][20*n]-dist[39*n][20*n]; //ll ules=dist[20*n][40*n]-dist[20*n][39*n]; //cout<<korval<<' '<<ules<<endl; ld sum=0,cnt=0,s2=0,c2=0; for(int i=0;i<rad;i++){ for(int j=0;j<rad;j++){ int d=abs((i-n+1)/n-sx/n)+abs((j-n+1)/n-sy/n); //int d=abs(i-sx)/n+abs(j-sy)/n; if(d==e){ sum+=dist[i][j]; cnt+=1; } if(d==e-1){ s2+=dist[i][j]; c2+=1; } } } ld kesk=sum/cnt; ld k2=s2/c2; ld v=kesk-k2; ld kihte=1e20/v; ld ruute=kihte*kihte; return ruute*.8; //cout<<kesk<<' '<<k2<<endl; } int main() { cin>>n; ar.resize(n,vector<ll>(n,0)); for(auto &a:ar){ for(auto &b:a)cin>>b; } cout<<dij(1001*n,500*n,500*n,250); //dij(6*n,2*n,2*n); return 0; }