cho.sh
Notes
Loading...

Mold

Time limit

2s

Memory limit

128 MB

Problem

Mold is growing on a wall. At first, the mold is split into several clusters. Determine how many days it takes until all mold cells form one cluster.

The wall is divided into an m by n grid. Mold cells that are adjacent horizontally or vertically belong to the same cluster.

Initially, every mold cell in the same cluster is the same species and has the same growth speed. Mold cells in different clusters may be different species, and their growth speeds may differ. As time passes, clusters of different species can touch and become one cluster.

If a mold has growth speed k, then after one day it spreads its species to every cell in the (2k+1) by (2k+1) square centered at that mold cell. If different species spread to the same cell, the species with the higher growth speed occupies that cell.

Input

The first line contains two integers m and n, the size of the wall. (1 <= m, n <= 100)

The next m lines describe the wall, one row per line. A cell containing mold is written as that mold's growth speed, and an empty cell is written as 0. Each growth speed is an integer from 1 to 5. There are no spaces between digits in a row.

Output

Print the number of days needed until all mold cells form one cluster.

Hint

For the visible test, the wall changes over time as follows.

222220000111111222220000111111222220111111111222220111111111222200111111111
222222201111111222222211111111222222211111111222222211111111222222211111111
222220000111111222220000111111222220111111111222220111111111222200111111111
222222201111111222222211111111222222211111111222222211111111222222211111111