<pre class="html" name="code">#include<stdio.h> char map[8][8]; int visited,flag; int n,i,j,m; void dfs(int x,int y) { if( x<0 || y<0 || x>=n || y>=m) return; if(map[x][y]!='.') return; if(flag) return; map[x][y] = 's'; visited++; if(visited == n*m) { flag=1; return; } dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1); visited--; map[x][y]='.'; } int main(void) { while(scanf("%d%d",&n,&m) && n && m) { for(i=0;i<n;i++) scanf("%s",map[i]); for(i = 0 , visited = 0 ; i < n ; i++) for(j = 0;j<m;j++) if(map[i][j]=='S') visited++; flag = 0; dfs(0,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }