#include #include #include #include #include #include using namespace std; const int MAXN = 500 + 10; const int INF = 0x7fffffff; int n, x0, y0; int a[MAXN][MAXN]; int move_count(int x, int y) { int d = abs(x - x0) + abs(y - y0); return d == 0 ? 0 : (d - 1) / 2 + 1; } int gene_count(int x, int y) { int c = 0; static int v[25 + 5] = {}; memset(v, 0, sizeof(v)); v[c++] = a[x][y]; for (int i = -3; i <= 3; i++) for (int j = -3; j <= 3; j++) if (abs(i) + abs(j) <= 3 && (i != 0 || j != 0) && 1 <= i + x && i + x <= n && 1 <= j + y && j + y <= n) v[c++] = a[i + x][j + y]; sort(v + 1, v + c, greater()); for (int i = 1; i < c; i++) v[i] += v[i - 1]; // cerr << x << ' ' << y << ": "; // for (int i = 0; i < c; i++) // cerr << v[i] << ' '; // cerr << endl; int t = 0, r = 0; for (int p = 1; p <= 8; p++) { int w = v[min(p - 1, c - 1)]; // cerr << w << endl; int dt = ((8 * p * p - r) + (w - 1)) / w; t += dt; r += dt * w; } return t; } void work() { cin >> n >> x0 >> y0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; int ans = INF; // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) // cerr << move_count(i, j) << ' '; // cerr << endl; // } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = min(ans, move_count(i, j) + gene_count(i, j)); cout << ans << endl; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) work(); return 0; }