#include using namespace std; int test, n, x, y, a[501][501], inc, sum, cnt, cur, worker, rest[4], ans; int main() { scanf("%d", &test); while (test--) { 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 = 1e9; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { inc = sum = cnt = 0; memset(rest, 0, sizeof(rest)); for (int k = max(1 - i, -3); k <= 3 && i + k <= n; ++k) for (int l = max(1 - j, -3); l <= 3 && j + l <= n; ++l) if (abs(k) + abs(l) <= 3) ++rest[a[i + k][j + l]]; --rest[a[i][j]]; inc = a[i][j]; cnt = worker = 1; for (cur = 0; cnt < 9; ++cur) { sum += inc; if (sum >= 8 * cnt * cnt) ++cnt; for (int k = 3; cnt > worker && k > 0; --k) if (rest[k]) { --rest[k]; ++worker; inc += k; break; } } if (x == i && y == j) ans = min(ans, cur); else ans = min(ans, cur + (abs(x - i) + abs(y - j) - 1) / 2 + 1); } printf("%d\n", ans); } return 0; }