# Check if there are T number of continuous of blocks of 0s or not in given Binary Matrix

• Difficulty Level : Easy
• Last Updated : 12 Apr, 2022

Given a binary matrix mat[][] of dimensions M*N, the task is to check whether there exist T continuous blocks of 0s or not and at least 2*max(M, N) cells with value 0s or not. If found to be true, then print Yes. Otherwise, print No.

T is defined as the GCD of N and M. A continuous block is defined as the continuous stretch through row-wise or columns-wise or diagonal-wise.

Examples:

Input: N = 3, M = 4, mat[][] = { {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {0, 0, 1}}
Output: Yes
Explanation: Matrix has exactly 8 cells with value 0 which is equal to 2*max(N, M )) and there is 1 continuous spot which is equal to GCD of N and M.

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

Approach: The idea is to count the number of cells with value 0 and if that satisfies the condition then find the greatest common divisor of M and N and perform a depth-first search to find the number of connected components with value 0. Follow the steps below to solve the problem:

• Initialize a variable, say black as 0 and count the number of cells with value 0.
• If black is less than equal to 2*max(M, N) then print No and return from the function.
• Initialize a variable, say blackSpots as 0 to count the number of continuous spots.
• Iterate over the range [0, M) using the variable i and nested iterate over the range [0, N) using the variable j and if the current cell visited[i][j] as false and if mat[i][j] is 0 then perform the DFS Traversal on given matrix with current cell to find the continuous segment with value 0 row-wise, column-wise and diagonal-wise and increase the value of blackSpots by 1.
• After performing the above steps, if the value of blackSpots is greater than gcd(M, N), then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;``const` `long` `long` `M = 1e9 + 7;` `// Stores the moves in the matrix``vector > directions``    ``= { { 0, 1 }, { -1, 0 }, { 0, -1 },``        ``{ 1, 0 }, { 1, 1 }, { -1, -1 },``        ``{ -1, 1 }, { 1, -1 } };` `// Function to find if the current cell``// lies in the matrix or not``bool` `isInside(``int` `i, ``int` `j, ``int` `N, ``int` `M)``{``    ``if` `(i >= 0 && i < N``        ``&& j >= 0 && j < M) {``        ``return` `true``;``    ``}``    ``return` `false``;``}` `// Function to perform the DFS Traversal``void` `DFS(vector > mat, ``int` `N,``         ``int` `M, ``int` `i, ``int` `j,``         ``vector >& visited)``{``    ``if` `(visited[i][j] == ``true``) {` `        ``return``;``    ``}` `    ``visited[i][j] = ``true``;` `    ``// Iterate over the direction vector``    ``for` `(``auto` `it : directions) {``        ``int` `I = i + it.first;``        ``int` `J = j + it.second;``        ``if` `(isInside(I, J, N, M)) {``            ``if` `(mat[I][J] == 0) {` `                ``// DFS Call``                ``DFS(mat, N, M, I,``                    ``J, visited);``            ``}``        ``}``    ``}``}` `// Function to check if it satisfy the``// given criteria or not``void` `check(``int` `N, ``int` `M,``           ``vector >& mat)``{``    ``// Keeps the count of cell having``    ``// value as 0``    ``int` `black = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {``        ``for` `(``int` `j = 0; j < M; j++) {``            ``if` `(mat[i][j] == 0) {``                ``black++;``            ``}``        ``}``    ``}` `    ``// If condition doesn't satisfy``    ``if` `(black < 2 * (max(N, M))) {``        ``cout << ``"NO"` `<< endl;``        ``return``;``    ``}` `    ``vector > visited;` `    ``for` `(``int` `i = 0; i < N; i++) {``        ``vector<``bool``> temp;``        ``for` `(``int` `j = 0; j < M; j++) {``            ``temp.push_back(``false``);``        ``}``        ``visited.push_back(temp);``    ``}` `    ``// Keeps the track of unvisited cell``    ``// having values 0``    ``int` `black_spots = 0;` `    ``for` `(``int` `i = 0; i < N; i++) {``        ``for` `(``int` `j = 0; j < M; j++) {``            ``if` `(visited[i][j] == ``false``                ``&& mat[i][j] == 0) {` `                ``// Increasing count of``                ``// black_spot``                ``black_spots++;``                ``DFS(mat, N, M, i, j, visited);``            ``}``        ``}``    ``}` `    ``// Find the GCD of N and M``    ``int` `T = __gcd(N, M);` `    ``// Print the result``    ``cout << (black_spots >= T ? ``"Yes"` `: ``"No"``);``}` `// Driver Code``int` `main()``{``    ``int` `N = 3, M = 3;``    ``vector > mat``        ``= { { 0, 0, 1 }, { 1, 1, 1 }, { 0, 0, 1 } };` `    ``check(M, N, mat);` `    ``return` `0;``}`

## Python3

 `# python program for the above approach` `import` `math` `M ``=` `1000000000` `+` `7` `# Stores the moves in the matrix``directions ``=` `[[``0``, ``1``], [``-``1``, ``0``], [``0``, ``-``1``],``              ``[``1``, ``0``], [``1``, ``1``], [``-``1``, ``-``1``],``              ``[``-``1``, ``1``], [``1``, ``-``1``]]` `# Function to find if the current cell``# lies in the matrix or not`  `def` `isInside(i, j, N, M):` `    ``if` `(i >``=` `0` `and` `i < N ``and` `j >``=` `0` `and` `j < M):``        ``return` `True` `    ``return` `False`  `# Function to perform the DFS Traversal``def` `DFS(mat, N, M, i, j, visited):` `    ``if` `(visited[i][j] ``=``=` `True``):` `        ``return` `    ``visited[i][j] ``=` `True` `    ``# Iterate over the direction vector``    ``for` `it ``in` `directions:``        ``I ``=` `i ``+` `it[``0``]``        ``J ``=` `j ``+` `it[``1``]``        ``if` `(isInside(I, J, N, M)):``            ``if` `(mat[I][J] ``=``=` `0``):` `                ``# DFS Call``                ``DFS(mat, N, M, I, J, visited)`  `# Function to check if it satisfy the``# given criteria or not``def` `check(N, M, mat):` `    ``# Keeps the count of cell having``    ``# value as 0``    ``black ``=` `0` `    ``for` `i ``in` `range``(``0``, N):``        ``for` `j ``in` `range``(``0``, M):``            ``if` `(mat[i][j] ``=``=` `0``):``                ``black ``+``=` `1` `        ``# If condition doesn't satisfy``    ``if` `(black < ``2` `*` `(``max``(N, M))):``        ``print``(``"NO"``)``        ``return` `    ``visited ``=` `[]``    ``for` `i ``in` `range``(``0``, N):``        ``temp ``=` `[]``        ``for` `j ``in` `range``(``0``, M):``            ``temp.append(``False``)` `        ``visited.append(temp)` `        ``# Keeps the track of unvisited cell``        ``# having values 0``    ``black_spots ``=` `0` `    ``for` `i ``in` `range``(``0``, N):``        ``for` `j ``in` `range``(``0``, M):``            ``if` `(visited[i][j] ``=``=` `False` `and` `mat[i][j] ``=``=` `0``):` `                ``# Increasing count of``                ``# black_spot``                ``black_spots ``+``=` `1``                ``DFS(mat, N, M, i, j, visited)` `        ``# Find the GCD of N and M``    ``T ``=` `math.gcd(N, M)` `    ``# Print the result``    ``if` `black_spots >``=` `T:``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `3``    ``M ``=` `3``    ``mat ``=` `[[``0``, ``0``, ``1``], [``1``, ``1``, ``1``], [``0``, ``0``, ``1``]]` `    ``check(M, N, mat)` `    ``# This code is contributed by rakeshsahni`

## Javascript

 ``

Output:

`NO`

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

My Personal Notes arrow_drop_up