Taxicab Distance
InterviewTime limit1sMemory limit256 MB
Given an N by M grid of 0s and 1s with exactly two 1s, print the Manhattan distance between them.
- Level
Easy2 of 10
- Topics
- Array, Implementation, Math, Brute force
- Solved
- No attempts yet
Problem

In a city whose roads form a grid, the taxicab distance between points A and B is the length of the shortest path from A to B that stays on the roads.
In the picture above, the red line is the taxicab distance. If point A has coordinates and point B has coordinates , the taxicab distance between the two points is
Assume the distance between two cells that share a horizontal or vertical side is 1. Write a program that finds the distance between the two 1s.
Input
The first line gives the height and the width of the grid.
Starting from the second line, lines each contain digits, 0 or 1, separated by spaces as in the sample input.
There are always exactly two 1s.
Output
Print the taxicab distance between the two 1s in the given grid.