#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <string.h>
#define elif else if
using namespace std;
struct inf {int x, y;};
int n, m, sx, sy, s, Lx[4] = {0, 0, 1, -1}, Ly[4] = {1, -1, 0, 0}, i, j, x, y, xi, yi;
char c;
queue <inf> q;
signed main () {
cin >> n >> m;
int vip[n + 3][m + 3]; memset(vip, 0, sizeof(vip));
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
cin >> c;
if (c == '#') {
vip[i][j] = 1;
} if (c == 'S') {
sx = i; sy = j;
vip[i][j] = 1;
}
}
} for (i = 0; i < 4; ++i) {
x = sx + Lx[i]; y = sy + Ly[i];
if (x < 1 or x > n or y < 1 or y > m) {
continue;
} if (!(vip[x][y])) {
vip[x][y] = (++s); while (q.size() > 0) {
q.pop();
} q.push({x, y});
while (q.size()) {
inf t = q.front();
q.pop();
for (j = 0; j < 4; ++j) {
xi = t.x + Lx[j]; yi = t.y + Ly[j];
if (xi < 1 or xi > n or yi < 1 or yi > m or vip[xi][yi]) {
continue;
} vip[xi][yi] = s;
q.push({xi, yi});
}
}
} elif (vip[x][y] != 1) {
puts("Yes"); return 0;
}
}
puts("No");
return 0;
}
谢谢,QaQ