#include #include #include #include #include #include #include #include #include using namespace std; void prework() { } int zm[510][510]; unordered_map zr; int moni(int cv, int c1, int c2, int c3) { int tp = cv * 1000000 + c1 * 10000 + c2 * 100 + c3; if (zr.count(tp)) return zr[tp]; int ac = 0; int num = 1; int gen = cv; int food = 0; while (num < 9) { ++ac; food += gen; if (food >= 8 * num * num) { ++num; if (c3) { --c3; gen += 3; } else if (c2) { --c2; gen += 2; } else if (c1) { --c1; gen += 1; } } } return zr[tp] = ac; } int n; int px, py; vector z(4); void check(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= n) return; z[zm[x][y]] ++; } void work() { cin >> n >> px >> py; --px; --py; int res; res = 100000; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin>> zm[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { z.assign(4, 0); for (int d = 1; d <= 3; ++d) { for (int x = -d; x <=d; ++x) { int y = d - abs(x); check(i + x, j + y); if (y!=0) check(i + x, j - y); } } int d0 = abs(i - px) + abs(j - py); int v = d0 / 2; if (d0 % 2) ++v; int r = v + moni(zm[i][j], z[1], z[2], z[3]); res = min(res, r); } } cout << res << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); // close sync between cout and printf int T; cin >> T; prework(); for (int ii=1; ii<=T; ++ii) { work(); } return 0; }