#include using namespace std; typedef long long ll; const int maxn=2e5+100; const int mod = 1e9+7; int a[505][505]; ll cnt[5]; int main() { int t; scanf("%d",&t); while(t--){ int n,x,y; scanf("%d%d%d",&n,&x,&y); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); } } ll ans=1e9; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ ll tmp=(abs(x-i)+abs(y-j)+1)/2; cnt[1]=0;cnt[2]=0;cnt[3]=0; int l=max(i-3,1),r=min(i+3,n); int ll=max(j-3,1),rr=min(j+3,n); for(int k=l;k<=r;k++){ for(int kk=ll;kk<=rr;kk++){ if(k==i&&kk==j) continue; int xx=k-i; int yy=kk-j; if(xx<0) xx=-xx; if(yy<0) yy=-yy; if(xx+yy<=3) cnt[a[k][kk]]++; } } long long v=a[i][j]; long long now=0; for(int k=1;k<9;k++){ long long mubiao=k*k*8; long long day=(mubiao-now)/v; if((mubiao-now)%v) day++; tmp+=day; now+=day*v; if(cnt[3]){ cnt[3]--; v+=3; } else if(cnt[2]){ cnt[2]--; v+=2; } else if(cnt[1]){ cnt[1]--; v++; } else { continue; } } ans=min(ans,tmp); } } printf("%lld\n",ans); } return 0; }