#include #include const int MN = 505; inline int Abs(int x) { return x < 0 ? -x : x; } int N, sx, sy, Ans; int A[MN][MN]; int main() { int Tests; scanf("%d", &Tests); while (Tests--) { scanf("%d%d%d", &N, &sx, &sy), Ans = 0x3f3f3f3f; for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) scanf("%d", &A[i][j]); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { int b[35], t = 1; b[1] = A[i][j]; for (int x = -3; x <= 3; ++x) { for (int y = -3; y <= 3; ++y) { if (Abs(x) + Abs(y) > 3 || (!x && !y)) continue; int ni = i + x, nj = j + y; if (ni < 1 || ni > N || nj < 1 || nj > N) continue; b[++t] = A[ni][nj]; } } std::sort(b + 2, b + t + 1); std::reverse(b + 2, b + t + 1); for (int k = t + 1; k <= 8; ++k) b[k] = 0; int num = 0, cnt = 1, turn = 0; while (cnt < 9) { ++turn; for (int k = 1; k <= cnt; ++k) num += b[k]; if (num >= 8 * cnt * cnt) ++cnt; } Ans = std::min(Ans, turn + (Abs(i - sx) + Abs(j - sy) + 1) / 2); } } printf("%d\n", Ans); } return 0; }