#include using namespace std; char tmp_char[1000123]; string read() { scanf("%s", tmp_char); return string(tmp_char); } void te() { int L, n; scanf("%d%d", &L, &n); string moves = read(); vector grid(n); for(int row = 0; row < n; ++row) grid[row] = read(); vector> ans(n, vector(n, L)); // ans[row][col] should be the number of moves if we start from (row,col) auto isBlocked = [&] (int row, int col) { return min(row, col) < 0 || max(row, col) >= n || grid[row][col] == '#'; }; for(int MAGIC = L; MAGIC <= L; MAGIC++) { for(int row = -1; row <= n; ++row) for(int col = -1; col <= n; ++col) if(isBlocked(row, col)) { pair memo = {row, col}; for(int i = 0; i < min(MAGIC, L); ++i) { // we make opposite moves if(moves[i] == 'U') ++row; else if(moves[i] == 'D') --row; else if(moves[i] == 'L') ++col; else if(moves[i] == 'R') --col; else assert(false); // if(isBlocked(row, col)) break; // wrong if(!isBlocked(row, col)) ans[row][col] = min(ans[row][col], i); } row = memo.first, col = memo.second; } bool correct = true; for(int row = 0; row < n; ++row) for(int col = 0; col < n; ++col) if(!isBlocked(row, col) && ans[row][col] >= MAGIC) correct = false; if(correct) break; } int total = 0; for(int row = 0; row < n; ++row) for(int col = 0; col < n; ++col) if(!isBlocked(row, col)) total ^= ans[row][col]; printf("%d\n", total); } int main() { int T; scanf("%d", &T); while(T--) te(); }