#P1363. Escape the Illusory Maze

    ID: 14650 Type: Default 1000ms 256MiB

Escape the Illusory Maze

Escape the Illusory Maze

The maze is infinite and is formed by a repetition of an \(N \times M\) grid. Each cell in the grid is either a road (denoted by .), a wall (denoted by #), or a starting point (denoted by S). In the infinite maze, for any cell with coordinates \((x,y)\), its type is determined by the cell at \((x \bmod N,\; y \bmod M)\) in the given grid. If that cell is either . or S, then the cell \((x,y)\) is a road; if it is #, then it is a wall.

LHX and WD start at the cell marked S and can move up, down, left or right into adjacent cells, but they cannot move into walls. They want to know whether they can escape the maze. We say they can "escape" if they are able to reach cells arbitrarily far away from their starting point. Otherwise, if they are trapped in a finite region, then escape is impossible.

Your task is to determine whether the infinite maze contains an unbounded reachable area from the starting point.

inputFormat

The first line contains two integers \(N\) and \(M\) (the dimensions of the repeated grid pattern). Each of the next \(N\) lines contains a string of length \(M\) consisting of characters ., # and S. It is guaranteed that there is exactly one S in the grid.

outputFormat

Output a single line containing Yes if LHX and WD can escape the maze (i.e. reach points arbitrarily far from the starting point), or No otherwise.

sample

2 2
S.
..
Yes

</p>