Travel diaries

0

0 votes
Queue, Graphs, Graph Theory, Algorithms, Medium, Grammar-Verified, Open, Breadth-first search
Problem

You are given a matrix of size N×M that contains the digits 0, 1, or 2 only. All the cells that contain 1 and are adjacent to any cell that contains 2 gets converted from 1 to 2. This operation must be performed simultaneously in 1 second. Write a program to find the minimum time to convert all the cells having value 1 to 2.

Input format

  • First line: Two space-separated integers N and M
  • Next N lines: M space-separated integers denoting the matrix

Output format

Print the minimum time to convert all the cells having value 1 to 2.

Constraints

1N,M103

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

If Monk starts from the cell [1,4] or [3,4] and travels to [2,3] then the cost will be 2 which is maximum of all possible journeys.

Editor Image

?