#include using namespace std; #define mp make_pair #define pb push_back #define x first #define y second typedef pair pii; typedef long long ll; typedef unsigned long long ull; template void chkmax(T &x,T y){x void chkmin(T &x,T y){x>y?x=y:T();} template void readint(T &x) { x=0;int f=1;char c; for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1; for(;isdigit(c);c=getchar())x=x*10+c-'0'; x*=f; } /*const int MOD=; inline int dmy(int x){return x>=MOD?x-MOD:x;} inline void inc(int &x,int y){x=dmy(x+y);} int qmi(int x,int y) { int ans=1; for(;y;y>>=1,x=1ll*x*x%MOD) if(y&1)ans=1ll*ans*x%MOD; return ans; }*/ const int MAXN=505; int n,sx,sy,a[MAXN][MAXN],b[MAXN]; void solve() { int ans=1<<30; readint(n),readint(sx),readint(sy); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) readint(a[i][j]); for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { int m=0; for(int x=-3;x<=3;++x) for(int y=abs(x)-3;y<=3-abs(x);++y) if( (x||y) && i+x>0 && j+y>0 && i+x<=n && j+y<=n) b[++m]=a[i+x][j+y]; sort(b+1,b+m+1); b[++m]=a[i][j]; int cur=0,ic=0,res=(abs(sx-i)+abs(sy-j)+1)/2; for(int i=1;i<=8;++i) { ic+=(i<=m?b[m-i+1]:0); int t=(8*i*i-cur+ic-1)/ic; res+=t,cur+=ic*t; } chkmin(ans,res); } } printf("%d\n",ans); } int main() { #ifdef LOCAL freopen("code.in","r",stdin); // freopen("code.out","w",stdout); #endif int T; readint(T); while(T--)solve(); return 0; }