#include using namespace std; template void read(T &x){ x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();} while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();} x *= f; } inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); } int n,sx,sy,a[505][505]; inline bool in(int x){ return 1<=x&&x<=n; } inline int fff(int n){ return n+1>>1; } inline int calc(int x,int y){ int i,j; int cnt[4]; cnt[0]=cnt[1]=cnt[2]=cnt[3]=0; for (i = -3; i <= 3; ++i) for (j = -3; j <= 3; ++j) if (abs(i) + abs(j) <= 3 && in(x+i) && in(y+j)) ++cnt[a[x+i][y+j]]; int nowt = 0,nows = 0,nowv,nowp; int posi,dt; nowt = 1,cnt[a[x][y]]--,nowv = a[x][y],nowp = 1; while (nowp < 9){ posi = nowp * nowp * 8 - nows; dt = posi % nowv ? (posi/nowv+1) : (posi/nowv); nowt += dt; nows += dt * nowv; nowp += 1; for (i = 3; i >= 1; --i) if (cnt[i]){ --cnt[i],nowv += i; break; } } return nowt; } inline void solve(){ int i,j,ans = 100000000; read(n),read(sx),read(sy); for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) read(a[i][j]); for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) ans = min(ans,calc(i,j)+fff(abs(i-sx)+abs(j-sy))); cout << ans-1 << '\n'; } int main(){ int T; read(T); while (T--) solve(); return 0; }