AlgoUniversity
  • Go Back
Discussion
Rotting Oranges :

Author

hardik kapoor

Difficulty Level : Easy

Submissions : 134

Asked In :

Marks :10

: 2 | : 0

You are given an n x m grid where each cell can have one of three values:

  • 0 representing an empty cell,
  • 1 representing a fresh orange, or
  • 2 representing a rotten orange.

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

Input

The first line contains N (1 <= N <= 1e4) and M (1 <= M <= 1e4) , the size of the grid.

Next N lines contain M integers, 0 1 or 2, representing the grid.

N*M <= 1e4 for all test cases

Output

Output a single integer, the minimum number of minutes.

Examples

Input
3 3
2 1 1
1 1 0
0 1 1
Output
4
Input
3 3
2 1 1
0 1 1
1 0 1
Output
-1

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.