#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,t,ans;
inline int dfs(int r,int c,vector<vector<int>> &matrix)
{
if(r == n-1 && c == m-1) return matrix[r][c];
if(r<0 || r>=n || c<0 || c>=m) return INF;
return matrix[r][c] + min(dfs(r,c+1,matrix),dfs(r+1,c,matrix));
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--)
{
cin>>n>>m;
vector<vector<int>> matrix(n,vector<int>(m));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>matrix[i][j];
ans = dfs(0,0,matrix);
cout<<ans<<"\n";
}
return 0;
}