The Wayback Machine - https://web.archive.org/web/20241006000112/https://www.geeksforgeeks.org/find-number-of-closed-islands-in-given-matrix/
Open In App

Find number of closed islands in given Matrix

Last Updated : 11 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Hard
61.43%
26.1K

Given a binary matrix mat[][] of dimensions NxM such that 1 denotes the island and 0 denotes the water. The task is to find the number of closed islands in the given matrix. 

A closed island is known as the group of 1s that is surrounded by only 0s on all the four sides (excluding diagonals). If any 1 is at the edges of the given matrix then it is not considered as the part of the connected island as it is not surrounded by all 0. 
 

Examples: 

Input: N = 5, M = 8, 
mat[][] = 
{{0, 0, 0, 0, 0, 0, 0, 1}, 
{0, 1, 1, 1, 1, 0, 0, 1}, 
{0, 1, 0, 1, 0, 0, 0, 1}, 
{0, 1, 1, 1, 1, 0, 1, 0}, 
{0, 0, 0, 0, 0, 0, 0, 1}} 
Output: 2 
Explanation: 
mat[][] = 
{{0, 0, 0, 0, 0, 0, 0, 1}, 
{0, 1, 1, 1, 1, 0, 0, 1}, 
{0, 1, 0, 1, 0, 0, 0, 1}, 
{0, 1, 1, 1, 1, 0, 1, 0}, 
{0, 0, 0, 0, 0, 0, 0, 1}} 
There are 2 closed islands. 
The islands in dark are closed because they are completely surrounded by 
0s (water). 
There are two more islands in the last column of the matrix, but they are not completely surrounded by 0s. 
Hence they are not closed islands.

Input: N = 3, M = 3, matrix[][] = 
{{1, 0, 0}, 
{0, 1, 0}, 
{0, 0, 1}} 
Output: 1

Recommended Practice

Method 1 – using DFS Traversal: The idea is to use DFS Traversal to count the number of island surrounded by water. But we have to keep the track of the island at the corner of the given matrix as they will not be counted in the resultant island. Below are the steps: 

  1. Initialize a 2D visited matrix(say vis[][]) to keep the track of traversed cell in the given matrix.
  2. Perform DFS Traversal on all the corner of the given matrix and if any element has value 1 then marked all the cell with value 1 as visited because it cannot be counted in the resultant count.
  3. Perform DFS Traversal on all the remaining unvisited cell and if value encountered is 1 then marked this cell as visited, count this island in the resultant count and recursively call DFS for all the 4 directions i.e., left, right, top, and bottom to make all the 1s connected to the current cell as visited.
  4. Repeat the above step until all cell with value 1 are not visited.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// DFS Traversal to find the count of
// island surrounded by water
void dfs(vector<vector<int> >& matrix,
         vector<vector<bool> >& visited, int x, int y,
         int n, int m)
{
    // If the land is already visited
    // or there is no land or the
    // coordinates gone out of matrix
    // break function as there
    // will be no islands
    if (x < 0 || y < 0 || x >= n || y >= m
        || visited[x][y] == true || matrix[x][y] == 0)
        return;
 
    // Mark land as visited
    visited[x][y] = true;
 
    // Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m);
    dfs(matrix, visited, x, y + 1, n, m);
    dfs(matrix, visited, x - 1, y, n, m);
    dfs(matrix, visited, x, y - 1, n, m);
}
 
// Function that counts the closed island
int countClosedIsland(vector<vector<int> >& matrix, int n,
                      int m)
{
 
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    vector<vector<bool> > visited(n,
                                  vector<bool>(m, false));
 
    // Mark visited all lands
    // that are reachable from edge
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
 
            // Traverse corners
            if ((i * j == 0 || i == n - 1 || j == m - 1)
                and matrix[i][j] == 1
                and visited[i][j] == false)
                dfs(matrix, visited, i, j, n, m);
        }
    }
 
    // To stores number of closed islands
    int result = 0;
 
    for (int i = 0; i < n; ++i) {
 
        for (int j = 0; j < m; ++j) {
 
            // If the land not visited
            // then there will be atleast
            // one closed island
            if (visited[i][j] == false
                and matrix[i][j] == 1) {
 
                result++;
 
                // Mark all lands associated
                // with island visited.
                dfs(matrix, visited, i, j, n, m);
            }
        }
    }
 
    // Return the final count
    return result;
}
 
// Driver Code
int main()
{
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    vector<vector<int> > matrix
        = { { 0, 0, 0, 0, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 0, 1 },
            { 0, 1, 0, 1, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 1, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
    // Function Call
    cout << countClosedIsland(matrix, N, M);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG {
 
    // DFS Traversal to find the count of
    // island surrounded by water
    static void dfs(int[][] matrix, boolean[][] visited,
                    int x, int y, int n, int m)
    {
        // If the land is already visited
        // or there is no land or the
        // coordinates gone out of matrix
        // break function as there
        // will be no islands
        if (x < 0 || y < 0 || x >= n || y >= m
            || visited[x][y] == true || matrix[x][y] == 0)
            return;
 
        // Mark land as visited
        visited[x][y] = true;
 
        // Traverse to all adjacent elements
        dfs(matrix, visited, x + 1, y, n, m);
        dfs(matrix, visited, x, y + 1, n, m);
        dfs(matrix, visited, x - 1, y, n, m);
        dfs(matrix, visited, x, y - 1, n, m);
    }
 
    // Function that counts the closed island
    static int countClosedIsland(int[][] matrix, int n,
                                 int m)
    {
 
        // Create boolean 2D visited matrix
        // to keep track of visited cell
 
        // Initially all elements are
        // unvisited.
        boolean[][] visited = new boolean[n][m];
 
        // Mark visited all lands
        // that are reachable from edge
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
 
                // Traverse corners
                if ((i * j == 0 || i == n - 1 || j == m - 1)
                    && matrix[i][j] == 1
                    && visited[i][j] == false)
                    dfs(matrix, visited, i, j, n, m);
            }
        }
 
        // To stores number of closed islands
        int result = 0;
 
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
 
                // If the land not visited
                // then there will be atleast
                // one closed island
                if (visited[i][j] == false
                    && matrix[i][j] == 1) {
                    result++;
 
                    // Mark all lands associated
                    // with island visited.
                    dfs(matrix, visited, i, j, n, m);
                }
            }
        }
 
        // Return the final count
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given size of Matrix
        int N = 5, M = 8;
 
        // Given Matrix
        int[][] matrix = { { 0, 0, 0, 0, 0, 0, 0, 1 },
                           { 0, 1, 1, 1, 1, 0, 0, 1 },
                           { 0, 1, 0, 1, 0, 0, 0, 1 },
                           { 0, 1, 1, 1, 1, 0, 1, 0 },
                           { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
        // Function Call
        System.out.print(countClosedIsland(matrix, N, M));
    }
}
 
// This code is contributed by Rohit_ranjan


Python3




# Python3 program for the above approach
 
# DFS Traversal to find the count of
# island surrounded by water
def dfs(matrix, visited, x, y, n, m):
     
    # If the land is already visited
    # or there is no land or the
    # coordinates gone out of matrix
    # break function as there
    # will be no islands
    if (x < 0 or y < 0 or
        x >= n or y >= m or
        visited[x][y] == True or
        matrix[x][y] == 0):
        return
         
    # Mark land as visited
    visited[x][y] = True
 
    # Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m);
    dfs(matrix, visited, x, y + 1, n, m);
    dfs(matrix, visited, x - 1, y, n, m);
    dfs(matrix, visited, x, y - 1, n, m);
 
# Function that counts the closed island
def countClosedIsland(matrix, n, m):
     
    # Create boolean 2D visited matrix
    # to keep track of visited cell
  
    # Initially all elements are
    # unvisited.
    visited = [[False for i in range(m)]
                      for j in range(n)]
 
    # Mark visited all lands
    # that are reachable from edge
    for i in range(n):
        for j in range(m):
             
            # Traverse corners
            if ((i * j == 0 or i == n - 1 or
                 j == m - 1) and matrix[i][j] == 1 and
                 visited[i][j] == False):
                dfs(matrix, visited, i, j, n, m)
 
    # To stores number of closed islands
    result = 0
 
    for i in range(n):
        for j in range(m):
             
            # If the land not visited
            # then there will be atleast
            # one closed island
            if (visited[i][j] == False and
                 matrix[i][j] == 1):
                result += 1
                 
                # Mark all lands associated
                # with island visited.
                dfs(matrix, visited, i, j, n, m)
 
    # Return the final count
    return result
 
#  Driver Code
 
# Given size of Matrix
N = 5
M = 8
 
# Given Matrix
matrix = [ [ 0, 0, 0, 0, 0, 0, 0, 1 ],
           [ 0, 1, 1, 1, 1, 0, 0, 1 ],
           [ 0, 1, 0, 1, 0, 0, 0, 1 ],
           [ 0, 1, 1, 1, 1, 0, 1, 0 ],
           [ 0, 0, 0, 0, 0, 0, 0, 1 ] ]
            
# Function Call
print(countClosedIsland(matrix, N, M))
 
# This code is contributed by rag2127


C#




// C# program for the above approach
using System;
 
class GFG {
 
    // DFS Traversal to find the count of
    // island surrounded by water
    static void dfs(int[, ] matrix, bool[, ] visited, int x,
                    int y, int n, int m)
    {
 
        // If the land is already visited
        // or there is no land or the
        // coordinates gone out of matrix
        // break function as there
        // will be no islands
        if (x < 0 || y < 0 || x >= n || y >= m
            || visited[x, y] == true || matrix[x, y] == 0)
            return;
 
        // Mark land as visited
        visited[x, y] = true;
 
        // Traverse to all adjacent elements
        dfs(matrix, visited, x + 1, y, n, m);
        dfs(matrix, visited, x, y + 1, n, m);
        dfs(matrix, visited, x - 1, y, n, m);
        dfs(matrix, visited, x, y - 1, n, m);
    }
 
    // Function that counts the closed island
    static int countClosedIsland(int[, ] matrix, int n,
                                 int m)
    {
 
        // Create bool 2D visited matrix
        // to keep track of visited cell
 
        // Initially all elements are
        // unvisited.
        bool[, ] visited = new bool[n, m];
 
        // Mark visited all lands
        // that are reachable from edge
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
 
                // Traverse corners
                if ((i * j == 0 || i == n - 1 || j == m - 1)
                    && matrix[i, j] == 1
                    && visited[i, j] == false)
                    dfs(matrix, visited, i, j, n, m);
            }
        }
 
        // To stores number of closed islands
        int result = 0;
 
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
 
                // If the land not visited
                // then there will be atleast
                // one closed island
                if (visited[i, j] == false
                    && matrix[i, j] == 1) {
                    result++;
 
                    // Mark all lands associated
                    // with island visited.
                    dfs(matrix, visited, i, j, n, m);
                }
            }
        }
 
        // Return the readonly count
        return result;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
 
        // Given size of Matrix
        int N = 5, M = 8;
 
        // Given Matrix
        int[, ] matrix = { { 0, 0, 0, 0, 0, 0, 0, 1 },
                           { 0, 1, 1, 1, 1, 0, 0, 1 },
                           { 0, 1, 0, 1, 0, 0, 0, 1 },
                           { 0, 1, 1, 1, 1, 0, 1, 0 },
                           { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
        // Function call
        Console.Write(countClosedIsland(matrix, N, M));
    }
}
 
// This code is contributed by amal kumar choubey


Javascript




<script>
 
    // JavaScript program for the above approach
     
    // DFS Traversal to find the count of
    // island surrounded by water
    function dfs(matrix, visited, x, y, n, m)
    {
        // If the land is already visited
        // or there is no land or the
        // coordinates gone out of matrix
        // break function as there
        // will be no islands
        if (x < 0 || y < 0 || x >= n || y >= m
            || visited[x][y] == true || matrix[x][y] == 0)
            return;
  
        // Mark land as visited
        visited[x][y] = true;
  
        // Traverse to all adjacent elements
        dfs(matrix, visited, x + 1, y, n, m);
        dfs(matrix, visited, x, y + 1, n, m);
        dfs(matrix, visited, x - 1, y, n, m);
        dfs(matrix, visited, x, y - 1, n, m);
    }
  
    // Function that counts the closed island
    function countClosedIsland(matrix, n, m)
    {
  
        // Create boolean 2D visited matrix
        // to keep track of visited cell
  
        // Initially all elements are
        // unvisited.
        let visited = new Array(n);
        for (let i = 0; i < n; ++i)
        {
            visited[i] = new Array(m);
            for (let j = 0; j < m; ++j)
            {
                visited[i][j] = false;
            }
        }
         
  
        // Mark visited all lands
        // that are reachable from edge
        for (let i = 0; i < n; ++i) {
            for (let j = 0; j < m; ++j) {
  
                // Traverse corners
                if ((i * j == 0 || i == n - 1 || j == m - 1)
                    && matrix[i][j] == 1
                    && visited[i][j] == false)
                    dfs(matrix, visited, i, j, n, m);
            }
        }
  
        // To stores number of closed islands
        let result = 0;
  
        for (let i = 0; i < n; ++i) {
            for (let j = 0; j < m; ++j) {
  
                // If the land not visited
                // then there will be atleast
                // one closed island
                if (visited[i][j] == false
                    && matrix[i][j] == 1) {
                    result++;
  
                    // Mark all lands associated
                    // with island visited.
                    dfs(matrix, visited, i, j, n, m);
                }
            }
        }
  
        // Return the final count
        return result;
    }
     
    // Given size of Matrix
    let N = 5, M = 8;
 
    // Given Matrix
    let matrix = [ [ 0, 0, 0, 0, 0, 0, 0, 1 ],
      [ 0, 1, 1, 1, 1, 0, 0, 1 ],
      [ 0, 1, 0, 1, 0, 0, 0, 1 ],
      [ 0, 1, 1, 1, 1, 0, 1, 0 ],
      [ 0, 0, 0, 0, 0, 0, 0, 1 ] ];
 
    // Function Call
    document.write(countClosedIsland(matrix, N, M));
 
</script>


Output

2

Time Complexity: O(N*M) 
Auxiliary Space: O(N*M) 

Method: Single DFS Traversal

Improvement over Method 1: In the above Method 1, we see that we are calling DFS traversal twice (Once over the corner cells with ‘1’ and afterward over the cells which are not on the corners with ‘1’ and are not visited). We can solve this using only 1 DFS traversal. the idea is to call DFS for the cells with value ‘1’  which are not on the corners and while doing so, if we find a cell with value ‘1’ on the corner, then that means it should not be counted as an island. The code is shown below:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// DFS Traversal to find the count of
// island surrounded by water
void dfs(vector<vector<int> >& matrix,
         vector<vector<bool> >& visited, int x, int y,
         int n, int m, bool &hasCornerCell;)
{
    // If the land is already visited
    // or there is no land or the
    // coordinates gone out of matrix
    // break function as there
    // will be no islands
    if (x < 0 || y < 0 || x >= n || y >= m
        || visited[x][y] == true || matrix[x][y] == 0)
        return;
 
      // Check for the corner cell
    if(x == 0 || y == 0 || x == n-1 || y == m-1)
    {
      if(matrix[x][y] == 1)
        hasCornerCell = true;
    }
   
    // Mark land as visited
    visited[x][y] = true;
 
    // Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m, hasCornerCell);
    dfs(matrix, visited, x, y + 1, n, m, hasCornerCell);
    dfs(matrix, visited, x - 1, y, n, m, hasCornerCell);
    dfs(matrix, visited, x, y - 1, n, m, hasCornerCell);
}
 
// Function that counts the closed island
int countClosedIsland(vector<vector<int> >& matrix, int n,
                      int m)
{
 
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    vector<vector<bool>> visited(n,vector<bool>(m, false));
 
    // Store the count of islands
    int result = 0; 
   
    // Call DFS on the cells which
    // are not on corners with value '1'
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
 
            if ((i != 0 && j != 0 && i != n - 1 && j != m - 1)
                and matrix[i][j] == 1
                and visited[i][j] == false)
            {
               
                // Determine if the island is closed
                  bool hasCornerCell = false;
                   
                /* hasCornerCell will be
                 updated to true while DFS traversal
                if there is a cell with value
                 '1' on the corner */
                dfs(matrix, visited, i, j, n,
                              m, hasCornerCell);
                 
                /* If the island is closed*/
                  if(!hasCornerCell)
                  result = result + 1;
            }
        }
    }
 
    // Return the final count
    return result;
}
 
// Driver Code
int main()
{
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    vector<vector<int> > matrix
        = { { 0, 0, 0, 0, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 0, 1 },
            { 0, 1, 0, 1, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 1, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
    // Function Call
    cout << countClosedIsland(matrix, N, M);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// DFS Traversal to find the count of
// island surrounded by water
static void dfs(int[][] matrix, boolean[][] visited,
                int x, int y, int n, int m,
                boolean hasCornerCell)
{
     
    // If the land is already visited
    // or there is no land or the
    // coordinates gone out of matrix
    // break function as there
    // will be no islands
    if (x < 0 || y < 0 || x >= n || y >= m ||
        visited[x][y] == true || matrix[x][y] == 0)
        return;
 
    if (x == 0 || y == 0 ||
        x == n - 1 || y == m - 1)
    {
        if (matrix[x][y] == 1)
            hasCornerCell = true;
    }
 
    // Mark land as visited
    visited[x][y] = true;
 
    // Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m,
        hasCornerCell);
    dfs(matrix, visited, x, y + 1, n, m,
        hasCornerCell);
    dfs(matrix, visited, x - 1, y, n, m,
        hasCornerCell);
    dfs(matrix, visited, x, y - 1, n, m,
        hasCornerCell);
}
 
// Function that counts the closed island
static int countClosedIsland(int[][] matrix, int n,
                             int m)
{
     
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    boolean[][] visited = new boolean[n][m];
    int result = 0;
     
    // Mark visited all lands
    // that are reachable from edge
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < m; ++j)
        {
            if ((i != 0 && j != 0 &&
                 i != n - 1 && j != m - 1) &&
                 matrix[i][j] == 1 &&
                 visited[i][j] == false)
            {
 
                // Determine if the island is closed
                boolean hasCornerCell = false;
 
                // hasCornerCell will be updated to
                // true while DFS traversal if there
                // is a cell with value '1' on the corner
                dfs(matrix, visited, i, j, n, m,
                    hasCornerCell);
 
                // If the island is closed
                if (!hasCornerCell)
                    result = result + 1;
            }
        }
    }
 
    // Return the final count
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    int[][] matrix = { { 0, 0, 0, 0, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 0, 1 },
                       { 0, 1, 0, 1, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 1, 0 },
                       { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
    // Function Call
    System.out.print(countClosedIsland(matrix, N, M));
}
}
 
// This code is contributed by grand_master


Python3




# Python3 program for the above approach
 
# DFS Traversal to find the count of
# island surrounded by water
def dfs(matrix, visited, x, y, n, m, hasCornerCell):
     
    # If the land is already visited
    # or there is no land or the
    # coordinates gone out of matrix
    # break function as there
    # will be no islands
    if (x < 0 or y < 0 or
        x >= n or y >= m or
        visited[x][y] == True or
         matrix[x][y] == 0):
        return
 
    if (x == 0 or y == 0 or
        x == n - 1 or y == m - 1):
        if (matrix[x][y] == 1):
            hasCornerCell = True
 
    # Mark land as visited
    visited[x][y] = True
 
    # Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m, hasCornerCell)
    dfs(matrix, visited, x, y + 1, n, m, hasCornerCell)
    dfs(matrix, visited, x - 1, y, n, m, hasCornerCell)
    dfs(matrix, visited, x, y - 1, n, m, hasCornerCell)
 
# Function that counts the closed island
def countClosedIsland(matrix, n, m):
     
    # Create boolean 2D visited matrix
    # to keep track of visited cell
 
    # Initially all elements are
    # unvisited.
    visited = [[False for i in range(m)]
                      for j in range(n)]
    result = 0
     
    # Mark visited all lands
    # that are reachable from edge
    for i in range(n):
        for j in range(m):
            if ((i != 0 and j != 0 and
                 i != n - 1 and j != m - 1) and
                 matrix[i][j] == 1 and
                visited[i][j] == False):
 
                # Determine if the island is closed
                hasCornerCell = False
 
                # hasCornerCell will be updated to
                # true while DFS traversal if there
                # is a cell with value '1' on the corner
                dfs(matrix, visited, i, j,
                    n, m, hasCornerCell)
 
                # If the island is closed
                if (not hasCornerCell):
                    result = result + 1
 
    # Return the final count
    return result
     
# Driver Code
 
# Given size of Matrix
N, M = 5, 8
 
# Given Matrix
matrix = [ [ 0, 0, 0, 0, 0, 0, 0, 1 ],
           [ 0, 1, 1, 1, 1, 0, 0, 1 ],
           [ 0, 1, 0, 1, 0, 0, 0, 1 ],
           [ 0, 1, 1, 1, 1, 0, 1, 0 ],
           [ 0, 0, 0, 0, 0, 0, 0, 1 ] ]
 
# Function Call
print(countClosedIsland(matrix, N, M))
 
# This code is contributed by divyeshrabadiya07


C#




// C# program for the above approach
using System;
class GFG
{
     
  // DFS Traversal to find the count of
  // island surrounded by water
  static void dfs(int[,] matrix, bool[,] visited,
                  int x, int y, int n, int m,
                  bool hasCornerCell)
  {
 
    // If the land is already visited
    // or there is no land or the
    // coordinates gone out of matrix
    // break function as there
    // will be no islands
    if (x < 0 || y < 0 || x >= n || y >= m ||
        visited[x, y] == true || matrix[x, y] == 0)
      return;
 
    if (x == 0 || y == 0 ||
        x == n - 1 || y == m - 1)
    {
      if (matrix[x, y] == 1)
        hasCornerCell = true;
    }
 
    // Mark land as visited
    visited[x, y] = true;
 
    // Traverse to all adjacent elements
    dfs(matrix, visited, x + 1, y, n, m,
        hasCornerCell);
    dfs(matrix, visited, x, y + 1, n, m,
        hasCornerCell);
    dfs(matrix, visited, x - 1, y, n, m,
        hasCornerCell);
    dfs(matrix, visited, x, y - 1, n, m,
        hasCornerCell);
  }
 
  // Function that counts the closed island
  static int countClosedIsland(int[,] matrix, int n,
                               int m)
  {
 
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    bool[,] visited = new bool[n, m];
    int result = 0;
 
    // Mark visited all lands
    // that are reachable from edge
    for(int i = 0; i < n; ++i)
    {
      for(int j = 0; j < m; ++j)
      {
        if ((i != 0 && j != 0 &&
             i != n - 1 && j != m - 1) &&
            matrix[i, j] == 1 &&
            visited[i, j] == false)
        {
 
          // Determine if the island is closed
          bool hasCornerCell = false;
 
          // hasCornerCell will be updated to
          // true while DFS traversal if there
          // is a cell with value '1' on the corner
          dfs(matrix, visited, i, j, n, m,
              hasCornerCell);
 
          // If the island is closed
          if (!hasCornerCell)
            result = result + 1;
        }
      }
    }
 
    // Return the final count
    return result;
  }
 
  // Driver code
  static void Main()
  {
     
    // Given size of Matrix
    int N = 5, M = 8;
  
    // Given Matrix
    int[,] matrix = { { 0, 0, 0, 0, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 0, 1 },
                       { 0, 1, 0, 1, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 1, 0 },
                       { 0, 0, 0, 0, 0, 0, 0, 1 } };
  
    // Function Call
    Console.WriteLine(countClosedIsland(matrix, N, M));
  }
}
 
// This code is contributed by divyesh072019


Javascript




<script>
 
    // JavaScript program for the above approach
     
    // DFS Traversal to find the count of
    // island surrounded by water
    function dfs(matrix, visited, x, y, n, m, hasCornerCell)
    {
 
        // If the land is already visited
        // or there is no land or the
        // coordinates gone out of matrix
        // break function as there
        // will be no islands
        if (x < 0 || y < 0 || x >= n || y >= m ||
            visited[x][y] == true || matrix[x][y] == 0)
            return;
 
        if (x == 0 || y == 0 ||
            x == n - 1 || y == m - 1)
        {
            if (matrix[x][y] == 1)
                hasCornerCell = true;
        }
 
        // Mark land as visited
        visited[x][y] = true;
 
        // Traverse to all adjacent elements
        dfs(matrix, visited, x + 1, y, n, m,
            hasCornerCell);
        dfs(matrix, visited, x, y + 1, n, m,
            hasCornerCell);
        dfs(matrix, visited, x - 1, y, n, m,
            hasCornerCell);
        dfs(matrix, visited, x, y - 1, n, m,
            hasCornerCell);
    }
 
    // Function that counts the closed island
    function countClosedIsland(matrix, n, m)
    {
 
        // Create boolean 2D visited matrix
        // to keep track of visited cell
 
        // Initially all elements are
        // unvisited.
        let visited = new Array(n);
        for(let i = 0; i < n; ++i)
        {
            visited[i] = new Array(m);
            for(let j = 0; j < m; ++j)
            {
                visited[i][j] = false;
            }
        }
        let result = 0;
 
        // Mark visited all lands
        // that are reachable from edge
        for(let i = 0; i < n; ++i)
        {
            for(let j = 0; j < m; ++j)
            {
                if ((i != 0 && j != 0 &&
                     i != n - 1 && j != m - 1) &&
                     matrix[i][j] == 1 &&
                     visited[i][j] == false)
                {
 
                    // Determine if the island is closed
                    let hasCornerCell = false;
 
                    // hasCornerCell will be updated to
                    // true while DFS traversal if there
                    // is a cell with value '1' on the corner
                    dfs(matrix, visited, i, j, n, m, hasCornerCell);
 
                    // If the island is closed
                    if (!hasCornerCell)
                        result = result + 1;
                }
            }
        }
 
        // Return the final count
        return result;
    }
     
    // Given size of Matrix
    let N = 5, M = 8;
  
    // Given Matrix
    let matrix = [ [ 0, 0, 0, 0, 0, 0, 0, 1 ],
                       [ 0, 1, 1, 1, 1, 0, 0, 1 ],
                       [ 0, 1, 0, 1, 0, 0, 0, 1 ],
                       [ 0, 1, 1, 1, 1, 0, 1, 0 ],
                       [ 0, 0, 0, 0, 0, 0, 0, 1 ] ];
  
    // Function Call
    document.write(countClosedIsland(matrix, N, M));
     
</script>


Output

2

Time Complexity: O(N*M)
The time complexity of the above algorithm is O(N*M) where N and M are the number of rows and columns of the given matrix. We traverse the entire matrix once and then perform a DFS traversal over the islands.

Space Complexity: O(N*M)
The space complexity of the above algorithm is O(N*M) as we declare two boolean arrays of size N*M to keep track of visited cells.

Method 2 – using BFS Traversal: The idea is to visit every cell with value 1 at the corner using BFS and then traverse the given matrix and if any unvisited cell with value 1 is encountered then increment the count of the island and make all the 1s connected to it as visited. Below are the steps: 

  1. Initialize a 2D visited matrix(say vis[][]) to keep the track of traversed cell in the given matrix.
  2. Perform BFS Traversal on all the corner of the given matrix and if any element has value 1 then marked all the cell with value 1 as visited because it cannot be counted in the resultant count.
  3. Perform BFS Traversal on all the remaining unvisited cell and if value encountered is 1 then marked this cell as visited, count this island in the resultant count and marked every cell in all the 4 directions i.e., left, right, top, and bottom to make all the 1s connected to the current cell as visited.
  4. Repeat the above step until all cell with value 1 are not visited.
  5. Print the count of island after the above steps.

Below is the implementation of the above approach

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };
 
// DFS Traversal to find the count of
// island surrounded by water
void bfs(vector<vector<int> >& matrix,
         vector<vector<bool> >& visited,
         int x, int y, int n, int m)
{
    // To store the popped cell
    pair<int, int> temp;
 
    // To store the cell of BFS
    queue<pair<int, int> > Q;
 
    // Push the current cell
    Q.push({ x, y });
 
    // Until Q is not empty
    while (!Q.empty())
    {
 
        temp = Q.front();
        Q.pop();
 
        // Mark current cell
        // as visited
        visited[temp.first]
               [temp.second]
            = true;
 
        // Iterate in all four directions
        for (int i = 0; i < 4; i++)
        {
            int x = temp.first + dx[i];
            int y = temp.second + dy[i];
 
            // Cell out of the matrix
            if (x < 0 || y < 0
                || x >= n || y >= m
                || visited[x][y] == true
                || matrix[x][y] == 0)
            {
                continue;
            }
 
            // Check is adjacent cell is
            // 1 and not visited
            if (visited[x][y] == false
                && matrix[x][y] == 1)
            {
                Q.push({ x, y });
            }
        }
    }
}
 
// Function that counts the closed island
int countClosedIsland(vector<vector<int> >& matrix,
                      int n, int m)
{
 
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    vector<vector<bool> > visited(
        n, vector<bool>(m, false));
 
    // Mark visited all lands
    // that are reachable from edge
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
 
            // Traverse corners
            if ((i * j == 0
                 || i == n - 1
                 || j == m - 1)
                and matrix[i][j] == 1
                and visited[i][j] == false)
            {
                bfs(matrix, visited,
                    i, j, n, m);
            }
        }
    }
 
    // To stores number of closed islands
    int result = 0;
 
    for (int i = 0; i < n; ++i)
    {
 
        for (int j = 0; j < m; ++j)
        {
 
            // If the land not visited
            // then there will be atleast
            // one closed island
            if (visited[i][j] == false
                and matrix[i][j] == 1)
            {
 
                result++;
 
                // Mark all lands associated
                // with island visited
                bfs(matrix, visited, i, j, n, m);
            }
        }
    }
 
    // Return the final count
    return result;
}
 
// Driver Code
int main()
{
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    vector<vector<int> > matrix
        = { { 0, 0, 0, 0, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 0, 1 },
            { 0, 1, 0, 1, 0, 0, 0, 1 },
            { 0, 1, 1, 1, 1, 0, 1, 0 },
            { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
    // Function Call
    cout << countClosedIsland(matrix, N, M);
    return 0;
}


Java




// Java program for the above approach
import java.util.LinkedList;
import java.util.Queue;
 
class GFG{
     
static int dx[] = { -1, 0, 1, 0 };
static int dy[] = { 0, 1, 0, -1 };
 
// To store the row and column index
// of the popped cell
static class Cell
{
    int first, second;
    Cell(int x, int y)
    {
        this.first = x;
        this.second = y;
    }
}
 
// BFS Traversal to find the count of
// island surrounded by water
static void bfs(int[][] matrix, boolean[][] visited,
                int x, int y, int n, int m)
{
     
    // To store the popped cell
    Cell temp;
 
    // To store the cell of BFS
    Queue<Cell> Q = new LinkedList<Cell>();
 
    // Push the current cell
    Q.add(new Cell(x, y));
 
    // Until Q is not empty
    while (!Q.isEmpty())
    {
        temp = Q.peek();
        Q.poll();
 
        // Mark current cell
        // as visited
        visited[temp.first][temp.second] = true;
 
        // Iterate in all four directions
        for(int i = 0; i < 4; i++)
        {
            int xIndex = temp.first + dx[i];
            int yIndex = temp.second + dy[i];
 
            // Cell out of the matrix
            if (xIndex < 0 || yIndex < 0 || xIndex >= n ||
                yIndex >= m || visited[xIndex][yIndex] == true ||
                matrix[xIndex][yIndex] == 0)
            {
                continue;
            }
 
            // Check is adjacent cell is
            // 1 and not visited
            if (visited[xIndex][yIndex] == false &&
                 matrix[xIndex][yIndex] == 1)
            {
                Q.add(new Cell(xIndex, yIndex));
            }
        }
    }
}
 
// Function that counts the closed island
static int countClosedIsland(int[][] matrix, int n,
                             int m)
{
     
    // Create boolean 2D visited matrix
    // to keep track of visited cell
 
    // Initially all elements are
    // unvisited.
    boolean[][] visited = new boolean[n][m];
 
    // Mark visited all lands
    // that are reachable from edge
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < m; ++j)
        {
             
            // Traverse corners
            if ((i * j == 0 || i == n - 1 || j == m - 1) &&
                matrix[i][j] == 1 && visited[i][j] == false)
            {
                bfs(matrix, visited, i, j, n, m);
            }
        }
    }
 
    // To stores number of closed islands
    int result = 0;
 
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < m; ++j)
        {
             
            // If the land not visited
            // then there will be atleast
            // one closed island
            if (visited[i][j] == false &&
                 matrix[i][j] == 1)
            {
                result++;
                 
                // Mark all lands associated
                // with island visited
                bfs(matrix, visited, i, j, n, m);
            }
        }
    }
 
    // Return the final count
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    int[][] matrix = { { 0, 0, 0, 0, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 0, 1 },
                       { 0, 1, 0, 1, 0, 0, 0, 1 },
                       { 0, 1, 1, 1, 1, 0, 1, 0 },
                       { 0, 0, 0, 0, 0, 0, 0, 1 } };
 
    // Function Call
    System.out.println(countClosedIsland(matrix, N, M));
}
}
 
// This code is contributed by jainlovely450


Python3




# Python program for the above approach
dx = [-1, 0, 1, 0 ]
dy = [0, 1, 0, -1]
 
global matrix
 
# DFS Traversal to find the count of
# island surrounded by water
def bfs(x, y, n, m):
   
    # To store the popped cell
    temp = []
     
    # To store the cell of BFS
    Q = []
     
    # Push the current cell
    Q.append([x, y])
     
    # Until Q is not empty
    while(len(Q) > 0):
        temp = Q.pop()
         
        # Mark current cell
        # as visited
        visited[temp[0]][temp[1]] = True
         
        # Iterate in all four directions
        for i in range(4):
            x = temp[0] + dx[i]
            y = temp[1] + dy[i]
             
            # Cell out of the matrix
            if(x < 0 or y < 0 or x >= n or y >= n or visited[x][y] == True or matrix[x][y] == 0):
                continue
            # Check is adjacent cell is
            # 1 and not visited
            if(visited[x][y] == False and matrix[x][y] == 1):
                Q.append([x, y])
 
# Function that counts the closed island
def countClosedIsland(n, m):
     
    # Create boolean 2D visited matrix
    # to keep track of visited cell
  
    # Initially all elements are
    # unvisited.
    global visited
    visited = [[False for i in range(m)] for j in range(n)]
 
    # Mark visited all lands
    # that are reachable from edge
    for i in range(n):
        for j in range(m):
           
            # Traverse corners
            if((i * j == 0 or i == n - 1 or j == m - 1) and matrix[i][j] == 1 and visited[i][j] == False):
                bfs(i, j, n, m);
                 
    # To stores number of closed islands
    result = 0
    for i in range(n):
        for j in range(m):
           
            # If the land not visited
            # then there will be atleast
            # one closed island
            if(visited[i][j] == False and matrix[i][j] == 1):
                result += 1
                 
                # Mark all lands associated
                # with island visited
                bfs(i, j, n, m);
                 
    # Return the final count
    return result
   
# Driver Code
 
# Given size of Matrix
N = 5
M = 8
 
# Given Matrix
matrix = [[ 0, 0, 0, 0, 0, 0, 0, 1],
          [0, 1, 1, 1, 1, 0, 0, 1],
          [0, 1, 0, 1, 0, 0, 0, 1 ],
          [0, 1, 1, 1, 1, 0, 1, 0 ],
          [0, 0, 0, 0, 0, 0, 0, 1]]
 
# Function Call
print(countClosedIsland(N, M))
 
# This code is contributed by avanitrachhadiya2155


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    static int[,] matrix;
    static bool[,] visited;
    static int[] dx = {-1, 0, 1, 0 };
    static int[] dy = {0, 1, 0, -1};
      
    // DFS Traversal to find the count of
    // island surrounded by water
    static void bfs(int x, int y, int n, int m)
    {
        // To store the popped cell
        Tuple<int,int> temp;
  
        // To store the cell of BFS
        List<Tuple<int,int>> Q = new List<Tuple<int,int>>();
  
        // Push the current cell
        Q.Add(new Tuple<int, int>(x, y));
  
        // Until Q is not empty
        while(Q.Count <= 0)
        {
            temp = Q[0];
            Q.RemoveAt(0);
         
            // Mark current cell
            // as visited
            visited[temp.Item1,temp.Item2] = true;
  
            // Iterate in all four directions
            for(int i = 0; i < 4; i++)
            {
                int xIndex = temp.Item1 + dx[i];
                int yIndex = temp.Item2 + dy[i];
      
                // Cell out of the matrix
                if (xIndex < 0 || yIndex < 0 || xIndex >= n ||
                    yIndex >= m || visited[xIndex,yIndex] == true ||
                    matrix[xIndex,yIndex] == 0)
                {
                    continue;
                }
      
                // Check is adjacent cell is
                // 1 and not visited
                if (visited[xIndex,yIndex] == false &&
                     matrix[xIndex,yIndex] == 1)
                {
                    Q.Add(new Tuple<int, int>(x, y));
                }
            }
        }
    }
  
    // Function that counts the closed island
    static int countClosedIsland(int n, int m)
    {
        // Create boolean 2D visited matrix
        // to keep track of visited cell
  
        // Initially all elements are
        // unvisited.
        visited = new bool[n, m];
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                visited[i,j] = false;
            }
        }
  
        // Mark visited all lands
        // that are reachable from edge
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                // Traverse corners
                if((i * j == 0 || i == n - 1 || j == m - 1) && matrix[i,j] == 1 && visited[i,j] == false)
                {
                    bfs(i, j, n, m);
                }
            }
        }
  
        // To stores number of closed islands
        int result = 2;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                // If the land not visited
                // then there will be atleast
                // one closed island
                if(visited[i,j] == false && matrix[i,j] == 1)
                {
                    result = 2;
  
                    // Mark all lands associated
                    // with island visited
                    bfs(i, j, n, m);
                 }
             }
         }
  
        // Return the final count
        return result;
   }
    
  static void Main() {
    // Given size of Matrix
    int N = 5, M = 8;
  
    // Given Matrix
    matrix = new int[5,8]
    { { 0, 0, 0, 0, 0, 0, 0, 1 },
       { 0, 1, 1, 1, 1, 0, 0, 1 },
       { 0, 1, 0, 1, 0, 0, 0, 1 },
       { 0, 1, 1, 1, 1, 0, 1, 0 },
       { 0, 0, 0, 0, 0, 0, 0, 1 } };
  
    // Function Call
    Console.Write(countClosedIsland(N, M));
  }
}
 
// This code is contributed by rameshtravel07.


Javascript




<script>
    // Javascript program for the above approach
    let matrix;
    let visited;
    let dx = [-1, 0, 1, 0 ];
    let dy = [0, 1, 0, -1];
     
    // DFS Traversal to find the count of
    // island surrounded by water
    function bfs(x, y, n, m)
    {
        // To store the popped cell
        let temp;
 
        // To store the cell of BFS
        let Q = [];
 
        // Push the current cell
        Q.push([x, y]);
 
        // Until Q is not empty
        while(Q.length > 0)
        {
            temp = Q[0];
            Q.pop();
        
            // Mark current cell
            // as visited
            visited[temp[0]][temp[1]] = true;
 
            // Iterate in all four directions
            for(let i = 0; i < 4; i++)
            {
                let x = temp[0] + dx[i];
                let y = temp[1] + dy[i];
 
                // Cell out of the matrix
                if(x < 0 || y < 0 || x >= n || y >= n || visited[x][y] == true || matrix[x][y] == 0)
                {
                    continue;
                }
                // Check is adjacent cell is
                // 1 and not visited
                if(visited[x][y] == false && matrix[x][y] == 1)
                {
                    Q.push([x, y]);
                }
              }
        }
    }
 
    // Function that counts the closed island
    function countClosedIsland(n, m)
    {
        // Create boolean 2D visited matrix
        // to keep track of visited cell
 
        // Initially all elements are
        // unvisited.
        visited = new Array(n);
        for(let i = 0; i < n; i++)
        {
            visited[i] = new Array(m);
            for(let j = 0; j < m; j++)
            {
                visited[i][j] = false;
            }
        }
 
        // Mark visited all lands
        // that are reachable from edge
        for(let i = 0; i < n; i++)
        {
            for(let j = 0; j < m; j++)
            {
                // Traverse corners
                if((i * j == 0 || i == n - 1 || j == m - 1) && matrix[i][j] == 1 && visited[i][j] == false)
                {
                    bfs(i, j, n, m);
                }
            }
         }
 
        // To stores number of closed islands
        let result = 2;
        for(let i = 0; i < n; i++)
        {
            for(let j = 0; j < m; j++)
            {
                // If the land not visited
                // then there will be atleast
                // one closed island
                if(visited[i][j] == false && matrix[i][j] == 1)
                {
                    result += 1;
 
                    // Mark all lands associated
                    // with island visited
                    bfs(i, j, n, m);
                 }
             }
         }
 
        // Return the final count
        return result;
   }
    
  // Given size of Matrix
  let N = 5, M = 8;
 
  // Given Matrix
  matrix
    = [ [ 0, 0, 0, 0, 0, 0, 0, 1 ],
    [ 0, 1, 1, 1, 1, 0, 0, 1 ],
    [ 0, 1, 0, 1, 0, 0, 0, 1 ],
    [ 0, 1, 1, 1, 1, 0, 1, 0 ],
    [ 0, 0, 0, 0, 0, 0, 0, 1 ] ];
 
  // Function Call
  document.write(countClosedIsland(matrix, N, M));
 
// This code is contributed by decode2207.
</script>


Output

2

Time Complexity: O(N*M) 
Auxiliary Space: O(N*M) 

Method 3 – using Disjoint-Set(Union-Find):

  1. Traverse the given matrix and change all the 1s and connected at the corners of the matrix to 0s.
  2. Now traverse the matrix again, and for all set of connected 1s create an edges connecting to all the 1s.
  3. Find the connected components for all the edges stored using Disjoint-Set Approach.
  4. Print the count of components after the above steps.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that implements the Find
int Find(vector<int>& hashSet, int val)
{
 
    // Get the val
    int parent = val;
 
    // Until parent is not found
    while (parent != hashSet[parent]) {
        parent = hashSet[parent];
    }
 
    // Return the parent
    return parent;
}
 
// Function that implements the Union
void Union(vector<int>& hashSet,
           int first, int second)
{
 
    // Find the first father
    int first_father = Find(hashSet, first);
 
    // Find the second father
    int second_father = Find(hashSet, second);
 
    // If both are unequals then update
    // first father as ssecond_father
    if (first_father != second_father)
        hashSet[first_father] = second_father;
}
 
// Recursive Function that change all
// the corners connected 1s to 0s
void change(vector<vector<char> >& matrix,
            int x, int y, int n, int m)
{
 
    // If already zero then return
    if (x < 0 || y < 0 || x > m - 1
        || y > n - 1 || matrix[x][y] == '0')
        return;
 
    // Change the current cell to '0'
    matrix[x][y] = '0';
 
    // Recursive Call for all the
    // four corners
    change(matrix, x + 1, y, n, m);
    change(matrix, x, y + 1, n, m);
    change(matrix, x - 1, y, n, m);
    change(matrix, x, y - 1, n, m);
}
 
// Function that changes all the
// connected 1s to 0s at the corners
void changeCorner(vector<vector<char> >& matrix)
{
    // Dimensions of matrix
    int m = matrix.size();
    int n = matrix[0].size();
 
    // Traverse the matrix
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
 
            // If corner cell
            if (i * j == 0 || i == m - 1
                || j == n - 1) {
 
                // If value is 1s, then
                // recursively change to 0
                if (matrix[i][j] == '1') {
                    change(matrix, i, j, n, m);
                }
            }
        }
    }
}
 
// Function that counts the number
// of island in the given matrix
int numIslands(vector<vector<char> >& matrix)
{
 
    if (matrix.size() == 0)
        return 0;
 
    // Dimensions of the matrix
    int m = matrix.size();
    int n = matrix[0].size();
 
    // Make all the corners connecting
    // 1s to zero
    changeCorner(matrix);
 
    // First convert to 1 dimension
    // position and convert all the
    // connections to edges
    vector<pair<int, int> > edges;
 
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
 
            // If the cell value is 1
            if (matrix[i][j] == '1') {
                int id = i * n + j;
 
                // Move right
                if (j + 1 < n) {
 
                    // If right cell is
                    // 1 then make it as
                    // an edge
                    if (matrix[i][j + 1] == '1') {
 
                        int right = i * n + j + 1;
 
                        // Push in edge vector
                        edges.push_back(make_pair(id, right));
                    }
                }
                // Move down
                if (i + 1 < m) {
 
                    // If right cell is
                    // 1 then make it as
                    // an edge
                    if (matrix[i + 1][j] == '1') {
                        int down = (i + 1) * n + j;
 
                        // Push in edge vector
                        edges.push_back(make_pair(id, down));
                    }
                }
            }
        }
    }
 
    // Construct the Union Find structure
    vector<int> hashSet(m * n, 0);
    for (int i = 0; i < m * n; i++) {
        hashSet[i] = i;
    }
 
    // Next apply Union Find for all
    // the edges stored
    for (auto edge : edges) {
        Union(hashSet, edge.first, edge.second);
    }
 
    // To count the number of connected
    // islands
    int numComponents = 0;
 
    // Traverse to find the islands
    for (int i = 0; i < m * n; i++) {
        if (matrix[i / n][i % n] == '1'
            && hashSet[i] == i)
            numComponents++;
    }
 
    // Return the count of the island
    return numComponents;
}
 
// Driver Code
int main()
{
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    vector<vector<char> > matrix
        = { { '0', '0', '0', '0', '0', '0', '0', '1' },
            { '0', '1', '1', '1', '1', '0', '0', '1' },
            { '0', '1', '0', '1', '0', '0', '0', '1' },
            { '0', '1', '1', '1', '1', '0', '1', '0' },
            { '0', '0', '0', '0', '0', '0', '0', '1' } };
 
    // Function Call
    cout << numIslands(matrix);
    return 0;
}


Java




// Java program for the above approach
import java.util.ArrayList;
 
class GFG{
 
static class Edge
{
    int first, second;
     
    Edge(int x, int y)
    {
        this.first = x;
        this.second = y;
    }
}
 
// Function that implements the Find
static int Find(int[] hashSet, int val)
{
     
    // Get the val
    int parent = val;
 
    // Until parent is not found
    while (parent != hashSet[parent])
    {
        parent = hashSet[parent];
    }
     
    // Return the parent
    return parent;
}
 
// Function that implements the Union
static void Union(int[] hashSet, int first, int second)
{
     
    // Find the first father
    int first_father = Find(hashSet, first);
 
    // Find the second father
    int second_father = Find(hashSet, second);
 
    // If both are unequals then update
    // first father as ssecond_father
    if (first_father != second_father)
        hashSet[first_father] = second_father;
}
 
// Recursive Function that change all
// the corners connected 1s to 0s
static void change(char[][] matrix, int x, int y,
                                    int n, int m)
{
     
    // If already zero then return
    if (x < 0 || y < 0 || x > m - 1 ||
        y > n - 1 || matrix[x][y] == '0')
        return;
 
    // Change the current cell to '0'
    matrix[x][y] = '0';
 
    // Recursive Call for all the
    // four corners
    change(matrix, x + 1, y, n, m);
    change(matrix, x, y + 1, n, m);
    change(matrix, x - 1, y, n, m);
    change(matrix, x, y - 1, n, m);
}
 
// Function that changes all the
// connected 1s to 0s at the corners
static void changeCorner(char[][] matrix)
{
     
    // Dimensions of matrix
    int m = matrix.length;
    int n = matrix[0].length;
 
    // Traverse the matrix
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
             
            // If corner cell
            if (i * j == 0 || i == m - 1 || j == n - 1)
            {
                 
                // If value is 1s, then
                // recursively change to 0
                if (matrix[i][j] == '1')
                {
                    change(matrix, i, j, n, m);
                }
            }
        }
    }
}
 
// Function that counts the number
// of island in the given matrix
static int numIslands(char[][] matrix)
{
    if (matrix.length == 0)
        return 0;
 
    // Dimensions of the matrix
    int m = matrix.length;
    int n = matrix[0].length;
 
    // Make all the corners connecting
    // 1s to zero
    changeCorner(matrix);
 
    // First convert to 1 dimension
    // position and convert all the
    // connections to edges
    ArrayList<Edge> edges = new ArrayList<Edge>();
 
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
             
            // If the cell value is 1
            if (matrix[i][j] == '1')
            {
                int id = i * n + j;
 
                // Move right
                if (j + 1 < n)
                {
                     
                    // If right cell is
                    // 1 then make it as
                    // an edge
                    if (matrix[i][j + 1] == '1')
                    {
                        int right = i * n + j + 1;
 
                        // Push in edge vector
                        edges.add(new Edge(id, right));
                    }
                }
                 
                // Move down
                if (i + 1 < m)
                {
                     
                    // If right cell is
                    // 1 then make it as
                    // an edge
                    if (matrix[i + 1][j] == '1')
                    {
                        int down = (i + 1) * n + j;
 
                        // Push in edge vector
                        edges.add(new Edge(id, down));
                    }
                }
            }
        }
    }
 
    // Construct the Union Find structure
    int[] hashSet = new int[m * n];
    for(int i = 0; i < m * n; i++)
    {
        hashSet[i] = i;
    }
 
    // Next apply Union Find for all
    // the edges stored
    for(Edge edge : edges)
    {
        Union(hashSet, edge.first, edge.second);
    }
 
    // To count the number of connected
    // islands
    int numComponents = 0;
 
    // Traverse to find the islands
    for(int i = 0; i < m * n; i++)
    {
        if (matrix[i / n][i % n] == '1' &&
            hashSet[i] == i)
            numComponents++;
    }
 
    // Return the count of the island
    return numComponents;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given size of Matrix
    int N = 5, M = 8;
 
    // Given Matrix
    char[][] matrix = { { '0', '0', '0', '0',
                          '0', '0', '0', '1' },
                        { '0', '1', '1', '1',
                          '1', '0', '0', '1' },
                        { '0', '1', '0', '1',
                          '0', '0', '0', '1' },
                        { '0', '1', '1', '1',
                          '1', '0', '1', '0' },
                        { '0', '0', '0', '0',
                          '0', '0', '0', '1' } };
 
    // Function Call
    System.out.println(numIslands(matrix));
}
}
 
// This code is contributed by jainlovely450


Python3




# python 3 program for the above approach
 
# Function that implements the Find
def Find(hashSet, val):
   
    # Get the val
    parent = val
     
    # Until parent is not found
    while (parent != hashSet[parent]):
        parent = hashSet[parent]
 
    # Return the parent
    return parent
 
# Function that implements the Union
def Union(hashSet, first, second):
   
    # Find the first father
    first_father = Find(hashSet, first)
 
    # Find the second father
    second_father = Find(hashSet, second)
 
    # If both are unequals then update
    # first father as ssecond_father
    if (first_father != second_father):
        hashSet[first_father] = second_father
 
# Recursive Function that change all
# the corners connected 1s to 0s
def change(matrix, x, y, n, m):
   
    # If already zero then return
    if (x < 0 or y < 0 or x > m - 1 or y > n - 1 or matrix[x][y] == '0'):
        return
 
    # Change the current cell to '0'
    matrix[x][y] = '0'
 
    # Recursive Call for all the
    # four corners
    change(matrix, x + 1, y, n, m)
    change(matrix, x, y + 1, n, m)
    change(matrix, x - 1, y, n, m)
    change(matrix, x, y - 1, n, m)
 
# Function that changes all the
# connected 1s to 0s at the corners
def changeCorner(matrix):
   
    # Dimensions of matrix
    m = len(matrix)
    n = len(matrix[0])
 
    # Traverse the matrix
    for i in range(m):
        for j in range(n):
           
            # If corner cell
            if (i * j == 0 or i == m - 1 or j == n - 1):
               
                # If value is 1s, then
                # recursively change to 0
                if (matrix[i][j] == '1'):
                    change(matrix, i, j, n, m)
 
# Function that counts the number
# of island in the given matrix
def numIslands(matrix):
    if (len(matrix) == 0):
        return 0
 
    # Dimensions of the matrix
    m = len(matrix)
    n = len(matrix[0])
 
    # Make all the corners connecting
    # 1s to zero
    changeCorner(matrix)
 
    # First convert to 1 dimension
    # position and convert all the
    # connections to edges
    edges = []
    for i in range(m):
        for j in range(n):
           
            # If the cell value is 1
            if (matrix[i][j] == '1'):
                id = i * n + j
 
                # Move right
                if (j + 1 < n):
                    # If right cell is
                    # 1 then make it as
                    # an edge
                    if (matrix[i][j + 1] == '1'):
                        right = i * n + j + 1
 
                        # Push in edge vector
                        edges.append([id, right])
                # Move down
                if (i + 1 < m):
                   
                    # If right cell is
                    # 1 then make it as
                    # an edge
                    if (matrix[i + 1][j] == '1'):
                        down = (i + 1) * n + j
 
                        # Push in edge vector
                        edges.append([id, down])
 
    # Construct the Union Find structure
    hashSet = [0 for i in range(m*n)]
    for i in range(m*n):
        hashSet[i] = i
 
    # Next apply Union Find for all
    # the edges stored
    for edge in edges:
        Union(hashSet, edge[0], edge[1])
 
    # To count the number of connected
    # islands
    numComponents = 0
 
    # Traverse to find the islands
    for i in range(m*n):
        if (matrix[i // n][i % n] == '1' and hashSet[i] == i):
            numComponents += 1
 
    # Return the count of the island
    return numComponents
 
# Driver Code
if __name__ == '__main__':
   
    # Given size of Matrix
    N = 5
    M = 8
 
    # Given Matrix
    matrix = [['0', '0', '0', '0', '0', '0', '0', '1'],
              ['0', '1', '1', '1', '1', '0', '0', '1'],
              ['0', '1', '0', '1', '0', '0', '0', '1'],
              ['0', '1', '1', '1', '1', '0', '1', '0'],
              ['0', '0', '0', '0', '0', '0', '0', '1']]
 
    # Function Call
    print(numIslands(matrix))
 
    # This code is contributed by bgangwar59.


Javascript




<script>
// JavaScript program for the above approach
 
// Function that implements the Find
function Find(hashSet, val)
{
 
    // Get the val
    var parent = val;
     
    // Until parent is not found
    while (parent != hashSet[parent]) {
      parent = hashSet[parent];
    }
   
    // Return the parent
    return parent;
  }
   
  // Function that implements the Union
  function Union(hashSet, first, second) {
    // Find the first father
    var first_father = Find(hashSet, first);
    // Find the second father
    var second_father = Find(hashSet, second);
   
    // If both are unequals then update
    // first father as ssecond_father
    if (first_father != second_father) {
      hashSet[first_father] = second_father;
    }
  }
   
  // Recursive Function that change all
  // the corners connected 1s to 0s
  function change(matrix, x, y, n, m) {
    // If already zero then return
    if (x < 0 || y < 0 || x > m - 1 || y > n - 1 || matrix[x][y] == "0") {
      return;
    }
     
    // Change the current cell to '0'
    matrix[x][y] = "0";
   
    // Recursive Call for all the
    // four corners
    change(matrix, x + 1, y, n, m);
    change(matrix, x, y + 1, n, m);
    change(matrix, x - 1, y, n, m);
    change(matrix, x, y - 1, n, m);
  }
   
  // Function that changes all the
  // connected 1s to 0s at the corners
  function changeCorner(matrix) {
    // Dimensions of matrix
    var m = matrix.length;
    var n = matrix[0].length;
   
    // Traverse the matrix
    for (let i = 0; i < m; i++) {
      for (let j = 0; j < n; j++) {
        // If corner cell
        if (i * j == 0 || i == m - 1 || j == n - 1) {
          // If value is 1s, then
          // recursively change to 0
          if (matrix[i][j] == "1") {
            change(matrix, i, j, n, m);
          }
        }
      }
    }
  }
   
  // Function that counts the number
  // of island in the given matrix
  function numIslands(matrix) {
    if (matrix.length == 0) {
      return 0;
    }
   
    // Dimensions of the matrix
    var m = matrix.length;
    var n = matrix[0].length;
   
    // Make all the corners connecting
    // 1s to zero
    changeCorner(matrix);
   
    // First convert to 1 dimension
    // position and convert all the
    // connections to edges
    var edges = [];
    for (let i = 0; i < m; i++) {
      for (let j = 0; j < n; j++) {
        // If the cell value is 1
        if (matrix[i][j] == "1") {
          id = i * n + j;
   
          // Move right
          if (j + 1 < n) {
            // If right cell is
            // 1 then make it as
            // an edge
            if (matrix[i][j + 1] == "1") {
              var right = i * n + j + 1;
   
              // Push in edge vector
              edges.push([id, right]);
            }
          }
          // Move down
          if (i + 1 < m) {
            // If right cell is
            // 1 then make it as
            // an edge
            if (matrix[i + 1][j] == "1") {
              var down = (i + 1) * n + j;
   
              // Push in edge vector
              edges.push([id, down]);
            }
          }
        }
      }
    }
    // Construct the Union Find structure
    var hashSet = Array(m * n).fill(0);
    for (let i = 0; i < m * n; i++) {
      hashSet[i] = i;
    }
    // Next apply Union Find for all
    // the edges stored
    for (let i =0; i<edges.length; i++ ) {
      Union(hashSet, edges[i][0], edges[i][1]);
    }
    // To count the number of connected
    // islands
    var numComponents = 0;
   
    // Traverse to find the islands
    for (let i = 0; i < m * n; i++) {
      if (matrix[parseInt(i / n)][i % n] == "1" && hashSet[i] == i) {
        numComponents += 1;
      }
    }
    // Return the count of the island
    return numComponents;
  }
   
  // Driver Code
  // Given size of Matrix
  var N = 5;
  var M = 8;
   
  // Given Matrix
  var matrix = [
    ["0", "0", "0", "0", "0", "0", "0", "1"],
    ["0", "1", "1", "1", "1", "0", "0", "1"],
    ["0", "1", "0", "1", "0", "0", "0", "1"],
    ["0", "1", "1", "1", "1", "0", "1", "0"],
    ["0", "0", "0", "0", "0", "0", "0", "1"],
  ];
   
  // Function Call
  document.write(numIslands(matrix));
   
  // This code is contributed by rdtank.
</script>


C#




using System;
using System.Collections.Generic;
 
class Program
{
    static int Find(List<int> hashSet, int val)
    {
        int parent = val;
 
        while (parent != hashSet[parent])
        {
            parent = hashSet[parent];
        }
 
        return parent;
    }
 
    static void Union(List<int> hashSet, int first, int second)
    {
        int first_father = Find(hashSet, first);
        int second_father = Find(hashSet, second);
 
        if (first_father != second_father)
            hashSet[first_father] = second_father;
    }
 
    static void Change(ref List<List<char>> matrix, int x, int y, int n, int m)
    {
        if (x < 0 || y < 0 || x > m - 1 || y > n - 1 || matrix[x][y] == '0')
            return;
 
        matrix[x][y] = '0';
 
        Change(ref matrix, x + 1, y, n, m);
        Change(ref matrix, x, y + 1, n, m);
        Change(ref matrix, x - 1, y, n, m);
        Change(ref matrix, x, y - 1, n, m);
    }
 
    static void ChangeCorner(ref List<List<char>> matrix)
    {
        int m = matrix.Count;
        int n = matrix[0].Count;
 
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (i * j == 0 || i == m - 1 || j == n - 1)
                {
                    if (matrix[i][j] == '1')
                    {
                        Change(ref matrix, i, j, n, m);
                    }
                }
            }
        }
    }
 
    static int NumIslands(List<List<char>> matrix)
    {
        if (matrix.Count == 0)
            return 0;
 
        int m = matrix.Count;
        int n = matrix[0].Count;
 
        ChangeCorner(ref matrix);
 
        List<Tuple<int, int>> edges = new List<Tuple<int, int>>();
 
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (matrix[i][j] == '1')
                {
                    int id = i * n + j;
 
                    if (j + 1 < n)
                    {
                        if (matrix[i][j + 1] == '1')
                        {
                            int right = i * n + j + 1;
                            edges.Add(new Tuple<int, int>(id, right));
                        }
                    }
 
                    if (i + 1 < m)
                    {
                        if (matrix[i + 1][j] == '1')
                        {
                            int down = (i + 1) * n + j;
                            edges.Add(new Tuple<int, int>(id, down));
                        }
                    }
                }
            }
        }
 
        List<int> hashSet = new List<int>(m * n);
        for (int i = 0; i < m * n; i++)
        {
            hashSet.Add(i);
        }
 
        foreach (Tuple<int, int> edge in edges)
        {
            Union(hashSet, edge.Item1, edge.Item2);
        }
 
        int numComponents = 0;
 
        for (int i = 0; i < m * n; i++)
            if (matrix[i / n][i %n] == '1' && hashSet[i]==i)
                numComponents++;
        return numComponents;
    }
    public static void Main()
    {
        // Given size of Matrix
        int N = 5, M = 8;
 
        // Given Matrix
         List<List<char>> matrix = new List<List<char>>() {
            new List<char> { '0', '0', '0', '0', '0', '0', '0', '1' },
            new List<char> { '0', '1', '1', '1', '1', '0', '0', '1' },
            new List<char> { '0', '1', '0', '1', '0', '0', '0', '1' },
            new List<char> { '0', '1', '1', '1', '1', '0', '1', '0' },
            new List<char> { '0', '0', '0', '0', '0', '0', '0', '1' }
        };
        // Function Call
        Console.WriteLine(NumIslands(matrix));
    }
}
 
     
        


Output

2

Time Complexity: O(N*M) 
Auxiliary Space: O(N*M) 



Similar Reads

Find the number of distinct islands in a 2D matrix
Given a boolean 2D matrix. The task is to find the number of distinct islands where a group of connected 1s (horizontally or vertically) forms an island. Two islands are considered to be distinct if and only if one island is equal to another (not rotated or reflected). Examples: Input: grid[][] = {{1, 1, 0, 0, 0}, 1, 1, 0, 0, 0}, 0, 0, 0, 1, 1}, 0,
11 min read
Number of Islands in a Circular Matrix
Given a 2D Matrix world map[][] of size N * M, such that all the places covered via land are represented by ‘L’ and the rest by ‘W’. Since the world is spherical rows and columns are cyclic (before row 1 row n comes and after row n row 1 same for the column). Count the number of islands. Examples: Input: N = 4, M = 4, worldMap = [ ['L', 'W', 'W', '
8 min read
Find the number of Islands using Disjoint Set
Given a boolean 2D matrix, find the number of islands.A group of connected 1s forms an island. For example, the below matrix contains 5 islands {1, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 1, 0, 1} A cell in the 2D matrix can be connected to 8 neighbours. This is a variation of the standard problem: “Counting the numbe
15+ min read
Number of Islands after changing given cell for K queries
Given two integers N and M which denote the size of a matrix where initially all the cells are 0s. There are also K queries each of type {x, y} that denotes a cell of the matrix whose value needs to be flipped from 0 to 1. The task is to find the number of islands of 1s after each query. Note: An island means a group of 1s such that they share a co
10 min read
Find Simple Closed Path for a given set of points
Given a set of points, connect the dots without crossing. Example: Input: points[] = {(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1}, {3, 3}}; Output: Connecting points in following order would not cause any crossing {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (0, 3)} We strongly recommend you to minimize your browser and try t
11 min read
Minimum number of Water to Land conversion to make two islands connected in a Grid
Given a 2D grid arr[][] of ‘W’ and ‘L’ where 'W' denotes water and ‘L’ denotes land, the task is to find the minimum number of water components ‘W’ that must be changed to land component ‘L’ so that two islands becomes connected. An island is the set of connected ‘L’s. Note: There can be only two disjoint islands. Examples: Input: arr[][] = {{'W',
10 min read
Minimum number of Water to Land conversion to make two islands connected in a Grid | Set 2
Given a 2D grid arr[][] of ‘W’ and ‘L’ where ‘W’ denotes water and ‘L’ denotes land, the task is to find the minimum number of water components ‘W’ that must be changed to land component ‘L’ so that two islands becomes connected. An island is the set of connected 'L's Note: There can be only two disjoint islands. Examples: Input: arr[][] = {{‘W’, ‘
13 min read
Count number of islands where every island is row-wise and column-wise separated
Given a rectangular matrix which has only two possible values 'X' and 'O'. The values 'X' always appear in form of rectangular islands and these islands are always row-wise and column-wise separated by at least one line of 'O's. Note that islands can only be diagonally adjacent. Count the number of islands in the given matrix. Examples: mat[M][N] =
14 min read
Number of Islands
Given a binary 2D matrix, find the number of islands. A group of connected 1s forms an island. For example, the below matrix contains 4 islands. Examples: Input: M[][] = {{'1', '1', '0', '0', '0'},                         {'0', '1', '0', '0', '1'},                         {'1', '0', '0', '1', '1'},                        {'0', '0', '0', '0', '0'},
15+ min read
Find the centroid of a non-self-intersecting closed Polygon
Given N vertices of the polygon, the task is to find the centroid of the polygonExamples: Input: ar = {{0, 0}, {0, 8}, {8, 8}, {8, 0}} Output: {Cx, Cy} = {4, 4} Input: ar = {{1, 2}, {3, -4}, {6, -7}} Output: {Cx, Cy} = {3.33, -3} Approach: The centroid of a non-self-intersecting closed polygon defined by n vertices (x0, y0), (x1, y1), ..., (xn-1, y
5 min read
Distance between closest pair of islands
Given a grid[][] containing 0s and 1s, where '0' represents water and '1' represents the land. Given that an island is a group of land (1s) surrounded by water (0s) on all sides. The task is to find the distance between the two closest islands such that: Distance between two islands is the minimum number of '0' between two islands.Only 4 - directio
15+ min read
Islands in a graph using BFS
Given a binary 2D matrix, find the number of islands. A group of connected 1s forms an island. Examples: Input: M[][] = {{‘1’, ‘1’, ‘0’, ‘0’, ‘0’}, {‘0’, ‘1’, ‘0’, ‘0’, ‘1’}, {‘1’, ‘0’, ‘0’, ‘1’, ‘1’}, {‘0’, ‘0’, ‘0’, ‘0’, ‘0’}, {‘1’, ‘0’, ‘1’, ‘1’, ‘0’}}Output: 4Explanation: The image below shows all the 4 islands in the graph Input: M[][] = {{‘1’
15+ min read
Check if the door is open or closed
Given n doors and n persons. The doors are numbered 1 to n and persons are given id's numbered 1 to n. Each door can have only 2 status open and closed. Initially all the doors have status closed. Find the final status of all the doors if a person changes the current status of all the doors, i.e. if status open then change to status closed and vice
10 min read
Generate matrix from given Sparse Matrix using Linked List and reconstruct the Sparse Matrix
Given a sparse matrix mat[][] of dimensions N*M, the task is to construct and represent the original matrix using a Linked List and reconstruct the givensparse matrix. Examples: Input: mat[][] = {{0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 2, 0, 0}, {0, 3, 0, 0, 4}, {0, 0, 5, 0, 0}}Output:Linked list representation: (4, 2, 5) ? (3, 4, 4) ? (3, 1, 3) ?
15+ min read
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix
Given a matrix B[][] of dimensions N * M, the task is to generate a matrix A[][] of same dimensions that can be formed such that for any element B[i][j] is equal to Bitwise OR of all elements in the ith row and jth column of A[][]. If no such matrix exists, print "Not Possible". Otherwise, print the matrix A[][]. Examples: Input: B[][] = {{1, 1, 1}
11 min read
Find the original matrix from the given AND matrix
Given a binary matrix B[][] of size N*M, the task is to find a matrix A[][] of the same size such that B[i][j] is the bitwise AND of all the elements in ith row and jth column of A[][]. Examples: Input: B[][] = { {1, 0, 1}, {0, 0, 0} }Output: { {1, 1, 1}, {1, 0, 1} }Explanation:1 1 1 ? 1 0 11 0 1 0 0 0 Input: B[][] = { {0, 0}, {1, 1} }Output: -1 Ap
10 min read
Check if a given matrix can be converted to another given matrix by row and column exchanges
Given a matrix startMatrix and another matrix finalMatrix, the task is to check if startMatrix can be converted to finalMatrix by column exchanges and a row exchanges. Examples: Input: start[][] = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}, {13, 14, 15, 16}}final[][] = {{1, 4, 3, 2}, {5, 8, 7, 6}, {13, 16, 15, 14}, {9, 12, 11, 10}} Output: Yes Exp
9 min read
Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
Given a matrix of order NxN, Find the minimum number of steps to convert the given matrix into the Upper Hessenberg matrix. In each step, the only operation allowed is to decrease or increase any element value by 1.Examples: Input : N=3 1 2 8 1 3 4 2 3 4 Output :2 Decrease the element a[2][0] 2 times. Now the matrix is upper hessenbergInput : N=4 1
4 min read
Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
Given a matrix of order NxN, the task is to find the minimum number of steps to convert given matrix into Diagonally Dominant Matrix. In each step, the only operation allowed is to decrease or increase any element by 1.Examples: Input: mat[][] = {{3, 2, 4}, {1, 4, 4}, {2, 3, 4}} Output: 5 Sum of the absolute values of elements of row 1 except the d
6 min read
Find trace of matrix formed by adding Row-major and Column-major order of same matrix
Given two integers N and M. Consider two matrix ANXM, BNXM. Both matrix A and matrix B contains elements from 1 to N*M. Matrix A contains elements in Row-major order and matrix B contains elements in Column-major order. The task is to find the trace of the matrix formed by addition of A and B. Trace of matrix PNXM is defined as P[0][0] + P[1][1] +
15 min read
Check if it is possible to make the given matrix increasing matrix or not
Given a matrix of N X M of positive integers, the task is to find out whether it is possible to make the matrix increasing or not. Print the matrix constructed otherwise print -1. Matrix elements should be greater than zero. A matrix is said to be increasing matrix if: For each row, the elements are in the increasing order.For each column, the elem
10 min read
Program to convert given Matrix to a Diagonal Matrix
Given a N*N matrix. The task is to convert the matrix to a diagonal matrix. That is to change the values of the non-diagonal elements of a matrix to 0.Diagonal-Matrix: A matrix is called a Diagonal Matrix if all the non-diagonal elements of the matrix are zero.Examples: Input : mat[][] = {{ 2, 1, 7 }, { 3, 7, 2 }, { 5, 4, 9 }} Output : {{2, 0, 7},
8 min read
Maximum trace possible for any sub-matrix of the given matrix
Given an N x N matrix mat[][], the task is to find the maximum trace possible for any sub-matrix of the given matrix.Examples: Input: mat[][] = { {10, 2, 5}, {6, 10, 4}, {2, 7, -10}} Output: 20 {{10, 2}, {6, 10}} is the sub-matrix with the maximum trace. Input: mat[][] = { {1, 2, 5}, {6, 3, 4}, {2, 7, 1}} Output: 13 Approach: An efficient approach
6 min read
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B
Given two binary matrices, A[][] and B[][] of size N×M, the task is to find the minimum number of swaps of the elements of matrix A, needed to convert matrix A into matrix B. If it is impossible to do so then print "-1". Examples : Input: A[][] = {{1, 1, 0}, {0, 0, 1}, {0, 1, 0}}, B[][] = {{0, 0, 1}, {0, 1, 0}, {1, 1, 0}}Output: 3Explanation: One p
8 min read
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
Given a matrix, mat[][] of size N × N, the task is to minimize the count of adjacent row swaps to convert the given matrix to a lower triangular matrix. If it is not possible to convert the given matrix to a lower triangular matrix, then print -1. Note: A lower triangular matrix contains 0s at all the indices above the main diagonal. Examples: Inpu
9 min read
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
Given a matrix arr[][] of dimensions N * M, the task is to generate a matrix such that any cell (r, c) stores the sum of adjacent elements present horizontally, vertically, and diagonally in the given matrix. Examples: Input: arr[][] = {{1, 3}, {2, 4}}Output: {{9, 7}, {8, 6}}Explanation: Matrix is constructed by the following operations: For cell (
9 min read
Convert a Matrix into another Matrix of given dimensions
Given a matrix, mat[][] of size N * M and two positive integers A and B, the task is to construct a matrix of size A * B from the given matrix elements. If multiple solutions exist, then print any one of them. Otherwise, print -1. Input: mat[][] = { { 1, 2, 3, 4, 5, 6 } }, A = 2, B = 3 Output: { { 1, 2, 3 }, { 4, 5, 6 } } Explanation: Since the siz
6 min read
Check if a Matrix can be superimposed on the given Matrix
Given a matrix letter[][] of size N * M, composed of '#' and '*' and another matrix stamp[][] of size X * Y containing only '$'. The task is to find if all the '*' of the larger one can be replaced by '$' by superimposing the stamp matrix on the letter matrix. Note: In a superimpose operation only a area having all the characters as '*' or '$' can
10 min read
Generate a matrix having each element equal to the sum of specified submatrices of a given matrix
Given a matrix mat[][] of dimensions M*N and an integer K, the task is to generate a matrix answer[][], where answer[i][j] is equal to the sum of all elements mat[r][c][/c] such that r ∈ [i - K, i + K], c ∈ [j - K, j + K], and (r, c) is a valid position in the matrix. Examples: Input: mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, K = 1Output: {{12, 21,
15+ min read
Convert the given Matrix into a difference Matrix
Given an input matrix mat[][] of size m*m and the task is to convert the given matrix into a difference matrix of the same size (matrix does not contain any 0 in it) as the input matrix by using the below-mentioned formula: difference[i][j] = Total Even Numbers in ith Row - Total Odd Numbers in jth Column. Examples: Input: {{1, 2, 21, 12}, {4, 5, 6
15+ min read