#include using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair pdd; const ll mod = 1e9 + 7; //const ll mod = 998244353; #define REP(i,n) for(int i=0;i<(int)n;++i) //debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template ostream& operator << (ostream& os, const pair v){ os << "(" << v.first << ", " << v.second << ")"; return os; } template ostream& operator << (ostream& os, const vector v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os; } template ostream& operator << (ostream& os, const vector> v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os; } string num2bit(ll num, ll len){ string bit = ""; REP(i, len){ bit += char('0'+(num>>i & 1)); } return bit; } vector< vector< int > > grid_bfs(vector< string > &s, int sx, int sy) { const int vx[] = {0, 1, 0, -1, 1, 1, -1, -1}, vy[] = {1, 0, -1, 0, 1, -1, 1, -1}; vector< vector< int > > min_cost(s.size(), vector< int >(s[0].size(), -1)); queue< pair< int, int > > que; for(int i = 0; i < (int)s.size(); i++) { for(int j = 0; j < (int)s[i].size(); j++) { if(i == sx && j == sy) { que.emplace(i, j); min_cost[i][j] = 0; } } } while(!que.empty()) { auto p = que.front(); que.pop(); for(int i = 0; i < 8; i++) { int nx = p.first + vx[i], ny = p.second + vy[i]; if(nx < 0 || ny < 0 || ny >= (int)s[0].size() || nx >= (int)s.size()) continue; if(min_cost[nx][ny] != -1) continue; if(s[nx][ny] == '#') continue; min_cost[nx][ny] = min_cost[p.first][p.second] + 1; que.emplace(nx, ny); } } return min_cost; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vector> A(N, vector(N, 0)); REP(i, N){ REP(j, N){ cin >> A[i][j]; A[i][j]--; } } vector s(N); REP(i, N){ REP(j, N){ s[i].push_back('.'); } } auto dist0 = grid_bfs(s, 0, 0); //cout << dist0 << endl; vector dist(N, 0); vector res(N, 0); REP(i, N){ REP(j, N){ dist[A[i][j]] += dist0[i][j]; } } REP(i, N) res[i] = dist[i]; vector> S(N, vector(N+1, 0)); REP(i, N){ REP(j, N){ if(i+j> T(N, vector(N+1, 0)); REP(i, N){ REP(j, N){ if(i+j