 <p>I think you forget array like this:</p> <pre><code>1 0 0 9 1 1 0 1 0 1 1 1 0 0 0 1 </code></pre> <p>your recursive function will not be finished in the previous example, please check the size of array to finish recursion.</p> <p>Also your algorithm does not use up and right to find cheese</p> <p>EDIT</p> <p>you should handle the 4 directions, and don't repeat the square you visit before, I will add pseudo code here to explain the main idea of solution:</p> <pre><code>public class MAZE { static int x, y; static boolean result = false; public static void main(String[] args) { int [][] matrix =new int[5][];// fill your array // fill x and y // x= cheese x value // y = cheese y value isPath(matrix, x, x); // print result } static void isPath(int[][] matrix, int i, int j) { if (i - 1 &gt; -1 &amp;&amp; matrix[i - 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i-1][j] // checkSPath(subarray1,i-1,j); } if (j - 1 &gt; -1 &amp;&amp; matrix[i][j - 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j-1] // checkLPath(subarray1,i,j-1); } if (i + 1 &lt; x &amp;&amp; matrix[i + 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i+1][j] // checkNPath(subarray1,i+1,j); } if (j + 1 &lt; y &amp;&amp; matrix[i][j + 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j+1] // checkRPath(subarray1,i,j+1); } } static void checkRPath(int[][] matrix, int i, int j) { if (i == 0 &amp;&amp; j == 0) { result = true; } else { if (i - 1 &gt; -1 &amp;&amp; matrix[i - 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i-1][j] // checkSPath(subarray1,i-1,j); } if (i + 1 &lt; x &amp;&amp; matrix[i + 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i+1][j] // checkNPath(subarray1,i+1,j); } if (j + 1 &lt; y &amp;&amp; matrix[i][j + 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j+1] // checkRPath(subarray1,i,j+1); } } } static void checkLPath(int[][] matrix, int i, int j) { if (i == 0 &amp;&amp; j == 0) { result = true; } else { if (i - 1 &gt; -1 &amp;&amp; matrix[i - 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i-1][j] // checkSPath(subarray1,i-1,j); } if (j - 1 &gt; -1 &amp;&amp; matrix[i][j - 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j-1] // checkLPath(subarray1,i,j-1); } if (i + 1 &lt; x &amp;&amp; matrix[i + 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i+1][j] // checkNPath(subarray1,i+1,j); } } } static void checkNPath(int[][] matrix, int i, int j) { if (i == 0 &amp;&amp; j == 0) { result = true; } else { if (j - 1 &gt; -1 &amp;&amp; matrix[i][j - 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j-1] // checkLPath(subarray1,i,j-1); } if (i + 1 &lt; x &amp;&amp; matrix[i + 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i+1][j] // checkNPath(subarray1,i+1,j); } if (j + 1 &lt; y &amp;&amp; matrix[i][j + 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j+1] // checkRPath(subarray1,i,j+1); } } } static void checkSPath(int[][] matrix, int i, int j) { if (i == 0 &amp;&amp; j == 0) { result = true; } else { if (i - 1 &gt; -1 &amp;&amp; matrix[i - 1][j] == 1) { // subarray1 = copy starting from [0][0] to matrix[i-1][j] // checkSPath(subarray1,i-1,j); } if (j - 1 &gt; -1 &amp;&amp; matrix[i][j - 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j-1] // checkLPath(subarray1); } if (j + 1 &lt; y &amp;&amp; matrix[i][j + 1] == 1) { // subarray1 = copy starting from [0][0] to matrix[i][j+1] // checkRPath(subarray1); } } } } </code></pre> <p>You can merge these 5 methods in one. here I am trying to explain the solution not writing optimized code.</p>