ALGOR Contest: Round #2
Problem E · 200 points ·
ICPC rules
~/algor $ cat contests/round-2/E
E. Maze
2000 ms
128 MB
An n × m maze is given: «.» is a free cell, «#» is a wall, «S» is the start, «E» is the exit. In one step you may move to an adjacent (up, down, left or right) free cell; you cannot pass through walls or leave the maze. What is the smallest number of steps from S to E? If E cannot be reached, print −1.
Input
The first line contains n and m. Each of the next n lines contains m characters. S and E each appear exactly once.
Output
One integer — the smallest number of steps or −1.
Note
1 ≤ n, m ≤ 1000; n · m ≥ 2
Examples
Input #1
3 4 S..# .#.. ...E
Output #1
5
Input #2
3 3 S#E .#. .#.
Output #2
-1