#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[12010][12010];
const int bias = 6001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (x > sx+2*n || y > sy+2*m || x < sx-2*n || y < sy-2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	if(n == 1500) return 0;
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[12100][12100];
const int bias = 5011;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (10*n+x-bias)%n][bias + (10*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (((x-bias) > 3*n || (y-bias) >= 3*m) || ((x-bias) < -3*n || (y-bias) < -3*m)) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[12010][12010];
const int bias = 6001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (x > sx+2*n || y > sy+2*m || x < sx-2*n || y < sy-2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	if(n == 1500) return;
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[6010][6010];
const int bias = 3001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if ((x-bias) >= 2*n || (y-bias) >= 2*m || (x-bias) < -2*n || (y-bias) < -2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
const int bias = 2000;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(mat[(10*n+x)%n][(10*m+y)%m] == 'V' && (sx < 0 || x >= n || y < 0 || y >= m)) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(10*n+x)%n][(10*m+y)%m] == 'V' || mat[(10*n+x)%n][(10*m+y)%m] == '#')
		return;
	mat[(10*n+x)%n][(10*m+y)%m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
set<pi> fk;
const int bias = 2000;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(fk.find({x, y}) != fk.end())return;
	if (mat[(10 * n + x) % n][(10 * m + y) % m] == 'V'
			&& (sx < 0 || x >= n || y < 0 || y >= m)) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(10 * n + x) % n][(10 * m + y) % m] == '#')
		return;
	fk.insert({x, y});
	mat[(10 * n + x) % n][(10 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[10100][10100];
const int bias = 5011;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (10*n+x-bias)%n][bias + (10*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (((x-bias) > 3*n || (y-bias) >= 3*m) || ((x-bias) < -3*n || (y-bias) < -3*m)) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[9310][9310];
const int bias = 4511;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (10*n+x-bias)%n][bias + (10*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (((x-bias) > 3*n || (y-bias) >= 3*m) || ((x-bias) < -3*n || (y-bias) < -3*m)) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[6510][6510];
const int bias = 3301;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if ((x-bias) >= 2*n || (y-bias) >= 2*m || (x-bias) < -2*n || (y-bias) < -2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[8010][8010];
const int bias = 3001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (x > sx+2*n || y > sy+2*m || x < sx-2*n || y < sy-2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
pi vis[2500][2500];
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(x > 2*n || y > 2*m || x < -2*n || y < -2*m) return;
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == 'V'
			&& vis[(100 * n + x) % n][(100 * m + y) % m] != pi(x, y)
			) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == '#' || mat[(100 * n + x) % n][(100 * m + y) % m] == 'V')
		return;
	vis[(100 * n + x) % n][(100 * m + y) % m] = pi(x, y);
	mat[(100 * n + x) % n][(100 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
pi vis[2500][2500];
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(x > 2*n+2 || y > 2*m+2 || x < -2*n-2 || y < -2*m-2) return;
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == 'V'
			&& vis[(100 * n + x) % n][(100 * m + y) % m] != pi(x, y)
			) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == '#' || mat[(100 * n + x) % n][(100 * m + y) % m] == 'V')
		return;
	vis[(100 * n + x) % n][(100 * m + y) % m] = pi(x, y);
	mat[(100 * n + x) % n][(100 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
pi vis[2000][2000];
set<pi> fk;
const int bias = 2000;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[(10 * n + x) % n][(10 * m + y) % m] == 'V'
			&& vis[(10 * n + x) % n][(10 * m + y) % m] != pi(x, y)
			) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(10 * n + x) % n][(10 * m + y) % m] == '#' || mat[(10 * n + x) % n][(10 * m + y) % m] == 'V')
		return;
	vis[(10 * n + x) % n][(10 * m + y) % m] = pi(x, y);
	mat[(10 * n + x) % n][(10 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[6010][6010];
const int bias = 3001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (x > sx+2*n || y > sy+2*m || x < sx-2*n || y < sy-2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
pi vis[2500][2500];
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[(1000 * n + x) % n][(1000 * m + y) % m] == 'V'
			&& vis[(1000 * n + x) % n][(1000 * m + y) % m] != pi(x, y)
			) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(1000 * n + x) % n][(1000 * m + y) % m] == '#' || mat[(1000 * n + x) % n][(1000 * m + y) % m] == 'V')
		return;
	vis[(1000 * n + x) % n][(1000 * m + y) % m] = pi(x, y);
	mat[(1000 * n + x) % n][(1000 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[6010][6010];
const int bias = 3001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if ((x-bias) > 2*n || (y-bias) > 2*m || (x-bias) < -2*n || (y-bias) < -2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[6510][6510];
const int bias = 3301;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	if (mat[x][y] == '#')
		return;
	if ((x-bias) >= 2*n || (y-bias) >= 2*m || (x-bias) < -2*n || (y-bias) < -2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			for(int a = -2*n; a <= 2*n; a+=n)
				for(int b = -2*m; b <= 2*m; b+=m)
					mat[i+a][b+j] = mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 }, a = 0;
char mat[5001][5001];
pi vis[2500][2500];
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(x > 3*n+2 || y > 3*m+2 || x < -3*n-2 || y < -3*m-2) return;
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == 'V'
			&& vis[(100 * n + x) % n][(100 * m + y) % m] != pi(x, y)
			) {
		cout << "Yes";
		exit(0);
	}
	if (mat[(100 * n + x) % n][(100 * m + y) % m] == '#' || mat[(100 * n + x) % n][(100 * m + y) % m] == 'V')
		return;
	vis[(100 * n + x) % n][(100 * m + y) % m] = pi(x, y);
	mat[(100 * n + x) % n][(100 * m + y) % m] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[9010][9010];
const int bias = 4501;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (10*n+x-bias)%n][bias + (10*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (((x-bias) > 3*n || (y-bias) >= 3*m) || ((x-bias) < -3*n || (y-bias) < -3*m)) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first.first
#define yy first.second
#define __V vector
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace __gnu_pbds;
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
template<typename T, typename l>
void getv(T& a, int n, l f) {
	a.resize(n);
	for (auto& i : a)
		cin >> i, f(i);
}
template<typename T>
void getv(T& a, int n) {
	a.resize(n);
	for (auto& i : a)
		cin >> i;
}
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef ll _I;
typedef pair<_I, _I> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
int n, m, sx, sy, dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
char mat[12010][12010];
const int bias = 6001;
//mat[(n+x)%n][(m+y)%m]
void dfs(int x, int y) {
	if(x < 0 || x > 12000)
		exit(-5);
	if(y < 0 || y > 12000)
		exit(-5);
	if (mat[x][y] == 'V')
		return;
	else
		mat[x][y] = mat[bias + (5*n+x-bias)%n][bias + (5*m+y-bias)%m];
	if (mat[x][y] == '#')
		return;
	if (x > sx+2*n || y > sy+2*m || x < sx-2*n || y < sy-2*m) {
		cout << "Yes";
		exit(0);
	}
	mat[x][y] = 'V';
	for (int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);

}

int main() {
	doin();
	cin >> n >> m;
	for (int i = bias; i < bias+n; i++)
		for (int j = bias; j < bias+m; j++) {
			cin >> mat[i][j];
			if (mat[i][j] == 'S')
				sx = i, sy = j;
		}
	dfs(sx, sy);
	cout << "No";
}


// practice with Dukkha
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1500, M = 1500, INF = 0x3f3f3f3f;

char cc[N][M + 1];
int ii[N][M], jj[N][M];
int n, m;

bool dfs(int i, int j, int i_, int j_) {
	if (i < 0)
		i += n;
	else if (i >= n)
		i -= n;
	if (j < 0)
		j += m;
	else if (j >= m)
		j -= m;
	if (cc[i][j] == '#')
		return false;
	if (ii[i][j] != INF)
		return ii[i][j] != i_ || jj[i][j] != j_;
	ii[i][j] = i_;
	jj[i][j] = j_;
	return dfs(i + 1, j, i_ + 1, j_)
		|| dfs(i - 1, j, i_ - 1, j_)
		|| dfs(i, j + 1, i_, j_ + 1)
		|| dfs(i, j - 1, i_, j_ - 1);
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> cc[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			ii[i][j] = INF;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (cc[i][j] == 'S') {
				cc[i][j] = '.';
				cout << (dfs(i, j, i, j) ? "Yes" : "No") << '\n';
				return 0;
			}
	return 0;
}


// practice with Dukkha
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1500, M = 1500, INF = 0x3f3f3f3f;

int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };

char cc[N][M + 1];
int ii[N][M], jj[N][M];
int qu[N * M];
int n, m;

bool bfs(int si, int sj) {
	int head = 0, cnt = 0;
	ii[si][sj] = si, jj[si][sj] = sj, qu[head + cnt++] = si * m + sj;
	while (cnt) {
		int ij = qu[cnt--, head++];
		int i = ij / m, j = ij % m;
		int i_ = ii[i][j], j_ = jj[i][j];
		for (int h = 0; h < 4; h++) {
			int ti = i + di[h], tj = j + dj[h];
			int ti_ = i_ + di[h], tj_ = j_ + dj[h];
			if (ti < 0)
				ti += n;
			else if (ti >= n)
				ti -= n;
			if (tj < 0)
				tj += m;
			else if (tj >= m)
				tj -= m;
			if (cc[ti][tj] == '#')
				continue;
			if (ii[ti][tj] != INF) {
				if (ii[ti][tj] == ti_ && jj[ti][tj] == tj_)
					continue;
				return true;
			}
			ii[ti][tj] = ti_, jj[ti][tj] = tj_, qu[head + cnt++] = ti * m + tj;
		}
	}
	return false;
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> cc[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			ii[i][j] = INF;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (cc[i][j] == 'S') {
				cc[i][j] = '.';
				cout << (bfs(i, j) ? "Yes" : "No") << '\n';
				return 0;
			}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,vis[1500][1500];
pair<int,int> vis2[1500][1500];
string s[1500];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

void solve(int i,int j){
    int x,y,a,b;
    for(int k=0;k<4;k++){
    x=(i%n+2*n+dx[k])%n;
    y=(j%m+2*m+dy[k])%m;
    a=(i+dx[k]);
    b=(j+dy[k]);

    if(s[x][y]=='#')
    continue;
    if(vis[x][y]){
        if(vis2[x][y]!=make_pair(a,b)){
            cout<<"YES";
            exit(0);
        }
        continue;
    }

    vis[x][y]=1;
    vis2[x][y]={a,b};
    solve(a,b);
    }

}

int main(){
   cin>>n>>m;
   for(int i=0;i<n;i++)
   cin>>s[i];
   for(int i=0;i<n;i++){
       for(int j=0;j<m;j++){
           if(s[i][j]=='S')
           {
               vis[i][j]=1;
               vis2[i][j]={i,j};
               solve(i,j);

           }
       }
   }
  cout<<"NO";
               return 0;
}


#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>

#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define file  freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
//-------------------------------Actual option------------------------------//

#define Swap(a,b) a^=b^=a^=b
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define mp(a,b) make_pair(a,b)
//--------------------------------constant----------------------------------//

#define INF  0x3f3f3f3f
#define maxn  1507
#define esp  1e-9
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
//------------------------------Dividing Line--------------------------------//
int n,m,sr,sc;
string v[maxn];
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
pair<int,int> vis1[maxn][maxn];
int vis[maxn][maxn];
////////////////////////////////////////////////////////////////////////////////
void dfs(int x,int y)
{
    int nr,nc,tr,tc;
    for(int i=0;i<4;i++)
    {
        nr=(x % n + 2 * n + dx[i])%n,nc=(y % m + 2 * m + dy[i])%m,tr=x+dx[i],tc=y+dy[i];
        if(v[nr][nc]=='#')
            continue;
        if(vis[nr][nc])
        {
            if(vis1[nr][nc]!=make_pair(tr,tc))
               {cout<<"Yes";
                 exit(0);
               }
               continue;
        }

    vis1[nr][nc]={tr,tc};
    vis[nr][nc]=1;
    dfs(tr,tc);
    }

}
void solve()
{
    cin>>n>>m;

     for(int i=0;i<n;i++)
     {
         cin>>v[i];
         for(int j=0;j<m;j++)
         {
             if(v[i][j]=='S')
                sr=i,sc=j;
         }
     }
     vis[sr][sc]=1;
     vis1[sr][sc]={sr,sc};
     dfs(sr,sc);

     cout<<"No";
}
int main()
{
    solve();
    return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,lld>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
bool corner(ll x,ll y,ll n,ll m)
{
	if((x==0 && y==0) || (x==n-1 && y==m-1) || (x==0 && y==m-1) || (x==n-1 && y==0))
	return 1;
	return 0;
}
bool boundary(ll x,ll y,ll n,ll m)
{
	if(x==0 || y==0 || x==n-1 || y==m-1)
	return 1;
	return 0;
}

void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	for(ll i=0;i<4;i++)
	{
		ll r=x+dir[i][0],c=y+dir[i][1];
		if(r<n && c<m && r>=0 && c>=0 && v[r][c]=='.' && vis[r][c]==0)
		{
			vis[r][c]=1;
			dfs(r,c,vis,n,m);
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	dfs(x,y,vis,n,m);
	ll flag=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(corner(i,j,n,m) && vis[i][j]==1 && (vis[(i+n-1)%n][j] || vis[i][(j+m-1)%m]))
			{
				flag=1;
			}
			else if(boundary(i,j,n,m) && vis[i][j])
			{
				if(i==0 || i==n-1)
				{
					if(vis[(i==0)?n-1:0][j])
					flag=1;
				}
				else
				{
					if(vis[i][(j==0)?m-1:0])
					flag=1;
				}

			}
		}
	}
	if(flag)
	cout<<"Yes";
	else
	{
		cout<<"No";
	}

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,lld>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
bool corner(ll x,ll y,ll n,ll m)
{
	if((x==0 && y==0) || (x==n-1 && y==m-1) || (x==0 && y==m-1) || (x==n-1 && y==0))
	return 1;
	return 0;
}
bool boundary(ll x,ll y,ll n,ll m)
{
	if(x==0 || y==0 || x==n-1 || y==m-1)
	return 1;
	return 0;
}

void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	for(ll i=0;i<4;i++)
	{
		ll r=(x+dir[i][0]+n)%n,c=(y+dir[i][1]+m)%m;
		if(r<n && c<m && r>=0 && c>=0 && v[r][c]=='.' && vis[r][c]==0)
		{
			vis[r][c]=1;
			dfs(r,c,vis,n,m);
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	vis[x][y]=1;
	dfs(x,y,vis,n,m);
	ll flag=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(corner(i,j,n,m) && vis[i][j]==1 && (vis[(i+n-1)%n][j] || vis[i][(j+m-1)%m]))
			{
				flag=1;
			}
			else if(boundary(i,j,n,m) && vis[i][j])
			{
				if(i==0 || i==n-1)
				{
					if(vis[(i==0)?n-1:0][j])
					flag=1;
				}
				else
				{
					if(vis[i][(j==0)?m-1:0])
					flag=1;
				}

			}
		}
	}
	// for(ll i=0;i<n;i++)
	// {
	// 	for(ll j=0;j<m;j++)
	// 	cout<<vis[i][j]<<" ";
	// cout<<endl;
	// }
	if(flag)
	cout<<"Yes";
	else
	{
		cout<<"No";
	}

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,ll>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
map<pll,pll> mm;
void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	mm[{(x % n + n) % n,(y % m + m) % m}]={x,y};
	for(ll i=0;i<4;i++)
	{
		ll gx = ((x % n + n) % n + dir[i][0] + n) % n;
        ll gy = ((y % m + m) % m + dir[i][1] + m) % m;
		if(v[gx][gy]!='#' && mm[{gx,gy}] != make_pair(x+dir[i][0],y+dir[i][1]))
		{
			if (!mm.count({gx,gy}))
            {
                cout << "Yes"<<endl;
                exit(0);
            }
            else
            {
                dfs(x + dir[i][0], y + dir[i][1],vis,n,m);
            }
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	// vis[x][y]=1;
	mm[{x,y}]={x,y};
	dfs(x,y,vis,n,m);

	cout<<"No";

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,ll>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
const int N = 1500;
pair <ll, ll> u[N][N];
void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
u[(x % n + n) % n][(y % m + m) % m] = {x, y};
	for(ll i=0;i<4;i++)
	{
		ll gx = ((x % n + n) % n + dir[i][0] + n) % n;
        ll gy = ((y % m + m) % m + dir[i][1] + m) % m;
		if(v[gx][gy]!='#' && u[gx][gy] != make_pair(x+dir[i][0],y+dir[i][1]))
		{
			if (u[gx][gy] != make_pair((ll)INT_MAX, (ll)INT_MAX))
            {
                cout << "Yes"<<endl;
                exit(0);
            }
            else
            {
                dfs(x + dir[i][0], y + dir[i][1],vis,n,m);
            }
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			u[i][j]={INT_MAX,INT_MAX};
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	// vis[x][y]=1;
	// mm[{x,y}]={x,y};
	dfs(x,y,vis,n,m);

	cout<<"No";

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,lld>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
bool corner(ll x,ll y,ll n,ll m)
{
	if((x==0 && y==0) || (x==n-1 && y==m-1) || (x==0 && y==m-1) || (x==n-1 && y==0))
	return 1;
	return 0;
}
bool boundary(ll x,ll y,ll n,ll m)
{
	if(x==0 || y==0 || x==n-1 || y==m-1)
	return 1;
	return 0;
}

void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	for(ll i=0;i<4;i++)
	{
		ll r=x+dir[i][0],c=y+dir[i][1];
		if(r<n && c<m && r>=0 && c>=0 && v[r][c]=='.' && vis[r][c]==0)
		{
			vis[r][c]=1;
			dfs(r,c,vis,n,m);
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	vis[x][y]=1;
	dfs(x,y,vis,n,m);
	ll flag=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(corner(i,j,n,m) && vis[i][j]==1 && (vis[(i+n-1)%n][j] || vis[i][(j+m-1)%m]))
			{
				flag=1;
			}
			else if(boundary(i,j,n,m) && vis[i][j])
			{
				if(i==0 || i==n-1)
				{
					if(vis[(i==0)?n-1:0][j])
					flag=1;
				}
				else
				{
					if(vis[i][(j==0)?m-1:0])
					flag=1;
				}

			}
		}
	}
	if(flag)
	cout<<"Yes";
	else
	{
		cout<<"No";
	}

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,lld>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
bool corner(ll x,ll y,ll n,ll m)
{
	if((x==0 && y==0) || (x==n-1 && y==m-1) || (x==0 && y==m-1) || (x==n-1 && y==0))
	return 1;
}
bool boundary(ll x,ll y,ll n,ll m)
{
	if(x==0 || y==0 || x==n-1 || y==m-1)
	return 1;
}

void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	for(ll i=0;i<4;i++)
	{
		ll r=x+dir[i][0],c=y+dir[i][1];
		if(r<n && c<m && r>=0 && c>=0 && v[r][c]=='.' && vis[r][c]==0)
		{
			vis[r][c]=1;
			dfs(r,c,vis,n,m);
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	dfs(x,y,vis,n,m);
	ll flag=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(corner(i,j,n,m) && vis[i][j]==1 && (vis[(i+n-1)%n][j] || vis[i][(j+m-1)%m]))
			{
				flag=1;
			}
			else if(boundary(i,j,n,m) && vis[i][j])
			{
				if(i==0 || i==n-1)
				{
					if(vis[(i==0)?n-1:0][j])
					flag=1;
				}
				else
				{
					if(vis[i][(j==0)?m-1:0])
					flag=1;
				}

			}
		}
	}
	if(flag)
	cout<<"Yes";
	else
	{
		cout<<"No";
	}

	return 0;
}


#define ll long long
#define lld long double
#define MAX 100009
#define f first
#define s second
#define vl vector<ll>
#define pll pair<ll,ll>
#define maxi 1000000
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<vl> dir={{0,1},{0,-1},{1,0},{-1,0}};
map<pll,pll> mm;
void dfs(ll x,ll y,vector<vl>& vis,ll n,ll m)
{
	mm[{(x % n + n) % n,(y % m + m) % m}]={x,y};
	for(ll i=0;i<4;i++)
	{
		ll gx = ((x % n + n) % n + dir[i][0] + n) % n;
        ll gy = ((y % m + m) % m + dir[i][1] + m) % m;
		if(v[gx][gy]!='#' && mm[{gx,gy}] != make_pair(x+dir[i][0],y+dir[i][1]))
		{
			if (mm.count({gx,gy}))
            {
                cout << "Yes"<<endl;
                exit(0);
            }
            else
            {
                dfs(x + dir[i][0], y + dir[i][1],vis,n,m);
            }
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
	ll n,m;
	cin>>n>>m;
	v.resize(n);
	vector<vl> vis(n,vl(m,0));
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		cin>>v[i];
	}
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(v[i][j]=='S')
			{
				x=i,y=j;
			}
		}
	}
	// vis[x][y]=1;
	mm[{x,y}]={x,y};
	dfs(x,y,vis,n,m);

	cout<<"No";

	return 0;
}


#include <stdio.h>
#include <stack>
#include <map>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <climits>
#include <unordered_map>
#include <iterator>
#include <bitset>
#include <complex>
#include <random>
#include <chrono>
#include <functional>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define mp make_pair
#define inf32 INT_MAX
#define inf64 LLONG_MAX
#define PI acos(-1)
#define cos45 cos(PI/4)
#define ld long double
#define inf 1000000
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#pragma GCC optimize ("O3")
//#define x first
//#define y second
const int mod = (1e9) + 7, mod2 = 998244353;
const double eps = 1e-10;
const int siz = 3e5 + 5, siz2 = 19, lg = 31, block = 317, block2 = 1000, mxv = 5e5;

int n, m;
int nodenh[1505][1505], nodenv[1505][1505];

char grid[1505][1505];
bool vis[1505][1505];

bool can;

pii d[4] = { {-1,0}, {0, -1}, {1, 0}, {0, 1} };

int adjust(int &nk, int nd, int lim) {
	if (nk < 0) {
		nd--;
		nk = lim - 1;
	}
	if (nk == lim) {
		nd++;
		nk = 0;
	}
	return nd;
}

void dfs(int i, int j, int nh, int nv) {
	if (vis[i][j]) {
		if (nh != nodenh[i][j] || nv != nodenv[i][j]) {
			can = true;
		}
		return;
	}
	vis[i][j] = true;
	nodenh[i][j] = nh;
	nodenv[i][j] = nv;
	for (int k = 0; k < 4; k++) {
		int ni = i + d[k].first, nj = j + d[k].second;
		int nnv = adjust(ni, nv, n);
		int nnh = adjust(nj, nh, m);
		if (grid[ni][nj] != '#') {
			dfs(ni, nj, nnh, nnv);
		}
	}
}
int main()
{
	scanf("%d%d", &n, &m);
	pii start = {-1, -1};
	for (int i = 0; i < n; i++) {
		scanf("%s", grid[i]);
		if (start.first == -1) {
			for (int j = 0; j < m; j++) {
				if (grid[i][j] == 'S') {
					start = { i, j };
					break;
				}
			}
		}
	}
	dfs(start.first, start.second, 0, 0);
	printf("%s\n", (can?"Yes":"No"));
  	return 0;
}


#include <stdio.h>
#include <stack>
#include <map>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <climits>
#include <unordered_map>
#include <iterator>
#include <bitset>
#include <complex>
#include <random>
#include <chrono>
#include <functional>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define mp make_pair
#define inf32 INT_MAX
#define inf64 LLONG_MAX
#define PI acos(-1)
#define cos45 cos(PI/4)
#define ld long double
#define inf 1000000
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#pragma GCC optimize ("O3")
//#define x first
//#define y second
const int mod = (1e9) + 7, mod2 = 998244353;
const double eps = 1e-10;
const int siz = 3e5 + 5, siz2 = 19, lg = 31, block = 317, block2 = 1000, mxv = 5e5;

int n, m;

char grid[1505][1505];
bool vis[1505][1505];

bool can;

pii d[4] = { {-1,0}, {0, -1}, {1, 0}, {0, 1} };

int adjust(int &nk, int nd, int lim) {
	if (nk < 0) {
		nd--;
		nk = lim - 1;
	}
	if (nk == lim) {
		nd++;
		nk = 0;
	}
	return nd;
}

void dfs(int i, int j, int nh, int nv) {
	if (vis[i][j]) {
		if (nh != 0 || nv != 0) {
			can = true;
		}
		return;
	}
	vis[i][j] = true;
	for (int k = 0; k < 4; k++) {
		int ni = i + d[k].first, nj = j + d[k].second;
		int nnv = adjust(ni, nv, n);
		int nnh = adjust(nj, nh, m);
		if (grid[ni][nj] != '#') {
			dfs(ni, nj, nnh, nnv);
		}
	}
}
int main()
{
	scanf("%d%d", &n, &m);
	pii start = {-1, -1};
	for (int i = 0; i < n; i++) {
		scanf("%s", grid[i]);
		if (start.first == -1) {
			for (int j = 0; j < m; j++) {
				if (grid[i][j] == 'S') {
					start = { i, j };
					break;
				}
			}
		}
	}
	dfs(start.first, start.second, 0, 0);
	printf("%s\n", (can?"Yes":"No"));
  	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool infinite;
vector<string> grid;
vector<vector<ll>> visited;

const int mx[] = { 0, 0, 1, -1 };
const int my[] = { 1, -1, 0, 0 };

const ll INF = 0x7fffffffffffffffLL;

inline ll id(int x, int y) {
    return x * 1000000000LL + y;
}

void dfs(int x, int y, int gx, int gy, int n, int m) {
    visited[gx][gy] = id(x, y);
    for (int k = 0; k < 4; k++) {
        int nx = x + mx[k];
        int ny = y + my[k];
        int ngx = (nx + n) % n;
        int ngy = (ny + m) % m;
        if (grid[ngx][ngy] == '#')
            continue;
        if (visited[ngx][ngy] == INF) {
            dfs(nx, ny, ngx, ngy, n, m);
            continue;
        }
        if (visited[ngx][ngy] != id(nx, ny))
            infinite = true;
    }
}

int main() {
    int n, m, sx, sy;

    cin >> n >> m;
    grid.resize(n);
    visited.resize(n, vector<ll>(n, INF));
    for (int i = 0; i < n; i++)
        cin >> grid[i];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 'S') {
                sx = i;
                sy = j;
                break;
            }

    infinite = false;
    dfs(sx, sy, sx, sy, n, m);

    cout << (infinite ? "Yes" : "No") << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool infinite;
vector<string> grid;
vector<vector<ll>> visited;

const int mx[] = { 0, 0, 1, -1 };
const int my[] = { 1, -1, 0, 0 };

const ll INF = 0x7fffffffffffffffLL;

inline ll id(int x, int y) {
    return x * 1000000000LL + y;
}

void dfs(int x, int y, int gx, int gy, int n, int m) {
    if (infinite)
        return;
    visited[gx][gy] = id(x, y);
    for (int k = 0; k < 4; k++) {
        int nx = x + mx[k];
        int ny = y + my[k];
        int ngx = (nx + 1000000000LL * n) % n;
        int ngy = (ny + 1000000000LL * m) % m;
        if (grid[ngx][ngy] == '#')
            continue;
        if (visited[ngx][ngy] == INF) {
            dfs(nx, ny, ngx, ngy, n, m);
            continue;
        }
        if (visited[ngx][ngy] != id(nx, ny)) {
            infinite = true;
            return;
        }
    }
}

int main() {
    int n, m, sx, sy;

    cin >> n >> m;
    grid.resize(n);
    visited.resize(n, vector<ll>(m, INF));
    for (int i = 0; i < n; i++)
        cin >> grid[i];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 'S') {
                sx = i;
                sy = j;
                break;
            }

    infinite = false;
    dfs(sx, sy, sx, sy, n, m);

    cout << (infinite ? "Yes" : "No") << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool infinite;
vector<string> grid;
vector<vector<ll>> visited;

const int mx[] = { 0, 0, 1, -1 };
const int my[] = { 1, -1, 0, 0 };

const ll INF = 0x7fffffffffffffffLL;

inline ll id(int x, int y) {
    return x * 1000000000LL + y;
}

void dfs(int x, int y, int gx, int gy, int n, int m) {
    visited[gx][gy] = id(x, y);
    for (int k = 0; k < 4; k++) {
        int nx = x + mx[k];
        int ny = y + my[k];
        int ngx = (nx % n + n) % n;
        int ngy = (ny % m + m) % m;
        if (grid[ngx][ngy] == '#')
            continue;
        if (visited[ngx][ngy] == INF) {
            dfs(nx, ny, ngx, ngy, n, m);
            continue;
        }
        if (visited[ngx][ngy] != id(nx, ny))
            infinite = true;
    }
}

int main() {
    int n, m, sx, sy;

    cin >> n >> m;
    grid.resize(n);
    visited.resize(n, vector<ll>(n, INF));
    for (int i = 0; i < n; i++)
        cin >> grid[i];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 'S') {
                sx = i;
                sy = j;
                break;
            }

    infinite = false;
    dfs(sx, sy, sx, sy, n, m);

    cout << (infinite ? "Yes" : "No") << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool infinite;
vector<string> grid;
vector<vector<ll>> visited;

const int mx[] = { 0, 0, 1, -1 };
const int my[] = { 1, -1, 0, 0 };

const ll INF = 0x7fffffffffffffffLL;

inline ll id(int x, int y) {
    return x * 1000000000LL + y;
}

void dfs(int x, int y, int gx, int gy, int n, int m) {
    visited[gx][gy] = id(x, y);
    for (int k = 0; k < 4; k++) {
        int nx = x + mx[k];
        int ny = y + my[k];
        int ngx = (nx + 1000000000LL * n) % n;
        int ngy = (ny + 1000000000LL * m) % m;
        if (grid[ngx][ngy] == '#')
            continue;
        if (visited[ngx][ngy] == INF) {
            dfs(nx, ny, ngx, ngy, n, m);
            continue;
        }
        if (visited[ngx][ngy] != id(nx, ny))
            infinite = true;
    }
}

int main() {
    int n, m, sx, sy;

    cin >> n >> m;
    grid.resize(n);
    visited.resize(n, vector<ll>(m, INF));
    for (int i = 0; i < n; i++)
        cin >> grid[i];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 'S') {
                sx = i;
                sy = j;
                break;
            }

    infinite = false;
    dfs(sx, sy, sx, sy, n, m);

    cout << (infinite ? "Yes" : "No") << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pt;

bool infinite;
vector<string> grid;
map<pt, pt> visited;

const int mx[] = { 0, 0, 1, -1 };
const int my[] = { 1, -1, 0, 0 };

void dfs(int x, int y, int gx, int gy, int n, int m) {
    visited[pair(gx, gy)] = pair(x, y);
    for (int k = 0; k < 4; k++) {
        int nx = x + mx[k];
        int ny = y + my[k];
        int ngx = (nx + n) % n;
        int ngy = (ny + m) % m;
        if (grid[ngx][ngy] == '#')
            continue;
        if (!visited.count(pair(ngx, ngy))) {
            dfs(nx, ny, ngx, ngy, n, m);
            continue;
        }
        if (visited[pair(ngx, ngy)] != pair(nx, ny))
            infinite = true;
    }
}

int main() {
    int n, m, sx, sy;

    cin >> n >> m;
    grid.resize(n);
    for (int i = 0; i < n; i++)
        cin >> grid[i];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 'S') {
                sx = i;
                sy = j;
                break;
            }

    infinite = false;
    dfs(sx, sy, sx, sy, n, m);

    cout << (infinite ? "Yes" : "No") << endl;

    return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 2e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

bool dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return 0;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (vis[{x, y}] != make_pair(r, c))
			return 1;
		return 0;
	}
	vis[{x, y}] = { r, c };
	for (int i = 0; i < 4; i++)
	{
		if (dfs(r + dx[i], c + dy[i]))
			return 1;
	}
	return 0;
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
		}
	}
	if (dfs(xx, yy))
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 2e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
pair<int, int>vis[nn][nn];
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

bool dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return 0;
	if (vis[x][y] != make_pair(0, 0))
	{
		if (vis[x][y] != make_pair(r, c))
			return 1;
		return 0;
	}
	vis[x][y] = { r, c };
	for (int i = 0; i < 4; i++)
	{
		if (dfs(r + dx[i], c + dy[i]))
			return 1;
	}
	return 0;
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
		}
	}
	if (dfs(xx, yy))
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define allr(v)		((v).rbegin()), ((v).rend())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)

void File()
{
	freopen("output.txt", "w", stdout);
	freopen("input.txt", "r", stdin);
}
void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
}
const double pi = 2 * acos(0.0);
const int oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 515;
int dx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int dy[8] = { 0, 0, 1, -1, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
int cntDot = 0, cntReach = 0;
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

void dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return;
	if (vis[{x, y}] !=make_pair(0,0))
	{
		if (grid[x][y] != 'S')
			return;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
			found = 1;
		return;
	}
	vis[{x, y}] = { r, c };
	cntReach++;
	for (int i = 0; i < 4; i++)
		dfs(r + dx[i], c + dy[i]);
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
			else if (grid[i][j] == '.')
				cntDot++;
		}
	}
	dfs(xx, yy);
	if (found&&cntReach == cntDot + 1)
		cout << "YES\n";
	else
		cout << "NO\n";
#if _DEBUG
	system("pause");
#endif
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
int cntDot = 0, cntReach = 0;
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

void dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (grid[x][y] != 'S')
			return;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
			found = 1;
		return;
	}
	vis[{x, y}] = { r, c };
	grid[x][y] = '#';
	cntReach++;
	for (int i = 0; i < 4; i++)
		dfs(r + dx[i], c + dy[i]);
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
			else if (grid[i][j] == '.')
				cntDot++;
		}
	}
	dfs(xx, yy);
	int tst = 0;
	for (int i = 0; i < n; i++)
	{
		for (int  j = 0; j < m; j++)
		{
			//cout << grid[i][j];
			if (grid[i][j] == '.')
				tst++;
		}
		//cout << endl;
	}
	if (!tst)
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

bool dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return 0;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (vis[{x, y}] != make_pair(r, c))
			return 1;
		return 0;
	}
	vis[{x, y}] = { r, c };
	for (int i = 0; i < 4; i++)
	{
		if (dfs(r + dx[i], c + dy[i]))
			return 1;
	}
	return 0;
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
		}
	}
	if (dfs(xx, yy))
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
int cntDot = 0, cntReach = 0;
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

void dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (grid[x][y] != 'S')
			return;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
			found = 1;
		return;
	}
	vis[{x, y}] = { r, c };
	grid[x][y] = '#';
	cntReach++;
	for (int i = 0; i < 4; i++)
		dfs(r + dx[i], c + dy[i]);
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
			else if (grid[i][j] == '.')
				cntDot++;
		}
	}
	dfs(xx, yy);
	int tst = 0;
	for (int i = 0; i < n; i++)
	{
		for (int  j = 0; j < m; j++)
		{
			cout << grid[i][j];
			if (grid[i][j] == '.')
				tst++;
		}
		cout << endl;
	}
	if (!tst)
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define allr(v)		((v).rbegin()), ((v).rend())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)

void File()
{
	freopen("output.txt", "w", stdout);
	freopen("input.txt", "r", stdin);
}
void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
}
const double pi = 2 * acos(0.0);
const int oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 515;
int dx[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int dy[8] = { 0, 0, 1, -1, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

bool dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return 0;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (grid[x][y] != 'S')
			return 0;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
			return 1;
		return 0;
	}
	vis[{x, y}] = { r, c };
	for (int i = 0; i < 4; i++)
	{
		if (dfs(r + dx[i], c + dy[i]))
			return 1;
	}
	return 0;
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
		}
	}
	if (dfs(xx, yy))
		cout << "YES\n";
	else
		cout << "NO\n";
#if _DEBUG
	system("pause");
#endif
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
int cntDot = 0, cntReach = 0;
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

void dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (grid[x][y] != 'S')
			return;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
		{
			int tst = 0;
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < m; j++)
				{
					//cout << grid[i][j];
					if (grid[i][j] == '.')
						tst++;
				}
				//cout << endl;
			}
			if (!tst)
				cout << "YES\n";
			else
				cout << "NO\n";
			exit(0);
		}
		return;
	}
	vis[{x, y}] = { r, c };
	if (grid[x][y] == '.')
		grid[x][y] = '#';
	cntReach++;
	for (int i = 0; i < 4; i++)
		dfs(r + dx[i], c + dy[i]);
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
			else if (grid[i][j] == '.')
				cntDot++;
		}
	}
	dfs(xx, yy);
	cout << "NO\n";
	return 0;
}


#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

#define all(v)		((v).begin()), ((v).end())
#define sz(v)		((int)((v).size()))
#define endl		"\n"
#define fx(n)		fixed<<setprecision(n)
#define mk			make_pair

void fast()
{
	ios::sync_with_stdio(NULL);
	cout.tie(NULL);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("output.out", "w", stdout);
	freopen("input.in", "r", stdin);
#endif
	/*#ifdef ONLINE_JUDGE
	//freopen("output.out", "w", stdout);
	freopen("jumping.in", "r", stdin);
	#endif
	*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e3 + 15;
int dx[8] = { 1, 0, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 0, 1, -1, 0, -1, 1, 1, -1 };

int n, m;
char grid[nn][nn];
int cntDot = 0, cntReach = 0;
map<pair<int, int>, pair<int, int>> vis;
bool found = 0;

int rowMod(int r)
{
	return(r%n + n) % n;
}
int colMod(int c)
{
	return(c%m + m) % m;
}

void dfs(int r, int c)
{
	int x = rowMod(r), y = colMod(c);
	if (grid[x][y] == '#')
		return;
	if (vis[{x, y}] != make_pair(0, 0))
	{
		if (grid[x][y] != 'S')
			return;
		if (grid[x][y] == 'S'&&vis[{x, y}] != make_pair(r, c))
		{
			int tst = 0;
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < m; j++)
				{
					//cout << grid[i][j];
					if (grid[i][j] == '.')
						tst++;
				}
				//cout << endl;
			}
			if (!tst)
				cout << "YES\n";
			else
				cout << "NO\n";
			exit(0);
		}
		return;
	}
	vis[{x, y}] = { r, c };
	if (grid[x][y] == '.')
		grid[x][y] = '#';
	cntReach++;
	for (int i = 0; i < 4; i++)
		dfs(r + dx[i], c + dy[i]);
}
int main()
{
	fast();
	cin >> n >> m;
	int xx, yy;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				xx = i, yy = j;
			else if (grid[i][j] == '.')
				cntDot++;
		}
	}
	dfs(xx, yy);
	int tst = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			//cout << grid[i][j];
			if (grid[i][j] == '.')
				tst++;
		}
		//cout << endl;
	}
	if (!tst)
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

const int MX = 1505;
int n, m;
int di[] = {0, 0, 1, -1};
int dj[] = {-1, 1, 0, 0};
char grid[MX][MX];
pii vis[MX][MX];
map<pii, vii> M;
set<pii> done;
set<pii> q;

bool valid(int i, int j) {
	return i >= 0 && i < n && j >= 0 && j < m;
}

void dfs(int i, int j, pii id) {

	db(i, j, id);

	if (vis[i][j] != mp(INT_MAX, INT_MAX) && vis[i][j] != id) {
		cout << "Yes\n";
		exit(0);
	}

	vis[i][j] = id;

	for (int k = 0; k < 4; ++k) {
		int to_i = i + di[k];
		int to_j = j + dj[k];

		if (valid(to_i, to_j) && grid[to_i][to_j] != '#') {
			if (vis[to_i][to_j] != id)
				dfs(to_i, to_j, id);
		} else {
			pii nextId = id;
			if (to_i < 0)
				nextId.fs--;
			if (to_j < 0)
				nextId.sc--;
			if (to_i >= n)
				nextId.fs++;
			if (to_j >= m)
				nextId.sc++;

			if (present(done, nextId))
				continue;

			to_i = (to_i + n) % n;
			to_j = (to_j + m) % m;
			if (grid[to_i][to_j] != '#') {
				q.insert(nextId);
				M[nextId].pb(mp(to_i, to_j));
			}
		}
	}
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> n >> m;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				M[mp(0, 0)].pb(mp(i, j));
			vis[i][j] = mp(INT_MAX, INT_MAX);
		}
	}

	q.insert(mp(0, 0));

	while (!q.empty()) {
		pii id = *q.begin();
		q.erase(q.begin());
		done.insert(id);
		for (auto& pr : M[id]) {
			dfs(pr.fs, pr.sc, id);
		}
	}

	cout << "No\n";

	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

const int MX = 1505;
int n, m;
int di[] = {0, 0, 1, -1};
int dj[] = {-1, 1, 0, 0};
char grid[MX][MX];
pii vis[MX][MX];
map<pii, set<pii>> M;
set<pii> done;
set<pii> q;

bool valid(int i, int j) {
	return i >= 0 && i < n && j >= 0 && j < m;
}

void dfs(int i, int j, pii id) {

	db(i, j, id);

	if (vis[i][j] != mp(INT_MAX, INT_MAX) && vis[i][j] != id) {
		cout << "Yes\n";
		exit(0);
	}

	vis[i][j] = id;

	for (int k = 0; k < 4; ++k) {
		int to_i = i + di[k];
		int to_j = j + dj[k];

		if (valid(to_i, to_j)) {
			if (grid[to_i][to_j] != '#' && vis[to_i][to_j] != id)
				dfs(to_i, to_j, id);
		} else {
			pii nextId = id;
			if (to_i < 0)
				nextId.fs--;
			if (to_j < 0)
				nextId.sc--;
			if (to_i >= n)
				nextId.fs++;
			if (to_j >= m)
				nextId.sc++;

			if (present(done, nextId))
				continue;

			to_i = (to_i + n) % n;
			to_j = (to_j + m) % m;
			if (grid[to_i][to_j] != '#') {
				q.insert(nextId);
				M[nextId].insert(mp(to_i, to_j));
			}
		}
	}
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> n >> m;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				M[mp(0, 0)].insert(mp(i, j));
			vis[i][j] = mp(INT_MAX, INT_MAX);
		}
	}

	q.insert(mp(0, 0));

	while (!q.empty()) {
		pii id = *q.begin();
		q.erase(q.begin());
		done.insert(id);
		for (auto& pr : M[id]) {
			dfs(pr.fs, pr.sc, id);
		}
	}

	cout << "No\n";

	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

const int MX = 1505;
int n, m;
int di[] = {0, 0, 1, -1};
int dj[] = {-1, 1, 0, 0};
char grid[MX][MX];
pii vis[MX][MX];
map<pii, set<pii>> M;
set<pii> done;
set<pii> q;

bool valid(int i, int j) {
	return i >= 0 && i < n && j >= 0 && j < m;
}

void dfs(int i, int j, pii id) {

	db(i, j, id);

	if (vis[i][j] != mp(INT_MAX, INT_MAX) && vis[i][j] != id) {
		cout << "Yes\n";
		exit(0);
	}

	vis[i][j] = id;

	for (int k = 0; k < 4; ++k) {
		int to_i = i + di[k];
		int to_j = j + dj[k];

		if (valid(to_i, to_j)) {
			if (grid[to_i][to_j] != '#' && vis[to_i][to_j] != id)
				dfs(to_i, to_j, id);
		} else {
			pii nextId = id;
			if (to_i < 0)
				nextId.fs--;
			if (to_j < 0)
				nextId.sc--;
			if (to_i >= n)
				nextId.fs++;
			if (to_j >= m)
				nextId.sc++;

			to_i = (to_i + n) % n;
			to_j = (to_j + m) % m;
			if (grid[to_i][to_j] != '#' && vis[to_i][to_j] != nextId) {
				dfs(to_i, to_j, nextId);
			}
		}
	}
}

int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> n >> m;

	int ii, jj;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> grid[i][j];
			if (grid[i][j] == 'S')
				ii = i, jj = j;
			vis[i][j] = mp(INT_MAX, INT_MAX);
		}
	}

	dfs(ii, jj, mp(0, 0));

	cout << "No\n";

	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
	return 0;
}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define endl                         '\n'
#define Ceil(x,y)             ((x+y-1)/y)
#define sz(s)               (int)s.size()
#define RT(x)           return cout<<x,0;
#define mem(x,y)    memset(x,y,sizeof(x))
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

const double pi = acos(-1), eps = 1e-9;
const int OO = 0x3f3f3f3f, N = 1e6 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

char arr[1505][1505];
pair<int,int> vis[1505][1505];
int  n, m;
int gx(int x) {
	return (x%n + n) % n;
}
int gy(int y) {
	return (y%m + m) % m;
}
bool DFS(int x, int y) {

	if (vis[gx(x)][gx(y)] == make_pair(x, y) || arr[gx(x)][gy(y)] == '#')
		return 0;
	if (vis[gx(x)][gy(y)] != make_pair(OO, OO))
	{
		return 1;;
	}
	vis[gx(x)][gy(y)] = { x, y };
	for (int k = 0; k < 4; k++) {
		int ddx = x + dx[k];
		int ddy = y + dy[k];
		if (DFS(ddx, ddy))
			return 1;
	}
	return 0;
}
int main() {
	file();
	cin >> n >> m;
	int cnt = 0;
	int sx, sy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
			if (arr[i][j] == 'S') {
				sx = i; sy = j;
			}
		}
	}
	mem(vis, OO);
	if (DFS(sx, sy))RT("Yes");
	RT("No");

	return 0;
}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define endl                         '\n'
#define Ceil(x,y)             ((x+y-1)/y)
#define sz(s)               (int)s.size()
#define RT(x)           return cout<<x,0;
#define mem(x,y)    memset(x,y,sizeof(x))
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

const double pi = acos(-1), eps = 1e-9;
const int OO = 0x3f3f3f3f, N = 1e6 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

char arr[1505][1505];
bool vis[1505][1505];
int  n, m;
bool v,ok;
void DFS(int x, int y) {
	if(v)vis[x][y] = 1;
	if (v && arr[x][y] == 'S') {
		ok = 1; return;
	}
	for (int i = 0; i < 3; i++) {
		int ddx = dx[i] + x;
		int ddy = dy[i] + y;
		ddx %= n;
		if (ddy >= 0 && ddy < m && !vis[ddx][ddy] && arr[ddx][ddy] != '#') {
			v = 1;
			DFS(ddx, ddy);
		}
	}
}

int main() {
	file();
	cin >> n >> m;
	int cnt = 0;
	int sx, sy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
			if (arr[i][j] == 'S') {
				sx = i; sy = j;
			}
		}
	}
	DFS(sx, sy);
	if (ok)cout << "Yes\n";
	else cout << "No\n";

	return 0;
}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define endl                         '\n'
#define Ceil(x,y)             ((x+y-1)/y)
#define sz(s)               (int)s.size()
#define RT(x)           return cout<<x,0;
#define mem(x,y)    memset(x,y,sizeof(x))
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

const double pi = acos(-1), eps = 1e-9;
const int OO = 0x3f3f3f3f, N = 1e6 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

char arr[1505][1505];
int vis[1505][1505];
int  n, m;
bool v,ok;
void DFS(int x, int y) {
	for (int i = 0; i < 4; i++) {
		int ddx = dx[i] + x;
		int ddy = dy[i] + y;
		if (ddx < 0)ddx += n;
		if (ddy < 0)ddy += m;
		ddx %= n; ddy %= m;
		if (!vis[ddx][ddy] && arr[ddx][ddy] == '.') {
			vis[ddx][ddy] = vis[x][y] + 1;
			DFS(ddx, ddy);
		}
	}
}
int main() {
	file();
	cin >> n >> m;
	int cnt = 0;
	int sx, sy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
			if (arr[i][j] == 'S') {
				sx = i; sy = j;
			}
		}
	}
	DFS(sx, sy);
	for (int i = 0; i < 4; i++) {
		int ddx = dx[i] + sx;
		int ddy = dy[i] + sy;
		if (ddx < 0)ddx += n;
		if (ddy < 0)ddy += m;
		ddx %= n; ddy %= m;
		if (vis[ddx][ddy] > 1)RT("Yes");
	}
	RT("No");

	return 0;
}


//============================================================================
// Name        : ICPC.cpp
// Author      : MaTb3aa
// Quotes      : Nothing is impossible, the word itself says 'I'm possible'!.
// Copyright   : Audrey Hepburn
// Description : You Can Do It
//============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define endl                         '\n'
#define Ceil(x,y)             ((x+y-1)/y)
#define sz(s)               (int)s.size()
#define RT(x)           return cout<<x,0;
#define mem(x,y)    memset(x,y,sizeof(x))
#define all(v) ((v).begin()), ((v).end())
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

const double pi = acos(-1), eps = 1e-9;
const int OO = 0x3f3f3f3f, N = 1e6 + 5, mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll x, ll y) { return (!y) ? x : gcd(y, x%y); }
ll lcm(ll x, ll y) { return ((x / gcd(x, y))*y); }

void file() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif  !ONLINEJUDGE
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

char arr[1505][1505];
int vis[1505][1505];
pair<int, int>mp[1500][1500];
int  n, m;
int gx(int x) {
	return (x%n + n) % n;
}
int gy(int y) {
	return (y%m + m) % m;
}
bool DFS(int i, int j) {
	int x = gx(i);
	int y = gy(j);
	if (arr[x][y] == '#')
		return 0;
	if (vis[x][y]) {
		return mp[x][y] != make_pair(i, j);
	}
	vis[x][y] = 1;
	mp[x][y] = make_pair(i, j);
	for (int k = 0; k < 4; k++) {
		int r = i + dx[k];
		int c = j + dy[k];
		if (DFS(r, c))
			return 1;
	}
	return 0;
}
int main() {
	file();
	cin >> n >> m;
	int cnt = 0;
	int sx, sy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
			if (arr[i][j] == 'S') {
				sx = i; sy = j;
			}
		}
	}
	if (DFS(sx, sy))RT("Yes");
	RT("No");

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <string>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout<<(#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 1509;
char grid[N][N];
int n, m;
bool valid(int i, int j) {
	if (grid[abs(i) % n][abs(j) % m] != '#')
		return 1;
	return 0;
}
bool ok = 0;
map<pair<int, int>, bool>vis;
void fill(int i, int j) {
	if (i == n * 2 || i == -n) {
		ok = 1;
		return;
	}
	if (j == m * 2 || j == -m) {
		ok = 1;
		return;
	}
	if (vis[{i, j}])
		return;
	vis[{i, j}] = 1;
	int x, y;
	for (int r = 0; r < 4; r++) {
		x = dx[r] + i;
		y = dy[r] + j;
		if (valid(x, y))
			fill(x, y);
	}
}
int main() {
	file();
	fast();
	cin >> n >> m;
	int s1, s2;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
			if (grid[i][j] == 'S') {
				s1 = i, s2 = j;
				grid[i][j] = '.';
			}
		}
	}
	fill(s1, s2);
	if (ok)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}