#include <cstdio>
#include <cassert>
#define REP(i,a) for (int i = 0; i < (a); ++i)
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for (int i = (a); i >= (b); --i)
#define MAX 50
//#define DEBUG 1
int T, R, C, tab;
typedef unsigned long long ull;
ull A[MAX+1][MAX+1], sum[MAX+2][MAX+2], asum;
ull alice( int rf, int rt, int cf, int ct );
ull bob( int rf, int rt, int cf, int ct );
ull rectsum( int rf, int cf, int rt, int ct ) {
ull res = sum[rt+1][ct+1] - sum[rf][ct+1] - sum[rt+1][cf] + sum[rf][cf];
//if (DEBUG) printf( "DEBUG rectsum( rf=%d, rt=%d, cf=%d, ct=%d ) = %llu\n", rf, rt, cf, ct, res );
return res;
}
void spaces( int n ) {
REP(i, n) printf( " " );
}
ull alice( int rf, int rt, int cf, int ct ) {
tab += 2;
//if (DEBUG) { spaces(tab); printf( "alice( act=%llu, rf=%d, rt=%d, cf=%d, ct=%d ) {\n", act, rf, rt, cf, ct ); }
if ( rt < rf || ct < cf ) {
//if ( act << 1 == asum ) half = act; // we found half
//if ( act > max ) max = act;
//if (DEBUG) { spaces(tab); printf( "}\n"); }
//if (DEBUG) tab -= 2;
return 0;
}
// first row
ull min = rectsum(rf, cf, rf, ct);
int flag = 0;
// last row
ull v2 = rectsum(rt, cf, rt, ct);
if ( v2 < min ) { min = v2; flag = 1; }
// first column
ull v3 = rectsum(rf, cf, rt, cf);
if ( v3 < min ) { min = v3; flag = 2; }
// last column
ull v4 = rectsum(rf, ct, rt, ct);
if ( v4 < min ) { min = v3; flag = 3; }
//if (DEBUG) printf( "DEBUG: alice - flag=%d\n", flag );
ull res;
switch ( flag ) {
case 0: res = bob( rf+1, rt, cf, ct ); break;
case 1: res = bob( rf, rt-1, cf, ct ); break;
case 2: res = bob( rf, rt, cf+1, ct ); break;
case 3: res = bob( rf, rt, cf, ct-1 ); break;
}
//if (DEBUG) { spaces(tab); printf( "} (alice)\n"); }
tab -= 2;
return res;
}
ull bcache[MAX][MAX][MAX][MAX];
ull bob( int rf, int rt, int cf, int ct ) {
tab += 2;
//if (DEBUG) { spaces(tab); printf( "bob( act=%llu, rf=%d, rt=%d, cf=%d, ct=%d ) {\n", act, rf, rt, cf, ct ); }
if ( rt < rf || ct < cf ) {
//if ( act << 1 == asum ) half = act; // we found half
//if ( act > max ) max = act;
//if (DEBUG) { spaces(tab); printf( "} bob\n"); }
tab -= 2;
return 0;
}
if ( bcache[rf][rt][cf][ct] != -1 ) { return bcache[rf][rt][cf][ct]; }
ull tmp, max;
max = rectsum(rf, cf, rf, ct);
//if (DEBUG) printf( "DEBUG: tmp=%llu\n", tmp );
max += alice( rf+1, rt, cf, ct );
tmp = rectsum(rt, cf, rt, ct);
//if (DEBUG) printf( "DEBUG: tmp=%llu\n", tmp );
tmp += alice( rf, rt-1, cf, ct );
if ( tmp > max ) max = tmp;
tmp = rectsum(rf, cf, rt, cf);
//if (DEBUG) printf( "DEBUG: tmp=%llu\n", tmp );
tmp += alice( rf, rt, cf+1, ct );
if ( tmp > max ) max = tmp;
tmp = rectsum(rf, ct, rt, ct);
//if (DEBUG) printf( "DEBUG: tmp=%llu\n", tmp );
tmp += alice( rf, rt, cf, ct-1 );
if ( tmp > max ) max = tmp;
//if (DEBUG) { spaces(tab); printf( "} bob\n"); }
//if (DEBUG) tab -= 2;
bcache[rf][rt][cf][ct] = max;
return max;
}
int main() {
tab = 0;
assert( scanf( "%d", &T ) == 1 );
while ( T-- ) { // for each test case
// init
assert( scanf( "%d%d", &R, &C ) == 2 );
asum = 0;
REP( ci, C+1) sum[0][ci] = 0;
//if (DEBUG) { REP (ci, C+1) printf( "%llu ", sum[0][ci]); printf( "\n"); }
REP( ri, R) sum[ri][0] = 0;
REP( ri, R ) {
//if (DEBUG) printf( "%llu ", sum[ri][0] );
REP( ci, C ) {
assert( scanf( "%llu", &A[ri][ci] ) == 1 );
asum += A[ri][ci];
sum[ri+1][ci+1] = A[ri][ci] + sum[ri][ci+1] + sum[ri+1][ci] - sum[ri][ci];
//if (DEBUG) printf( "%llu ", sum[ri+1][ci+1] );
}
//if (DEBUG) printf("\n");
}
REP(i1, R) REP(i2, R) REP(i3, C) REP(i4, C) bcache[i1][i2][i3][i4] = -1;
// Alice moves first
ull bmax = alice( 0, R-1, 0, C-1 );
//if (DEBUG) printf( "DEBUG: max=%llu, half=%llu\n", max, half );
ull alice = asum - bmax;
if ( alice == bmax ) printf( "%llu\n", asum );
else if ( bmax > alice ) printf( "%llu\n", bmax );
else printf( "%llu\n", alice );
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNhc3NlcnQ+CgojZGVmaW5lIFJFUChpLGEpIGZvciAoaW50IGkgPSAwOyBpIDwgKGEpOyArK2kpCiNkZWZpbmUgRk9SKGksYSxiKSBmb3IgKGludCBpID0gKGEpOyBpIDw9IChiKTsgKytpKQojZGVmaW5lIEZPUkQoaSxhLGIpIGZvciAoaW50IGkgPSAoYSk7IGkgPj0gKGIpOyAtLWkpCgojZGVmaW5lIE1BWCA1MAoKLy8jZGVmaW5lIERFQlVHIDEKCmludCBULCBSLCBDLCB0YWI7Cgp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyB1bGw7Cgp1bGwgQVtNQVgrMV1bTUFYKzFdLCBzdW1bTUFYKzJdW01BWCsyXSwgYXN1bTsKCnVsbCBhbGljZSggaW50IHJmLCBpbnQgcnQsIGludCBjZiwgaW50IGN0ICk7CnVsbCBib2IoIGludCByZiwgaW50IHJ0LCBpbnQgY2YsIGludCBjdCApOwoKdWxsIHJlY3RzdW0oIGludCByZiwgaW50IGNmLCBpbnQgcnQsIGludCBjdCApIHsKICAgIHVsbCByZXMgPSBzdW1bcnQrMV1bY3QrMV0gLSBzdW1bcmZdW2N0KzFdIC0gc3VtW3J0KzFdW2NmXSArIHN1bVtyZl1bY2ZdOwoJLy9pZiAoREVCVUcpIHByaW50ZiggIkRFQlVHIHJlY3RzdW0oIHJmPSVkLCBydD0lZCwgY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