#include <stdio.h>
char map[21][21];
int visit[21][21];
int r, c;
int dfs(int x, int y,int direction, int memory, int count) {//0 오른 1 아래 2 왼 3위 count->dfs호출횟수
// printf("%d %d %d\n", x,y, memory);
visit[x][y]++;
if (map[x][y] == '@') {
return 1;
}
else if (visit[x][y] > 500) {
return 0;
}
else if (count > 3000) {//일정 수 이상 dfs 호출되면 탈출 불가능
return 0;
}
else if (map[x][y] == '<') {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
else if (map[x][y] == '>') {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else if (map[x][y] == '^') {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
else if (map[x][y] == 'v') {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else if (map[x][y] == '_') {
if (memory == 0) {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
}
else if (map[x][y] == '|') {
if (memory == 0) {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
}
else if (map[x][y] == '?') {//4방향 다 해봐야함
int check = 0;
int result = 0;
if (y + 1 >= c)
{
y = -1;
check = 1;
}
result = dfs(x, y + 1, 0, memory, count+1);
if (check == 1) {
y = c - 1;
}
if (result == 1) {
return result;
}
check = 0;
if (x + 1 >= r)
{
x = -1;
check = 1;
}
result = dfs(x + 1, y, 1, memory, count+1);
if (check == 1) {
x = r - 1;
}
if (result == 1) {
return result;
}
check = 0;
if (y - 1 < 0)
{
y = c;
check = 1;
}
result = dfs(x, y - 1, 2, memory, count+1);
if (check == 1) {
y = 0;
}
if (result == 1) {
return result;
}
check = 0;
if (x - 1 < 0)
{
x = r;
check = 1;
}
result = dfs(x - 1, y, 3, memory, count+1);
if (result == 1) {
return result;
}
}
else if (map[x][y] == '.') {
if (direction == 0) {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else if (direction == 1) {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else if (direction == 2) {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
else if (direction == 3) {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
}
else if (map[x][y] >= '0' && map[x][y] <= '9') {
memory = map[x][y] - '0';
if (direction == 0) {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else if (direction == 1) {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else if (direction == 2) {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
else if (direction == 3) {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
}
else if (map[x][y] == '+') {
if (memory == 15)
memory = 0;
else
memory++;
if (direction == 0) {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else if (direction == 1) {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else if (direction == 2) {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
else if (direction == 3) {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
}
else if (map[x][y] == '-') {
if (memory == 0)
memory = 15;
else
memory--;
if (direction == 0) {
if (y + 1 >= c)
y = -1;
return dfs(x, y + 1, 0, memory, count+1);
}
else if (direction == 1) {
if (x + 1 >= r)
x = -1;
return dfs(x + 1, y, 1, memory, count+1);
}
else if (direction == 2) {
if (y - 1 < 0)
y = c;
return dfs(x, y - 1, 2, memory, count+1);
}
else if (direction == 3) {
if (x - 1 < 0)
x = r;
return dfs(x - 1, y, 3, memory, count+1);
}
}
return 0;
}
int main(void) {
int T;
scanf("%d\n", &T);
for (int testCase = 1; testCase <= T; testCase++) {
scanf("%d %d\n", &r,&c);
int check = 0;
for (int i = 0; i < 21; i++) {
for (int j = 0; j < 21; j++) {
map[i][j] = 0;
visit[i][j] = 0;
}
}
for (int i = 0; i < r; i++) {
scanf("%s", map[i]);
for (int j = 0; check==0 && j < c; j++) {
if (map[i][j] == '@') {
check = 1;
break;
}
}
}
printf("#%d", testCase);
if (!check) {
printf(" NO\n");
continue;
}
int result = dfs(0, 0, 0, 0,0);
if (result == 1) {
printf(" YES\n");
}
else {
printf(" NO\n");
}
}
return 0;
}