#include #include #include #include #include #include using namespace std; int a[505][505]; int main() { ios::sync_with_stdio(false); int test; cin >> test; while(test--) { int n, x, y; cin >> n >> x >> y; for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) cin >> a[i][j]; int ans = 1e9; for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) { int step = (abs(i - x) + abs(j - y) + 1) / 2; int cnt[4] = {0, 0, 0, 0}; for(int ii = max(1, i - 3); ii <= min(n, i + 3); ++ii) for(int jj = max(1, j - (3 - abs(i - ii))); jj <= min(n, j + (3 - abs(i - ii))); ++jj) { ++cnt[a[ii][jj]]; } int now = 0, daily = a[i][j]; --cnt[a[i][j]]; int tt = 3; for(int t = 1; t <= 8; ++t) { int need = t * t * 8; int tmp = (need - now + daily - 1) / daily; step += tmp; now += tmp * daily; while(tt && !cnt[tt]) --tt; daily += tt; --cnt[tt]; } ans = min(ans, step); } cout << ans << '\n'; } return 0; }