#include #include #include using namespace __gnu_pbds; using namespace std; typedef long long LL; typedef unsigned long long uLL; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; LL z = 1; int read(){ int x, f = 1; char ch; while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1; x = ch - '0'; while(ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48; return x * f; } int ksm(int a, int b, int p){ int s = 1; while(b){ if(b & 1) s = z * s * a % p; a = z * a * a % p; b >>= 1; } return s; } int a[505][505], n; multiset s; int build(int x, int y){ s.clear(); int i, j, t, day = 1, aim, need, now = 0, sum = a[x][y], tot = 0; for(i = x - 3; i <= x + 3; i++){ for(j = y - 3; j <= y + 3; j++){ if(i < 1 || i > n || j < 1 || j > n) continue; if(i == x && j == y) continue; if(s.size() < 8){ s.insert(a[i][j]); } else{ t = *s.begin(); if(t < a[i][j]) s.erase(s.begin()), s.insert(a[i][j]); } } } for(i = 2; i <= 9; i++){ aim = 8 * (i - 1) * (i - 1); need = aim - now; if(need < 0) t = 1; else t = (need - 1) / sum + 1; tot += t; //printf("\t%d %d %d %d\n", i, need, sum, (need - 1) / sum + 1); now += sum * t; if(s.size()){ t = *s.rbegin(); s.erase(s.find(t)); sum += t; } } return tot; } int main(){ int i, j, m, ans, T, x, y, t1, t2; T = read(); while(T--){ ans = 1e9; n = read(); x = read(); y = read(); for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) a[i][j] = read(); //printf("%d\n", build(9, 8)); for(i = 1; i <= n; i++){ for(j = 1; j <= n; j++){ if(ans < max(abs(x - i), abs(y - j))) continue; ans = min(ans, max(abs(x - i), abs(y - j)) + build(i, j)); //printf("\t===%d %d %d\n", i, j, max(abs(x - i), abs(y - j)) + build(i, j)); } } printf("%d\n", ans); } return 0; }