Connected Components in Matrix
BFS & DFS Problems DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Connected Components Problem in Matrix, Connected Components, Matrices, Two-dimensional Array Traversal, Graph Traversal, Depth-First Search, Breadth-First Search, Recursion, Queue, Time Complexity, Space Complexity, matrix representation, union-find, dfs, graph traversal, time complexity analysis, graph properties, bfs, Disjoint Set Union (DSU) / Union-Find, Connected Components.
Count Islands Problem Statement Given an m x n binary matrix grid (represented by 1s for land and 0s for water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Input Specification The first line contains two integers m and n, representing the number of rows and columns of the grid, respectively. Following m lines each contain n characters ('0' or '1'), representing the binary matrix. Output Specification Return a single integer, the total number of islands. Constraints 1 <= m, n <= 300 grid[i][j] is '0' or '1'.