Good cells (Contest)

Good cells easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Given a boolean Matrix of size N*M, A cell of the matrix is called "Good" if it is completely surrounded by the cells containing '1' i.e. each adjacent cell (which shares a common edge) contains '1'. Your task is to find the number of such cells.

See the below example for a better understanding Input First line of input contains two space- separated integers N and M. Next N lines of input contain M space- separated integers depicting the values of the matrix.

Constraints:- 3 <= N, M <= 500 0 <= Matrix[][] <= 1 Output Print the number of good cells. Example Sample Input:- 3 3 1 1 0 1 1 1 1 1 1

Sample Output:- 1

Explanation:- Only cell at position 1, 1 is good

Sample Input:- 5 4 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0

Sample Output:- 3

Explanation:- (1, 2), (2, 1) and (3, 2) are good cells

link:https://my.newtonschool.co/playground/code/p0zpq11o6uvu/

```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n= sc.nextInt();
        int m=sc.nextInt();
        int[][] a = new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                a[i][j]=sc.nextInt();
            }
        }
        int count =0;
        for(int i=1;i<n-1;i++){
            for(int j=1;j<m-1;j++){
                if(a[i-1][j]==1 && a[i][j-1]==1 && a[i+1][j]==1 && a[i][j+1]==1){
                    count++;
                }
            }
        }
        System.out.print(count);
    }
}
```

Last updated