#include using namespace std; //#define int long long #define sc(x) scanf("%d",&x); bool vis[1005]; int A[504][505]; int B[504][504]; signed main() { int t,n,x,y; sc(t) while(t--){ sc(n)sc(x)sc(y) for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ sc(A[i][j]) } } int ans = 1e9; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int pq[4] ={0}; for(int z=-3;z <= 3; z++){ for(int w = -abs(3-abs(z)); w <= abs(3-abs(z));w++){ if(z==0&&w==0)continue; else if(z+i>0&&z+i<=n&&j+w>0&&j+w<=n){ pq[A[z+i][w+j]]++; } } } int t = A[i][j]; int b = 0; int a = 0; a += abs(i-x)/2 + abs(j-y)/2 + ((abs(i-x)%2==0&&abs(j-y)%2==0)?0:1); for(int i=1;i<9;i++){ int d = (8*i*i-b)/t+(((8*i*i-b)%t)?1:0); b += d * t; if(pq[3]) t += 3,pq[3]--; else if(pq[2]){ t+=2,pq[2]--; }else if(pq[1])t += 1,pq[1]--; a += d; if(a >= ans)break; } ans = min(ans,a); } } cout<