#include using namespace std; typedef long long ll; typedef pair pii; #define sz(a) ((int)a.size()) #define pb push_back #define lson (rt << 1) #define rson (rt << 1 | 1) #define gmid (l + r >> 1) const int maxn = 5e2 + 5; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int a[maxn][maxn]; int n, X, Y; int solve(int sx, int sy){ vector res; for(int i = max(1, sx - 3); i <= min(n, sx + 3); ++i){ for(int j = max(1, sy - 3); j <= min(n, sy + 3); ++j){ int dt = abs(sx - i) + abs(sy - j); if(dt > 3 || i == sx && j == sy) continue; res.pb(a[i][j]); } } sort(res.begin(), res.end()); int ret = 0, people = 1, food = 0, has = 0, incre = a[sx][sy]; // first one while(people < 9){ ++ret; if(has){ if(sz(res) > 0) incre += res.back(), res.pop_back(); has = 0; } food += incre; if(food >= 8 * people * people) has = 1, ++people; } return ret; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--){ cin >> n >> X >> Y; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ cin >> a[i][j]; } } int ret = inf; for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ int dt = abs(X - i) + abs(Y - j); ret = min(ret, (dt + 1) / 2 + solve(i, j)); } } cout << ret << "\n"; } return 0; }