#include using namespace std; template void read(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f; } const int INF=0x7f7f7f7f; int T,n,m,d[510][510],ans,cnt,tmp,sum,cur; struct node { int x,y; } st[100]; queue q; bool vis[510][510]; int dis[510][510],tot; bool cmp(node A,node B) { return d[A.x][A.y]>d[B.x][B.y]; } int main() { //freopen("1.txt","r",stdin); int x,y,a,b; read(T); while (T--) { read(n); m=n; read(x); read(y); for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) read(d[i][j]); } memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); vis[x][y]=1,dis[x][y]=0; q.push((node){x,y}); while (!q.empty()) { x=q.front().x,y=q.front().y; q.pop(); vis[x][y]=0; for (int i=-2;i<=2;i++) for (int j=-2;j<=2;j++) if (abs(i)+abs(j)<=2) { a=x+i,b=y+j; if (1<=a&&a<=n&&1<=b&&b<=m&&dis[a][b]>dis[x][y]+1) { dis[a][b]=dis[x][y]+1; if (!vis[a][b]) vis[a][b]=1,q.push((node){a,b}); } } } ans=INF; for (int x=1;x<=n;x++) for (int y=1;y<=m;y++) { tmp=dis[x][y]; st[tot=1]=(node){x,y}; for (int i=-3;i<=3;i++) for (int j=-3;j<=3;j++) if (abs(i)+abs(j)<=3) { if (!i&&!j) continue; a=x+i,b=y+j; if (1<=a&&a<=n&&1<=b&&b<=m) st[++tot]=(node){a,b}; } sort(st+2,st+tot+1,cmp); cnt=1; sum=d[x][y]; cur=0; while (1) { cur+=sum; tmp++; if (cur>=cnt*cnt*8) { cnt++; if (cnt==9) break; if (cnt<=tot) sum+=d[st[cnt].x][st[cnt].y]; } } ans=min(ans,tmp); } printf("%d\n",ans); } return 0; } /* 0. Enough array size? Enough array size? Enough array size? Interger overflow? 1. Think TWICE, Code ONCE! Are there any counterexamples to your algo? 2. Be careful about the BOUNDARIES! N=1? P=1? Something about 0? 3. Do not make STUPID MISTAKES! Time complexity? Memory usage? Precision error? */