// compile: g++ -o data data.cpp -O3 -std=gnu++20 -Wall -Wextra -Wshadow -D_GLIBCXX_ASSERTIONS -ggdb3 -fmax-errors=2 -DLOCAL
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
// #define debug(x...) {_variables(#x);_print(x);}
#else
#define debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define INTMAX (int)(9223372036854775807)
#define INTINF (int)(1152921504606846976)
#define int long long
#define MAXN 1010
int mp[MAXN][MAXN];
int h, w;
const int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct graph {
struct node {
int u, v, w;
};
vector<vector<node>> e;
int n, m;
bool directed;
graph(int V, bool D = 0) {
n = V;
e.resize(n);
directed = D;
}
void add_edge(int u, int v, int w = 1) {
e[u].push_back(node{u, v, w});
++m;
}
void graphviz_dump(string filename = "graph.dot") {
ofstream gf;
gf.open(filename);
gf << (directed ? "digraph" : "graph") << " {\n";
gf << " "; rep(i, 0, n) gf << i << " ;"[i==n-1]; gf << endl;
string notation = directed ? " -> " : " -- ";
for (auto es: e) {
for (auto edge: es) {
gf << " " << edge.u << notation << edge.v << (directed ? " ;\n" : ";\n");
}
}
gf << "}\n";
}
int dfs(vector<bool> &vis, int k) {
vis[k] = 1;
int ans = mp[k/w][k%w];
for (auto edge: e[k]) {
if (!vis[edge.v]) ans += dfs(vis, edge.v);
}
return ans;
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int _; cin >> _;
while (_--) {
cin >> h >> w;
rep(i, 0, h) rep(j, 0, w) cin >> mp[i][j];
graph g(h*w);
rep(i, 0, h) {
rep(j, 0, w) {
if (!mp[i][j]) continue;
rep(k, 0, 4) {
int dx = i + dir[k][0], dy = j + dir[k][1];
if (dx<0 || dx>=h || dy<0 || dy>=w) continue;
g.add_edge(i*w+j, dx*w+dy);
}
}
}
vector<bool> vis(h*w, 0);
int ans = 0;
rep(i, 0, h*w) {
if (vis[i]) continue;
ans = max(ans, g.dfs(vis, i));
}
cout << ans << endl;
}
return 0;
}
No Comments