#K94257. Minimum Moves to Convert Grid

    ID: 38601 Type: Default 1000ms 256MiB

Minimum Moves to Convert Grid

Minimum Moves to Convert Grid

You are given a grid with H rows and W columns where each cell is either '0' or '1'. In one move, every cell that is adjacent (up, down, left, right) to a cell with value '1' will be converted to '1'. Your task is to determine the minimum number of moves required so that all cells in the grid become '1'.

Note: It is guaranteed that the grid will always contain at least one cell with '1' initially, so the process will eventually convert every cell to '1'.

The process can be modeled as a multi-source Breadth-First Search (BFS), where all initial '1' cells are used as sources and the maximum BFS level required to cover all cells gives the answer.

The problem is challenging because you have to handle grid traversal efficiently and manage multiple starting points simultaneously.

inputFormat

The first line contains two integers H and W, separated by a space, indicating the number of rows and columns in the grid respectively.

The following H lines each contain a string of length W consisting only of the characters '0' and '1', representing the grid.

Input is provided via standard input (stdin).

outputFormat

Output a single integer representing the minimum number of moves required to convert all cells in the grid to '1'.

Output should be printed to standard output (stdout).

## sample
3 3
111
110
001
1