SIMPLE BFS solution | |  Connected Components | | C++
\n# Approach\nSame as island problem connected component. Using BFS traverse all the components and return the maximum sum of the the components.\n Describe you
\n# Approach\nSame as island problem connected component. Using BFS traverse all the components and return the maximum sum of the the components.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: o(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N*M)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n   /*  dfs(int row,int col,vector<vector<int>>&vis,cnt){\n        vis[row][col]=1;\n        cnt+=grid[row][col];\n        \n    }*/\n    int findMaxFish(vector<vector<int>>& grid) {\n        int cnt = 0;\n        int m = grid.size();\n        int n = grid[0].size();\nvector<vector<int>>vis;\n        for (int i = 0; i < m; i++)\n        {\n            for (int j = 0; j < n; j++)\n            {\n                if (grid[i][j] > 0)\n                {\n                    queue<pair<int, int>> q;\n                    q.push({i, j});\n                    int fish = grid[i][j];\n                    grid[i][j] = 0;\n\n                    while (!q.empty())\n                    {\n                        int row = q.front().first;\n                        int col = q.front().second;\n                        q.pop();\n\n                        if (row > 0 && grid[row - 1][col] > 0)\n                        {\n                            fish += grid[row - 1][col];\n                            q.push({row - 1, col});\n                            grid[row - 1][col] = 0;\n                        }\n                        if (row < m - 1 && grid[row + 1][col] > 0)\n                        {\n                            fish += grid[row + 1][col];\n                            q.push({row + 1, col});\n                            grid[row + 1][col] = 0;\n                        }\n                        if (col > 0 && grid[row][col - 1] > 0)\n                        {\n                            fish += grid[row][col - 1];\n                            q.push({row, col - 1});\n                            grid[row][col - 1] = 0;\n                        }\n                        if (col < n - 1 && grid[row][col + 1] > 0)\n                        {\n                            fish += grid[row][col + 1];\n                            q.push({row, col + 1});\n                            grid[row][col + 1] = 0;\n                        }\n                    }\n\n                    cnt = max(cnt, fish);\n                              grid[i][j] = fish;\n                }\n            }\n        }\n\n        return cnt;\n    }\n};\n```
['C++']