#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair PII; typedef vector VI; #define fi first #define se second #define MP make_pair int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) {if (c == '-') f = -1; c = getchar();} while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); return v * f; } const int INF = 1e9; int a[600][600], b[100], n, m, sx, sy; int dis(int a, int b, int c, int d) { return abs(a - c) + abs(b - d); } bool inmap(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; } int cal(int x, int y) { int m = 0; for (int i = x - 3; i <= x + 3; i++) for (int j = y - 3 + abs(x - i); j <= y + 3 - abs(x - i); j++) if (inmap(i, j) && (i != x || j != y)) b[++m] = a[i][j]; b[0] = a[x][y]; sort(b + 1, b + m + 1); reverse(b + 1, b + m + 1); int v = 0, re = 0, u = 0; for (int i = 1; i <= 8; i++) { if (i - 1 <= m) v += b[i - 1]; if (i * i * 8 > u) { int t = (i * i * 8 - u - 1) / v + 1; re += t; u += t * v; } else { u += v; re++; } } return re; } void work() { n = read(), sx = read(), sy = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = read(); int ans = INF; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = min(ans, (dis(sx, sy, i, j) + 1) / 2 + cal(i, j)); printf("%d\n", ans); } int main() { int cas = read(); while (cas--) work(); }