Easy to understand [ Clean Solution ] DFS -> (python + java)
T.C : O(n^2)\n\nPython\n\nclass Solution:\n    def findMaxFish(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n        fishes = 0\
**T.C : O(n^2)**\n\n**Python**\n```\nclass Solution:\n    def findMaxFish(self, grid: List[List[int]]) -> int:\n        m, n = len(grid), len(grid[0])\n        fishes = 0\n\n        def catchFish(r, c):\n            nonlocal fish\n            if 0 <= r < m and 0 <= c < n and grid[r][c] > 0:\n                fish += grid[r][c]\n                grid[r][c] = 0  # mark cell as visited\n                catchFish(r+1, c)\n                catchFish(r-1, c)\n                catchFish(r, c+1)\n                catchFish(r, c-1)\n\n        for r in range(m):\n            for c in range(n):\n                if grid[r][c] > 0:\n                    fish = 0\n                    catchFish(r, c)\n                    fishes = max(fishes, fish)\n\n        return fishes\n```\n\n**Java**\n\n```\npublic class Solution {\n    public int findMaxFish(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int fishes = 0;\n\n        // Helper function to catch fish recursively\n        // Updates the \'fishes\' variable as it catches fish\n        void catchFish(int r, int c) {\n            if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] > 0) {\n                fishes += grid[r][c];\n                grid[r][c] = 0; // mark cell as visited\n                catchFish(r+1, c);\n                catchFish(r-1, c);\n                catchFish(r, c+1);\n                catchFish(r, c-1);\n            }\n        }\n\n        for (int r = 0; r < m; r++) {\n            for (int c = 0; c < n; c++) {\n                if (grid[r][c] > 0) {\n                    int fish = 0;\n                    catchFish(r, c);\n                    fishes = Math.max(fishes, fish);\n                }\n            }\n        }\n\n        return fishes;\n    }\n}\n
['Python', 'Java']