Replace 0s

Moderate
0/80
Average time to solve is 30m
10 upvotes
Asked in companies
AmazonAmerican Express

Problem statement

Given a matrix where every element is either 1 or 0(zero), replace 0 with 1 if surrounded by 1. A 0 (or a set of 0s) is considered to be surrounded by 1 if there are 1 at locations just below, just above, just left and just right of it.

See the sample input.

Detailed explanation ( Input/output format, Notes, Images )
Input format :
Line 1 : Number of rows M and cols N in the matrix
Line 2 : MXN elements of the matrix separated by space 
Output Format :
 Just make the changes if any, no need to print or return the matrix.
Constraints :
 1 <= M <= 10^3
 1 <= N <= 10^3
Sample Input :
3 4
1 1 1 1 
1 0 0 1 
1 1 1 0
Sample Output :
1 1 1 1 
1 1 1 1 
1 1 1 0 
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Replace 0s
Full screen
Console