//
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
class solution
{
public:
int count;
vector<vector<char> > ss;
string str;
public:
void deal(int i,int j,int now,int type)
{
if(i<=ss.size()-1&&j<=ss[i].size()-1)
{
if(ss[i][j]==str[now])
{
if(now==str.size()-1) count++;
switch(type)
{
case 1:deal(i+1,j+1,now+1,1);break;
case 2:deal(i,j+1,now+1,2);break;
case 3:deal(i+1,j,now+1,3);break;
default:break;
}
}
}
}
};
int main(void)
{
int len,wid,num;
cin>>num;
solution s;
string str_;
while(num--)
{
cin>>len>>wid;
vector<vector<char> > table(len,vector<char>(wid,0));
for(int i=0;i<=len-1;i++)
{
for(int j=0;j<=wid-1;j++)
{
cin>>table[i][j];
//cout<<table[i][j];
}
//cout<<endl;
}
//cout<<"................"<<endl;
cin>>str_;
//cout<<"str:"<<str_<<endl;
//cout<<"................"<<endl;
s.str = str_;
s.count = 0;
s.ss = table;
for(int i=0;i<=len-1;i++)
for(int j=0;j<=wid-1;j++)
{
s.deal(i, j, 0,1);
s.deal(i, j, 0,2);
s.deal(i, j, 0,3);
}
cout<<s.count<<endl;
}
return 0;
}