#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
int height[120][120] = { 0 };//保存输入高度值
int length[120][120] = { 0 };//计算后的长度值
int R, C;
int dfs(int r, int c)
{
if (height[r][c] == -1)
{
return 0;
}
if(length[r][c] > 0)
return length[r][c];
int p = 0;
int q = 0;
int m = 0;
int n = 0;
if (height[r - 1][c] != -1 && height[r - 1][c] < height[r][c])
{
p = dfs(r - 1, c);
}
if (height[r + 1][c] != -1 && height[r + 1][c] < height[r][c])
{
q = dfs(r + 1, c);
}
if (height[r ][c - 1] != -1 && height[r][c - 1] < height[r][c])
{
m = dfs(r , c - 1);
}
if (height[r][c + 1] != -1 && height[r][c + 1] < height[r][c])
{
n = dfs(r , c + 1);
}
length[r][c]=MAX(MAX(p,q),MAX(m,n))+1;
return length[r][c];
}
int ans()
{
int i, j;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
length[i][j] = 0;
int maxLength = 0;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
{
int num = dfs(i, j);
if(maxLength < num)
maxLength=num;
}
return maxLength;
}
int main()
{
int T;
int i, j;
memset(height,-1,sizeof(height));
//freopen("in.txt", "r", stdin);
cin >> T;
while (T--)
{
cin >> R >> C;
for (i = 1; i <= R; ++i)
for (j = 1; j <= C; ++j)
cin >> height[i][j];//最外层为-1
cout << ans() << endl;
}
//fclose(stdin);
//system("pause");
return 0;
}