// Time 0ms, Memory 356K
#include #include #include using namespace std;int n,m,p[9][9],sx,sy,dx[]={0,1,0,-1},dy[]={1,0,-1,0};struct point{ int x,y,time,step; point(int x=0,int y=0,int time=0,int step=0):x(x),y(y),time(time),step(step){}};int bfs(){ point s(sx,sy,6,0),t; queue q; int nx,ny,i; q.push(s); while(!q.empty()) { s=q.front();q.pop(); for(i=0;i<4;i++) { nx=s.x+dx[i];ny=s.y+dy[i]; t=point(nx,ny,s.time-1,s.step+1); if(t.x<0 || t.x>=n || t.y<0 || t.y>=m || !p[nx][ny] || t.time==0) continue; if(p[nx][ny]==3) return t.step; if(p[nx][ny]==4) { t.time=6;p[nx][ny]=0; } q.push(t); } } return -1;}int main(){ int i,j,c; cin>>c; while(c--) { scanf("%d%d",&n,&m); for(i=0;i