#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define sz(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define umap unordered_map
#define uset unordered_set
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
/***************************************************************************************************************************************************/
/**DEBUG**/
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/***************************************************************************************************************************************************/
/* author : drkspark */
/*url : */
int main() {
fastio;
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr);
// #endif
int t;
cin >> t;
while(t--){
int row,col;
cin >> row >> col;
vvll mat(row,vll(col));
for(int i=0;i<row;i++){
for(int j=0; j<col;j++){
cin >> mat[i][j];
}
}
ll val = 0;
if(mat[0][0] <0 || mat[row-1][col-1] <0){
cout << -1 << endl;
continue;
}
// Filling 1st row
for(int i=1; i<col; i++){
if(val == -1){
mat[0][i] = -1;
continue;
}
if(mat[0][i] <0){
val = -1;
continue;
}
mat[0][i] += mat[0][i-1];
}
val = 0;
for(int i=1; i<row; i++){
if(val == -1){
mat[i][0] = -1;
continue;
}
if(mat[i][0] <0){
val = -1;
continue;
}
mat[i][0] += mat[i-1][0];
}
// debug(mat);
for(int i=1; i<row; i++){
for(int j=1; j<col; j++){
if(mat[i][j] <0){//mat[i][j] == -1
continue;
}
ll val = max(max(mat[i-1][j],mat[i][j-1]),mat[i-1][j-1]);//added diagonal case
if(val <0){//val == -1LL (val is not always -1 it may be any -ve number)
mat[i][j] = -1;
}
else{
mat[i][j] += val;
}
}
}
if(mat[row-1][col-1]==-1){
cout<<-1<<endl;
continue;
}
cout << mat[row-1][col-1] << endl;
}
return 0;
}