#pragma clang diagnostic push #pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions" #pragma ide diagnostic ignored "hicpp-signed-bitwise" #pragma GCC optimize ("Ofast,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #include using namespace std; typedef long long ll; typedef pair pll; typedef pair pii; typedef pair pdd; typedef vector vi; typedef vector vll; typedef vector vd; typedef vector vs; typedef vector vvi; typedef vector vvvi; typedef vector vvll; typedef vector vvvll; typedef vector vpii; typedef vector vvpii; typedef vector vpll; typedef vector vvpll; typedef vector vpdd; typedef vector vvd; #define yn(ans) printf("%s\n", (ans)?"Yes":"No"); #define YN(ans) printf("%s\n", (ans)?"YES":"NO"); template bool chmax(T &a, T b) { if (a >= b) return false; a = b; return true; } template bool chmin(T &a, T b) { if (a <= b) return false; a = b; return true; } #define FOR(i, s, e, t) for ((i) = (s); (i) < (e); (i) += (t)) #define REP(i, e) for (int i = 0; i < (e); ++i) #define REP1(i, s, e) for (int i = (s); i < (e); ++i) #define RREP(i, e) for (int i = (e); i >= 0; --i) #define RREP1(i, e, s) for (int i = (e); i >= (s); --i) #define all(v) v.begin(), v.end() #define pb push_back #define qb pop_back #define pf push_front #define qf pop_front #define maxe max_element #define mine min_element ll inf = 1e18; #define DEBUG printf("%d\n", __LINE__); fflush(stdout); template void print(vector &v, bool withSize = false) { if (withSize) cout << v.size() << endl; REP(i, v.size()) cout << v[i] << " "; cout << endl; } mt19937_64 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); int __FAST_IO__ = []() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 0; }(); #define TESTS int t; cin >> t; while (t--) #define TEST int main() { TEST { int H, W; cin >> H >> W; vvi v(H, vi(W)), c(H, vi(W, -1)); REP(i, H) REP(j, W) cin >> v[i][j]; vvi moves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; typedef array node; priority_queue, greater> pq, pq2; auto upd = [&](priority_queue, greater> &q, int color, int x, int y) { // printf("paint %d %d %d\n", x, y, color); c[x][y] = color; for (auto &m: moves) { int nx = x + m[0], ny = y + m[1]; if (nx >= 0 && nx < H && ny >= 0 && ny < W) { if (c[nx][ny] == -1) q.push({v[nx][ny], nx, ny}); else if (c[nx][ny] != color) throw 1; } } }; upd(pq, 1, 0, 0); upd(pq2, 0, H - 1, W - 1); int ans = 0; while (1) { try { while (1) { if (pq.empty()) goto finish; auto p = pq.top(); pq.pop(); int x = p[1], y = p[2]; //printf("q1 %d %d %d\n", x, y, c[x][y]); if (c[x][y] == -1) { ans++; upd(pq, 1, x, y); break; } } } catch (int e) { break; } try { while (1) { if (pq2.empty()) goto finish; auto p = pq2.top(); pq2.pop(); int x = p[1], y = p[2]; //printf("q2 %d %d %d\n", x, y, c[x][y]); if (c[x][y] == -1) { ans++; upd(pq2, 0, x, y); break; } } } catch (int e) { break; } } finish: printf("%d\n", ans); } return 0; }