title: Find area of largest island (area not being returned correctly)
body: <p>I am working on <a href="https://practice.geeksforgeeks.org/problems/length-of-largest-region-of-1s-1587115620/1#" rel="nofollow noreferrer">this</a> GeeksForGeeks problem that is looking for the area of the largest connected &quot;island&quot; of 1's in a 2D grid (details in question). I have a solution that works in some, but not all cases. My general idea is this:</p>
  <ol> <li> Iterate through, row by row, until you find a 1. </li><li> Upon finding a 1, use the helper method to find the size of the island containing the 1.</li> <li>Return the biggest island found in steps 1 and 2.</li></ol>
  <p>To calculate the size of an island, I recursively search each connected element (there are 8 of them), and flip each 1 to 0 when it has been searched.</p>
  <p>I know the following are true:</p>
  <ul>
  <li>The islands are found in the right order</li>
  <li>Each island is summed only once (as it should be)</li>
  <li>Each 1 within an island is visited exactly once (again, as expected)</li>
  </ul>
  <p>But for whatever reason, despite visiting all the 1's within an island, the sum of these islands are not properly returned. I'm looking for help to figure out why. I have tried examining each step of the process using print statements etc., But still cannot find out what's wrong.As an example, on this grid</p>
  <pre><code>0 0 1 0 1 0 
  0 0 0 0 0 1
  0 0 0 0 1 1
  0 1 0 0 0 0
  0 0 0 0 0 0
  0 1 1 0 0 0
  0 1 0 0 0 1
  1 1 1 0 0 0
  1 0 1 0 0 0
  </code></pre>
  <p>my code returns 3, but it should return 8. Can anyone see my mistake? My code is attached below.</p>
  <pre><code>class Solution:
  
      #Function to find unit area of the largest region of 1s.
      def findMaxArea(self, grid):
          curr_max = 0
          for i in range(0, len(grid)):
              for j in range(0, len(grid[i])):
                  if grid[i][j] == 0:
                      continue
                  else:
                      curr_island_size = self.helper(i, j, grid)
                      if curr_island_size &gt; curr_max:
                          curr_max = curr_island_size
          return curr_max
      
      def helper(self, row, column, grid):
          if row == -1 or column == -1:
              return 0 # outside of grid 
          elif column &gt;= len(grid[0]) or row &gt;= len(grid):
              return 0 # outside of grid
          elif grid[row][column] == 0:
              return 0
          else:
              grid[row][column] = 0
              up_left = self.helper(row - 1, column - 1, grid)
              mid_left = self.helper(row, column - 1, grid)
              down_left = self.helper(row + 1, column - 1, grid)
              up = self.helper(row - 1, column, grid)
              down = self.helper(row + 1, column, grid)
              up_right = self.helper(row - 1, column + 1, grid)
              mid_right = self.helper(row, column + 1, grid)
              down_right = self.helper(row + 1, column + 1, grid) 
              return 1 + sum([up_left, mid_left, up, down, up_right, mid_right, down_right])
  </code></pre>