#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b>n; int a[n][n]; vector>> v(n); rep(i,n){ rep(j,n){ cin>>a[i][j];a[i][j]--; v[a[i][j]].push_back(make_pair(i,j)); } } int ans=0; rep(i,n){ vector res(n,0),imou(n,0),imod(n,0); int cost=0; for(auto &p:v[i]){ cost+=p.second; int u=p.first-p.second-1; if(u>=0) imou[u]++; int d=p.first+p.second+1; if(d=0;i--){ cnt+=imou[i]; sum+=cnt; res[i]+=sum; } } // debug(imod);debug(imou); // debug(res); // cout<