695. Max Area of Island

695. Max Area of Island

class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        
        rows = len(grid)
        cols = len(grid[0])
        
        directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
        
        def dfs(row, col):
            grid[row][col] = "#"
            count = 1
            for dr, dc in directions:
                next_row = row + dr
                next_col = col + dc
                if 0 <= next_row < rows and 0 <= next_col < cols and grid[next_row][next_col] == 1:
                    count += dfs(next_row, next_col)
            return count
        
        
        res = 0
        for row in range(rows):
            for col in range(cols):
                if grid[row][col] == 1:
                    count = dfs(row, col)
                    res = max(res, count)
        
        return res