#include #include #include #include #include using Modint = atcoder::static_modint<998244353>; using namespace std; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; void testcase(){ int H, W; cin >> H >> W; vector> A(H, vector(W)); rep(y,H) rep(x,W) cin >> A[y][x]; auto f = [&](vector> B) -> int { vector Z(H),X(W),C(H+W),V(H+W); rep(y,H) rep(x,W){ int a = B[y][x]; Z[y] += a; X[x] += a; C[y+x] += a; V[y-x+W] += a; } int ans = 0; ans = max(ans, *max_element(Z.begin(), Z.end())); ans = max(ans, *max_element(X.begin(), X.end())); ans = max(ans, *max_element(C.begin(), C.end())); ans = max(ans, *max_element(V.begin(), V.end())); return ans; }; int ans = 0; rep(tt,2){ rep(y,H){ vector> B = A; int t = 0; rep(x,W){ t += B[y][x]; B[y][x] = 0; } ans = max(ans, t + f(move(B))); } rep(d,H+W-1){ vector> B = A; int t = 0; rep(x,W) if(x<=d && d-x> buf(W, vector(H)); rep(y,H) rep(x,W) buf[x][y] = A[y][x]; swap(A,buf); swap(H,W); } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; rep(t,T) testcase(); return 0; }