#include #include #include #include using namespace std; inline int read() { char c = getchar(); int x = 0, f = 1; for ( ; !isdigit(c); c = getchar()) if(c == '-') f = -1; for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return x * f; } const int N = 509; int a[N][N]; int res; vector ve; int abs(int x) { if(x < 0) return x * -1; return x; } int n; void pre(int x, int y) { ve.clear(); int tx, ty; for(int i = -3; i <= 3; i++) { for(int j = -3; j <= 3; j++){ tx = x + i; ty = y + j; if(tx < 1 || tx > n) continue; if(ty < 1 || ty > n) continue; if(abs(i) + abs(j) > 3) continue; if(i==0 && j == 0) continue; ve.push_back(a[tx][ty]); } } sort(ve.begin(), ve.end()); ve.push_back(a[x][y]); } int dis(int x1, int y1, int x2, int y2) { int tx = abs(x2 - x1); int ty = abs(y2 - y1); return min(tx, ty) + (max(tx, ty) - min(tx, ty) + 1) / 2; } int cal() { int size = ve.size(); int now = size - 1; int sum = ve[now]; int res = 0, total = 0, tmp; for(int i = 2; i <= 9; i++) { tmp = (8 * (i - 1) * (i - 1) - total + sum - 1) / sum; res += tmp; total += tmp * sum; if(now>=1) { now--; sum += ve[now]; } } return res; } int main() { int T, x, y; T = read(); while(T--) { scanf("%d %d %d", &n, &x, &y); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = read(); res = 1e9; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { pre(i, j); res = min(res, dis(x, y, i, j) + cal()); } } printf("%d\n", res); } }