#include #include using namespace std; int n,x,y,T,ans,t,sum,food; int fd[5],a[510][510]; int abs(int x){if (x<0) x=-x;return x;} int judge(int x,int y) { if (x<0||x>n||y<0||y>n) return 0; return 1; } int main() { scanf("%d",&T); while (T--) { 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]); ans=100000; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) //int i=2,j=3; { t=abs(i-x)+abs(j-y); sum=(t+1)/2; for (int k=1;k<=3;k++) fd[k]=0; for (int dx=-3;dx<=3;dx++) for (int dy=abs(dx)-3;dy<=3-abs(dx);dy++) if (judge(i+dx,j+dy)) fd[a[i+dx][j+dy]]++; //printf("%d\n",sum); //for (int k=1;k<=3;k++) printf("%d ",fd[k]);puts(""); sum++; fd[a[i][j]]--;t=a[i][j];food=0; for (int k=2;k<=9;k++) { int o=8*(k-1)*(k-1)-food; if (o>0) o=(o+t-1)/t;else o=0; sum+=o;food+=o*t; // printf("%d %d %d\n",t,sum,food); for (int h=3;h>=1;h--) if (fd[h]) { fd[h]--;t+=h;break; } } ans=min(ans,sum-1); } printf("%d\n",ans); } }