Barrett has discovered an ancient maze under his house. It has the shape of an $n \times m$ grid, where some cells are empty, while others are blocked. It is possible to walk from one empty cell to another if they share a~side. Two of the empty cells are an entrance and an exit, and it is possible to reach one from the other by walking through empty cells.

Barrett wants to isolate his house by building a wall inside the maze, blocking some cells to make the exit unreachable from the entrance. The wall should be straight and oriented either vertically or horizontally. Specifically, a wall of length $k$ will block a consecutive row or column of exactly $k$ cells. The wall may not contain the entrance, the exit, or any already blocked cells.

Help Barrett determine the minimum possible length of the wall.