Getting wrong answer : <p>So I am trying to solve a question on HackerEarth and I just don't get what is going wrong.. 11 out of 12 test cases are passed but one is failing..
Here is the question:</p>

<blockquote>
  <p>Stephen wants to explore the space, let's say which is of N rows and N
  columns. Each cell consist of a planet or black hole denoted with a
  character '.' or '#' respectively.  Stephen can only visit planets and
  he can't move outside the grid.</p>
  
  <p>Stephen wants to explore the unexplored space. So He follows the
  instructions of Dr. Hawking to explore the space. Dr. Hawking sends
  him a sequence of moves described by a string S of length L. Each
  character is one of 'U', 'D', 'R', 'L', denoting directions: up, down,
  right and left,  respectively.</p>
  
  <p>Depending on the starting cell, making all L moves might be
  impossible. Stephen considers each planet as a starting one and
  wonders how many moves in the sequence he can make, before being
  forced to stop. For example, if S starts with 'R' but a cell on the
  right from the starting cell is a black hole (or is outside the grid),
  Stephen would do 0 moves.</p>
  
  <p>Your task is to find the number of moves Stephen would do from each
  starting cell, and print the bitwise XOR of those numbers.</p>
  
  <p>Input The first line of the input contains an integer T denoting the
  number of test cases. The description of T test cases follows.</p>
  
  <p>The first line of each test case contains two integers L and N
  denoting the length of the sequence of moves and the size of the grid.</p>
  
  <p>The second line of a test case contains a string S denoting the
  sequence of moves.</p>
  
  <p>Next N lines describe the grid. The i-th line contains a string of
  length N denoting the i-th row of the grid.</p>
  
  <p>Output For each test case, output a single line containing one integer
  — the bitwise XOR of the number of moves made by Limak from each
  possible starting cell.</p>
  
  <p>Constraints 1 ≤ T ≤ 100,1 ≤ L ≤ 1000,1 ≤ N ≤ 1000</p>
</blockquote>

<p>Sample Input:</p>

<pre><code>2
3 4
DDU
#..#
#...
...#
..#.

10 4
RLLRDDLUUL
....
.#..
..#.
.#.#
</code></pre>

<p>Sample Output:</p>

<pre><code>2
3
</code></pre>

<p>Explanation:</p>

<pre><code>Test case 1. We are given the grid of size N = 4, and a sequence of L = 3 moves. For each empty cell of the grid, below you can see the number of moves Limak would make:

# 3 3 # 
# 3 1 0 
1 1 0 # 
0 0 # 0
The answer is 3 xor 3 xor 3 xor 1 xor 1 xor 1 = 2.

Test case 2. Again, below you can see the number of moves Limak would make from each cell:

2 4 5 0 
0 # 2 0 
2 0 # 0 
0 # 0 #
</code></pre>

<p>Here is my solution:</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;
char **a;
int l,n;
bool pos(int&amp; x,int&amp; y,char m)
{
    if(m=='D')x++;
    if(m=='U')x--;
    if(m=='L')y--;
    if(m=='R')y++;
    if(x&lt;0 || x&gt;=n || y&lt;0 || y&gt;=n)return false;
    if(a[x][y]=='#')return false;
    return true;
}
int val(int x,int y,string s)
{
    int t=0;
    for(int i=0;i&lt;s.length();i++)
    {
        if(pos(x,y,s[i]))t++;
        else break;
    }
    return t;
}
int main()
{
    int t;cin&gt;&gt;t;
    while(t--)
    {
        int ans=0;
        cin&gt;&gt;l&gt;&gt;n;
        a=new char*[n];
        for(int i=0;i&lt;n;i++)a[i]=new char[n];
        string s;cin&gt;&gt;s;
        for(int i=0;i&lt;n;i++)
            for(int j=0;j&lt;n;j++)
                cin&gt;&gt;a[i][j];
        for(int i=0;i&lt;n;i++)
        {
            for(int j=0;j&lt;n;j++)
            {
                if(a[i][j]=='.')
                {
                    ans=ans^val(i,j,s);
                }
            }
        }
        cout&lt;&lt;ans&lt;&lt;endl;
    }
}
</code></pre>

<p>Can anyone spot any error in my logic? The solution works for 11 out of 12 cases but fails for 1 and I just cant spot the issue.. Any help would be appreciated.</p>
