#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#define DEBUG 0
#define EAST 0
#define WEST 1
#define SOUTH 2
#define NORTH 3
#define MAXMAP 51
usingnamespacestd;typedefpair<int,int>pos;intmap[MAXMAP][MAXMAP]={-1,};introwsize,colsize,holerow,holecol,time_consumed;intdir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};// 동서남북voidget_input(){//clearing mapfor(inti=0;i<MAXMAP;i++)for(intj=0;j<MAXMAP;j++)map[i][j]=-1;cin>>rowsize>>colsize>>holerow>>holecol>>time_consumed;for(inti=0;i<rowsize;i++){for(intj=0;j<colsize;j++){cin>>map[i][j];}}}intcango(intdirection,poscurr){posnext=make_pair(curr.first+dir[direction][0],curr.second+dir[direction][1]);if(map[next.first][next.second]==0)return0;if(next.first<0||next.first>=rowsize||next.second<0||next.second>=colsize)return0;if(direction==EAST){// if 동 if(map[curr.first][curr.second]==1||map[curr.first][curr.second]==3||map[curr.first][curr.second]==4||map[curr.first][curr.second]==5){if(map[next.first][next.second]==1||map[next.first][next.second]==3||map[next.first][next.second]==6||map[next.first][next.second]==7)return1;}}elseif(direction==WEST){// 서if(map[curr.first][curr.second]==1||map[curr.first][curr.second]==3||map[curr.first][curr.second]==6||map[curr.first][curr.second]==7){if(map[next.first][next.second]==1||map[next.first][next.second]==3||map[next.first][next.second]==4||map[next.first][next.second]==5)return1;}}elseif(direction==SOUTH){// 남 if(map[curr.first][curr.second]==1||map[curr.first][curr.second]==2||map[curr.first][curr.second]==5||map[curr.first][curr.second]==6){if(map[next.first][next.second]==1||map[next.first][next.second]==2||map[next.first][next.second]==4||map[next.first][next.second]==7)return1;}}elseif(direction==NORTH){// 북if(map[curr.first][curr.second]==1||map[curr.first][curr.second]==2||map[curr.first][curr.second]==4||map[curr.first][curr.second]==7){if(map[next.first][next.second]==1||map[next.first][next.second]==2||map[next.first][next.second]==5||map[next.first][next.second]==6)return1;}}return0;};vector<pos>dfs(intfrom,intdepth,posnext){vector<pos>ret;if(depth>time_consumed)returnret;ret.push_back(next);if(from!=EAST&&cango(EAST,next)){vector<pos>temp;posnewpos=make_pair(next.first+dir[EAST][0],next.second+dir[EAST][1]);temp=dfs(WEST,depth+1,newpos);if(temp.size()!=0){ret.reserve(ret.size()+temp.size());ret.insert(ret.end(),temp.begin(),temp.end());}}if(from!=WEST&&cango(WEST,next)){vector<pos>temp;posnewpos=make_pair(next.first+dir[WEST][0],next.second+dir[WEST][1]);temp=dfs(EAST,depth+1,newpos);if(temp.size()!=0){ret.reserve(ret.size()+temp.size());ret.insert(ret.end(),temp.begin(),temp.end());}}if(from!=SOUTH&&cango(SOUTH,next)){vector<pos>temp;posnewpos=make_pair(next.first+dir[SOUTH][0],next.second+dir[SOUTH][1]);temp=dfs(NORTH,depth+1,newpos);if(temp.size()!=0){ret.reserve(ret.size()+temp.size());ret.insert(ret.end(),temp.begin(),temp.end());}}if(from!=NORTH&&cango(NORTH,next)){vector<pos>temp;posnewpos=make_pair(next.first+dir[NORTH][0],next.second+dir[NORTH][1]);temp=dfs(SOUTH,depth+1,newpos);if(temp.size()!=0){ret.reserve(ret.size()+temp.size());ret.insert(ret.end(),temp.begin(),temp.end());}}returnret;}intsolve(){vector<pos>result=dfs(-1,1,make_pair(holerow,holecol));sort(result.begin(),result.end());result.erase(unique(result.begin(),result.end()),result.end());returnresult.size();}intmain(){intsol[50];intntest;cin>>ntest;for(inti=0;i<ntest;i++){get_input();sol[i]=solve();}for(inti=0;i<ntest;i++)cout<<"#"<<i+1<<" "<<sol[i]<<endl;return0;}