//1349539 2009-05-08 18:24:44 Accepted 1983 1093MS 272K 5667 B C++ xufeng1618 
#include<iostream>
#include
<string>
#include
<queue>
using namespace std;

char gra[11][11];
int n,m,ttt;
int sx,sy,ex,ey;
int mark[11][11][2];
int dir[4][2= {-1,0,1,0,0,-1,0,1};

typedef 
struct node 
{
    
int x,y;
    
int step;
    
int flag;
}
Node;

int Bfs()
{
    queue
<Node> Q;
    
int k;
    Node p,q;
    p.flag 
= 0;
    p.step 
= 0;
    p.x 
= sx;
    p.y 
= sy;
    memset(mark,
0,sizeof(mark));
    mark[p.x][p.y][p.flag] 
= 1;
    Q.push(p);
    
while(!Q.empty())
    
{
        q 
= Q.front();
        Q.pop();
        
for(k = 0;k < 4;k++)
        
{
            p.x 
= q.x + dir[k][0];
            p.y 
= q.y + dir[k][1];
            p.step 
= q.step + 1;
            p.flag 
= q.flag;
            
if(p.x >= 1 && p.x <= m && p.y >= 1 && p.y <= n && gra[p.x][p.y] != '#')
            
{
                
if(gra[p.x][p.y] == 'E' && p.flag == 1)
                    
return 0;
                
if(gra[p.x][p.y] == 'J')
                    p.flag 
= 1;
                
if(p.step < ttt && mark[p.x][p.y][p.flag] ==0 )
                
{
                    mark[p.x][p.y][p.flag] 
= 1;
                    Q.push(p);
                }

            }

        }

    }

    
return 1;
}


int main()
{
    
int text;
    cin
>>text;
    
int i,j,p,q,x,y,mins,ok;
    
char ch1,ch2,ch3;
    
while(text--)
    
{
        scanf(
"%d%d%d",&m,&n,&ttt);
        
int flag = 0;
        
for(i = 1; i <= m;i++)
        
{
            scanf(
"%s",gra[i]+1);
            
for(j = 1; flag < 2 && j <= n;j++)
            
{
                
if(gra[i][j] == 'S')
                
{
                    sx 
= i;
                    sy 
= j;
                    flag 
++;
                }

                
else if(gra[i][j] == 'E')
                
{
                    ex 
= i;
                    ey 
= j;
                    flag 
++;
                }

            }

        }
 
        
if(ex == 1 && (ey == 1 || ey == n) || ex ==  m && (ey == 1 || ey == n))
            mins 
= 2;
        
else if(ex == 1 || ex == m || ey == 1 || ey == n)
            mins 
= 3;
        
else
            mins 
= 4;
        ok 
= Bfs();        
        
if(ok == 1)//不放障碍
            mins = 0;
        
for(i = 1;mins > 1 && i <= m;i++)//放一个障碍
            for(j = 1; mins > 1 && j <= n;j++)
            
{
                
if(gra[i][j] == '.' || gra[i][j] == 'J')
                
{
                    ch1 
= gra[i][j];
                    gra[i][j] 
= '#';
                    ok 
= Bfs();
                    
if(ok == 1)
                        mins 
= 1;
                    gra[i][j] 
= ch1;
                }

            }


        
for(p = 1;mins > 2 && p <= m;p++)//放两个障碍
            for(q = 1; mins > 2 && q <= n;q++)
            
{
                
if(gra[p][q] == '.' || gra[p][q] == 'J' )
                
{
                    ch1 
= gra[p][q] ;
                    gra[p][q] 
= '#';
                    
for(i = p;mins > 2 && i <= m;i++)
                        
for(j = 1; mins > 2 && j <= n; j++)
                        
{
                            
if(p == i && q == j)
                                
continue;
                            
if(gra[i][j] == '.' || gra[i][j] == 'J')
                            
{
                                ch2 
= gra[i][j];
                                gra[i][j] 
= '#';
                                ok 
= Bfs();
                                
if(ok == 1)
                                    mins 
= 2;
                                gra[i][j] 
= ch2;
                            }

                        }

                    gra[p][q] 
= ch1;
                }

            }



        
for(p=1;mins > 3 && p<=m;p++)//放三个障碍
        {
            
for(q=1;mins > 3 && q<=n;q++)
            
{
                
if(gra[p][q] == '.' || gra[p][q] =='J')
                
{
                    ch1 
= gra[p][q];
                    gra[p][q] 
= '#';
                
                    
for(x=p;mins > 3 && x<=m;x++)
                    
{
                        
for(y=1;mins > 3 && y<=n;y++)
                        
{
                            
if(p==&& q==y)
                                
continue;
                            
if(gra[x][y] == '.' || gra[x][y] =='J')
                            
{
                                ch2 
= gra[x][y];
                                 gra[x][y] 
= '#';
                     
                                
for(i=x;mins > 3 && i<=m;i++)//
                                    for(j=1;mins > 3 && j<=n;j++)//
                                    {
                                        
if(i==&& j==|| i==&& j==q)
                                            
continue;
                                        
if(gra[i][j] == '.' || gra[i][j] =='J')
                                        
{
                                             ch3 
= gra[i][j];
                                            gra[i][j] 
= '#';
                                            ok 
= Bfs();
                                            
if(ok == 1)
                                                mins 
= 3;
                                            gra[i][j] 
= ch3;
                                        }

                                    }

                                gra[x][y] 
= ch2;
                            }

                        }
//for(y=1;y<=n;y++)
                    }
//for(x=p+1;x<=m;x++)
                    gra[p][q] = ch1;
                }
              
            }
//for(q=1;q<=n;q++;)
        }

        cout
<<mins<<endl;
    }

    
return 0;
}