import java.io.*; import java.util.*; public class slippy_yuizumi { public static void main(String[] args) throws Exception { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); for(int i = 0; i < n; i++) { Queue queue = Stage.initialize(cin); while(true) { Stage stage = queue.remove(); if(stage.enqueue(queue)) { System.out.println(stage.getCost()); break; } } } } private static final int[] dx = new int[] { +1, 0, -1, 0 }; private static final int[] dy = new int[] { 0, +1, 0, -1 }; private static class Stage implements Cloneable { private char[][] map; private int nx; private int ny; private int sx; private int sy; private int tx; private int ty; private int vx; private int vy; private Stage() { } private Stage placeSnowman(int x, int y) { Stage copy = (Stage)(clone()); copy.map[x][y] = '8'; return copy; } private Queue scan(Scanner cin) { ny = cin.nextInt(); nx = cin.nextInt(); map = new char[nx][ny]; for(int y = 0; y < ny; y++) { String s = cin.next(); for(int x = 0; x < nx; x++) { if(s.charAt(x) == 'A') { sx = x; sy = y; } if(s.charAt(x) == '_') { tx = x; ty = y; } map[x][y] = s.charAt(x); } } Queue queue = new LinkedList(); for(int i = 0; i < 4; i++) { int ux = dx[i]; int uy = dy[i]; if(map[sx+ux][sy+uy] == '_') { Stage copy = (Stage)(clone()); copy.vx = uy; // (vx,vy) * (ux,uy) == 0 copy.vy = ux; queue.offer(copy); } } return queue; } public Object clone() { Stage copy = new Stage(); copy.map = new char[nx][ny]; copy.nx = nx; copy.ny = ny; copy.sx = sx; copy.sy = sy; copy.tx = tx; copy.ty = ty; copy.vx = vx; copy.vy = vy; for(int x = 0; x < nx; x++) { System.arraycopy(map[x], 0, copy.map[x], 0, ny); } return copy; } public boolean enqueue(Queue queue) { while(true) { int dir = -1; for(int i = 0; i < 4; i++) { int ux = dx[i]; int uy = dy[i]; if(ux * vx + uy * vy != 0) { continue; } char mp = map[sx-ux][sy-uy]; char mq = map[sx+ux][sy+uy]; if("#8^".indexOf(mp) < 0) { if("#8^".indexOf(mq) < 0) { queue.offer(placeSnowman(sx-ux, sy-uy)); } continue; } if(mp != '^' && "#8^".indexOf(mq) < 0) dir = i; } if(dir < 0) { return false; } vx = dx[dir]; vy = dy[dir]; int sx0 = sx; // backup int sy0 = sy; // backup while(true) { char mp = map[sx ][sy ]; char mq = map[sx+vx][sy+vy]; if(mp == '>') { return true; } if(mq == '^') { return false; } if(mq == '#' || mq == '8') break; if(sx != sx0 || sy != sy0) { if(mq != 'A') { queue.offer(placeSnowman(sx+vx, sy+vy)); } } map[sx][sy] = 'A'; sx += vx; sy += vy; } if(map[sx][sy] == 'A') { return false; } map[sx][sy] = 'A'; } } public int getCost() { int c = 0; for(int y = 0; y < ny; y++) { for(int x = 0; x < nx; x++) { c += (map[x][y] == '8') ? 1 : 0; } } return c; } public static Queue initialize(Scanner cin) { return (new Stage()).scan(cin); } public String toString() { StringBuffer sb = new StringBuffer(); for(int y = 0; y < ny; y++) { for(int x = 0; x < nx; x++) { if(x == sx && y == sy) sb.append("NW.ES".charAt(vx + vy * 2 + 2)); else sb.append(map[x][y]); } sb.append('\n'); } return sb.toString(); } } }