#include<fstream>
#include<string>
#include<vector>
std::ifstream cin("explore.in");
std::ofstream cout("explore.out");
void solve(){
	int const dx[]={0,1, 0,-1};
	int const dy[]={1,0,-1, 0};
	int n,m,k,x,y,d;
	cin>>n>>m>>k>>x>>y>>d;
	std::vector< std::vector<int> > vis(n,std::vector<int>(m));
	std::vector<std::string> s(n);
	for(int i=0;i<n;i++) cin>>s[i];
	int ans=1;
	vis[--x][--y]=1;
	for(int i=0;i<k;i++){
		int tx=x+dx[d],ty=y+dy[d];
		if(0<=tx&&tx<n&&0<=ty&&ty<m&&s[tx][ty]=='.') x=tx,y=ty;
		else d=(d+1)%4;
		if(!vis[x][y]) ++ans,vis[x][y]=1;
	}
	cout<<ans<<'\n';
}
int main(){
	int T;
	cin>>T;
	while(T--) solve();
	return 0;
}
