#include using namespace std; #define pii pair < int , int > int T , N , X , Y , A[503][503] , dst[503][503]; int main(){ for(cin >> T ; T ; --T){ cin >> N >> X >> Y; for(int i = 1 ; i <= N ; ++i) for(int j = 1 ; j <= N ; ++j) cin >> A[i][j]; queue < pii > q; q.push(make_pair(X , Y)); memset(dst , 0x3f , sizeof(dst)); dst[X][Y] = 0; while(!q.empty()){ int x = q.front().first , y = q.front().second; q.pop(); for(int t = -2 ; t <= 2 ; ++t) for(int p = -2 ; p <= 2 ; ++p) if(abs(t) + abs(p) <= 2){ int X = x + t , Y = y + p; if(X < 0 || X > N || Y < 0 || Y > N) continue; if(dst[X][Y] > dst[x][y] + 1){dst[X][Y] = dst[x][y] + 1; q.push(make_pair(X , Y));} } } int ans = 1e9; for(int x = 1 ; x <= N ; ++x) for(int y = 1 ; y <= N ; ++y){ vector < int > val; for(int t = -3 ; t <= 3 ; ++t) for(int p = -3 ; p <= 3 ; ++p) if(abs(t) + abs(p) <= 3 && (t || p)){ int X = x + t , Y = y + p; if(X < 0 || X > N || Y < 0 || Y > N) continue; val.push_back(A[X][Y]); } sort(val.begin() , val.end() , [&](int p , int q){return p > q;}); int food = 0 , peo = A[x][y] , cnt = dst[x][y] , tar = 1; while(tar < 9){ int t = (8 * tar * tar + peo - 1 - food) / peo; food += t * peo; cnt += t; peo += val[tar - 1]; ++tar; } ans = min(ans , cnt); } cout << ans << endl; } return 0; }