dfs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2000;
char mp[maxn][maxn];
int n,m;
typedef pair<int,int> pII;
const int dir_x[4]={1,-1,0,0};
const int dir_y[4]={0,0,1,-1};
pII last[maxn][maxn];
bool ret=false;
void dfs(int x,int y)
{
int mx=x%n,my=y%m;
if(mp[mx][my]=='#'||ret||last[mx][my]==pII(x,y)) return;
if(last[mx][my]!=pII(0,0))
{
ret=true; return ;
}
last[mx][my]=pII(x,y);
for(int i=0;i<4;i++)
{
dfs(x+dir_x[i],y+dir_y[i]);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
int sx,sy;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='S')
sx=i,sy=j;
}
}
dfs(sx+n*m,sy+n*m);
if(ret) puts("Yes");
else puts("No");
return 0;
}