#C3373. Taco: Shortest Distance from Land to House

    ID: 46793 Type: Default 1000ms 256MiB

Taco: Shortest Distance from Land to House

Taco: Shortest Distance from Land to House

You are given a grid representing a map, where each cell is either land ('0') or a house ('1'). Your task is to find the shortest Manhattan distance from any land cell to the nearest house cell.

The Manhattan distance between two cells with coordinates \( (i, j) \) and \( (k, l) \) is defined as \( |i-k| + |j-l| \).

If it is impossible to find a house cell that is reachable from any land cell, output -1.

inputFormat

The first line contains two integers \( n \) and \( m \) representing the number of rows and columns in the grid respectively. Each of the following \( n \) lines contains a string of length \( m \) composed of characters '0' and '1', representing the grid.

outputFormat

Output a single integer denoting the shortest distance from any land cell to a house cell. If no such pair exists, output -1.

## sample
3 3
001
010
000
1

</p>