The planet is an N by M grid. Each cell is either empty (0) or a village (1). You must place exactly 2 poison bags, each on an empty cell.
Every second, the poison spreads to the four neighboring cells (up, down, left, right). A poisoned village infects each adjacent village after 1 second, so the spread is equivalent to movement through every cell of the grid. A village at (x,y) becomes poisoned at the smaller of its Manhattan distances to the two bags, and the time to poison every village is the maximum of those values over all villages. Find the placement that minimizes this maximum.
Rows are numbered from 0 to N−1 and columns from 0 to M−1, with the top-left cell at (0,0). The input always admits a placement.