#include #include #include using namespace std; const int MAXN = 507; const int INF = 1e9; int n, x, y; int a[MAXN][MAXN]; struct Point { int i, j; } dir[] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {0, 2}, {0, -2}, {2, 0}, {-2, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}, {0, 3}, {0, -3}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {-3, 0}, {3, 0}, }; struct Item { struct Point p; bool operator<(const Item& rhs) const { return a[p.i][p.j] < a[rhs.p.i][rhs.p.j]; } }; int solve(int ii, int ij) { priority_queue pq; for (int i = 0; i < sizeof(dir)/sizeof(dir[0]); i ++) { struct Point p = {ii + dir[i].i, ij + dir[i].j}; if (1 <= p.i && 1 <= p.j && p.i <= n && p.j <= n) { pq.push({p}); } } int np = 1, vi = a[ii][ij], si = 0, round = 0; while (np < 9) { si += vi; round ++; if (si >= 8*np*np) { if (!pq.empty()) { struct Point p = pq.top().p; pq.pop(); vi += a[p.i][p.j]; } np ++; } } return round; } 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", &a[i][j]); int ans = INF; for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { ans = min(ans, (abs(i-x) + abs(j-y) + 1)/2 + solve(i, j)); } } printf("%d\n", ans); } return 0; }