func_code_string: #include <bits/stdc++.h>   using namespace std;   int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}, n, m;   const int N = 1505;   char c[N][N];   pair<int, int> seen[N][N];   int mod(int a, int m) {     while (a < 0) a += m;     return a % m;   }   bool dfs2(int x, int y) {     if (c[mod(x, n)][mod(y, m)] ==  # ) return false;     if (seen[mod(x, n)][mod(y, m)] != make_pair(1 << 27, 1 << 27)) {       if (seen[mod(x, n)][mod(y, m)] != make_pair(x, y))         return true;       else         return false;     }     seen[mod(x, n)][mod(y, m)] = make_pair(x, y);     for (int k = 0; k < 4; k++)       if (dfs2(x + dx[k], y + dy[k])) return true;     return false;   }   int k, comp[N][N], start_comp, start_x, start_y;   bool seen_edge;   vector<pair<int, int> > edges;   vector<int> edge_style;   void dfs(int x, int y) {     if (x < 0 || x >= n || y < 0 || y >= m) {       seen_edge = true;       return;     }     if (comp[x][y] || c[x][y] ==  # ) return;     if (c[x][y] ==  S ) {       start_x = x;       start_y = y;       start_comp = k;     }     comp[x][y] = k;     for (int k = 0; k < 4; k++) dfs(x + dx[k], y + dy[k]);   }   int dist[10000];   const int inf = 1 << 27;   bool relax(int a, int b, int w) {     if (dist[b] < inf && dist[a] > dist[b] + w) {       dist[a] = dist[b] + w;       return true;     }     return false;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> c[i];     k = 1;     start_comp = -1;     memset(comp, 0, sizeof(comp));     start_x = -1;     start_y = -1;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!comp[i][j] && c[i][j] !=  # ) {           seen_edge = false;           dfs(i, j);           if (seen_edge)             k++;           else if (start_x >= 0 && start_y >= 0 && comp[start_x][start_y] == k) {             cout <<  No  << endl;             return 0;           }         }     edges.clear();     edge_style.clear();     for (int i = 0; i < n; i++) {       if (c[i][0] !=  #  && c[i][m - 1] !=  # ) {         edge_style.push_back(1);         edges.push_back(make_pair(comp[i][m - 1], comp[i][0]));       }     }     for (int j = 0; j < m; j++) {       if (c[0][j] !=  #  && c[n - 1][j] !=  # ) {         edge_style.push_back(2);         edges.push_back(make_pair(comp[n - 1][j], comp[0][j]));       }     }     bool flag = false;     for (int z = 0; z < 4; z++) {       for (int i = 1; i < k; i++) dist[i] = inf;       dist[start_comp] = 0;       for (int i = 0; i < k; i++)         for (int j = 0; j < edges.size(); j++) {           int a = edges[j].first, b = edges[j].second;           int dw = (edge_style[j] == 1) ? (z % 2 ? 1 : -1) : (z / 2 ? 1 : -1);           bool u = relax(a, b, dw), v = relax(b, a, -dw);           if (i == k - 1 && (u || v)) flag = true;         }     }     cout << (flag ?  Yes  :  No ) << endl;   }   