PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000055. Đường đi khép kín (Round Trip)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi khép kín (Round Trip)

Round Trip

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a grid with HH rows and WW columns. Each cell is empty . or blocked #, except for exactly one cell marked S, the starting position. You may move to any of the four edge-adjacent cells and may not enter blocked cells.

Determine whether a walk can start at S, make some moves, and return to S without visiting any other cell more than once. Remaining in place does not count as a move; the route must form a genuine cycle of at least 44 steps.

The final visit to S is permitted even though S was visited at the beginning. Output No if no such route exists.

Input

The first line contains H,WH,W with H,W≥2H,W\ge2, followed by HH strings of length WW.

Output

Print Yes or No.

Subtasks

  • Subtask 1 (20 points): H,W≥2H,W\ge2, HW≤100HW\le100; The grid contains only ., #, S and exactly one cell S.
  • Subtask 2 (30 points): H,W≥2H,W\ge2, HW≤104HW\le10^4; The grid contains only ., #, S and exactly one cell S.
  • Subtask 3 (50 points): H,W≥2H,W\ge2, HW≤106HW\le10^6; The grid contains only ., #, S and exactly one cell S.

Examples

Input

4 4
....
#.#.
.S..
.##.

Output

Yes

Explanation

One valid route is $(3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(1,2)\to(2,2)\to(3,2)$, using 1-based coordinates. It returns to S without repeating an intermediate square, so the answer is Yes.