title: leetcode - number of islands, RecursionError
body: <p>I'm trying to solve the famous problem of &quot;number of islands&quot; from leetcode. (link : <a href="https://leetcode.com/problems/number-of-islands/" rel="nofollow noreferrer">https://leetcode.com/problems/number-of-islands/</a>)</p>
  <p>I solved this solution using BFS, but I got this error :
  &quot;RecursionError: maximum recursion depth exceeded in comparison&quot;</p>
  <p>Why I get this error?
  I cant figure why.</p>
  <p>Here is my code:</p>
  <pre><code>def numIslands(grid):    
      islands = 0
      for i in range(0, len(grid)):
          for j in range(0, len(grid[0])):
              if(grid[i][j] == &quot;1&quot;):
                  islands += 1
                  marksZero(grid, i, j)
  
      print(islands)           
      return islands
                      
                      
  def marksZero(grid, i, j):
  
      if(grid[i][j] == &quot;0&quot;):
          return
      
      grid[i][j] = 0
      if(i - 1 &gt;= 0):
          marksZero(grid, i - 1, j)
      if(i + 1 &lt; len(grid)):
          marksZero(grid, i + 1, j)
      if(j - 1 &gt;= 0):
          marksZero(grid, i, j - 1)
      if(j + 1 &lt; len(grid[0])):
          marksZero(grid, i, j + 1)
  
  numIslands([[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]])
  </code></pre>