#include #include #include #include #include #include #include #include #include #include #include #define maxn 510 using namespace std; int n, x, y; int w[maxn][maxn]; bool st[maxn][maxn]; int get_max(int i, int j, pair &pos) { int res = 0; for (int ix = 1; ix <= n; ix ++ ) for (int jx = 1; jx <= n; jx ++ ) if (abs(ix - i) + abs(jx - j) <= 3 && !st[ix][jx]) if (w[ix][jx] > res) pos = {ix, jx}, res = w[ix][jx]; return res; } int run(int rounds, pair city) { int k = rounds, f = 0, city_men = 1, limit = 8, total = w[city.first][city.second]; while (true) { k ++ ; f += total; if (f >= limit) { city_men ++ ; pair pos; total += get_max(city.first, city.second, pos); st[pos.first][pos.second] = 1; limit = 8 * city_men * city_men; } if (city_men >= 9) break; } return k; } int main() { int T; scanf("%d", &T); while (T -- ) { scanf("%d%d%d", &n, &x, &y); for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) scanf("%d", &w[i][j]); int res = 1e8; for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) if (abs(i - x) + abs(j - y) <= 2) { int tmp; memset(st, 0, sizeof st); st[i][j] = 1; if (i == x && j == y) tmp = run(0, {i, j}); else tmp = run(1, {i, j}); res = min(res, tmp); } printf("%d\n", res); } return 0; }