#include <iostream>
#include <cstring>
using namespace std;
int m,n,k,map[23][23],flag=1,minnum=9999999;
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
void print () {
for(int i=1; i<=m; i++) {
for(int j=1; j<=n; j++) {
cout<<map[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int avil(int x,int y,int tk) {
if(map[x][y]==-1||x>m||x<=0||y>n||y<=0||(tk==0&&map[x][y]==1)) return 0;
else return 1;
}
//void dfs(int tk,int x,int y,int counter)
//{
// int tempk,tempx,tempy,tempc,tempm;
// //tempm=map[x][y];
// for(int i=0;i<=3;i++)
// {
// tempx=x+dx[i],tempy=y+dy[i];
// if(avil(tempx,tempy,tk)==1)
// {
// tempm=map[tempx][tempy];
// tempc=counter+1;
// if(map[tempx][tempy]==1) tempk=tk-1;
// else tempk=k;
// map[tempx][tempy]=-1;
// if(tempx==m&&tempy==n)
// {
// if(counter<=minnum) minnum=tempc;
if(tempc==8){
cout<<"this is"<<tempc<<endl;
print();}
// flag=0;
// }
// else dfs(tempk,tempx,tempy,tempc);
// map[tempx][tempy]=tempm;
// }
// }
//}
void bfs()
{
int A[500][4];
int head=1,tail=1;
A[head][0]=A[head][1]=1;
A[head][2]=0;
A[head][3]=k;
map[1][1]=-1;
tail++;
while(head<tail)
{
head++;
int tempx,tempy;
for(int i=0;i<=3;i++)
{
tempx=A[head-1][0]+dx[i];
tempy=A[head-1][1]+dy[i];
if(avil(tempx,tempy,A[head-1][3])==1)
{
A[tail][0]=tempx,A[tail][1]=tempy;
A[tail][2]=A[head-1][2]+1;
if(map[tempx][tempy]==1) A[tail][3]=A[head-1][3]-1;
else A[tail][3]=k;
map[tempx][tempy]=-1;
if(tempx==m&&tempy==n)
{
cout<<A[tail][2]<<endl;
return;
}
tail++;
}
}
}
cout<<"-1"<<endl;
}
int main()
{
//freopen("input.txt","r",stdin);
int T;
cin>>T;
for(int t=1; t<=T; t++)
{
cin>>m>>n;
cin>>k;
flag=1;
minnum=999999;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin>>map[i][j];
}
}
if(m==1&&n==1) cout<<"0"<<endl;
else bfs();
}
return 0;
}