# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; int DX [] = {,-}; int dy [] = {-,}; int vis [44] [44]; int dis [44] [44]; int hash [1111]; int map [44] [44]; char STR [44] [44]; int Len; int N, m; int begin; int State [2, 1000]; c Onst int INF = 0 xfffffff; int DP [1 <17] [17]; int fin; int Ma (int A, int B) {return A> B? A: B;} void BFS (INT X, int y) {queue <int> q; q. push (x * m + Y); memset (DIS, 0, sizeof (DIS); memset (VIS, 0, sizeof (VIS )); vis [x] [Y] = 1; DIS [x] [Y] = 0; while (! Q. Empty () {int cur = Q. Front (); q. Pop (); int xx = cur/m; int YY = cur % m; If (~ Hash [cur]) {map [hash [x * m + Y] [hash [cur] = map [hash [cur] [hash [x * m + Y] = dis [xx] [YY];} for (INT I = 0; I <4; I ++) {int X1 = xx + dx [I]; int Y1 = YY + dy [I]; if (x1> = 0 & X1 <n & Y1> = 0 & Y1 <M &&! Vis [X1] [Y1] & STR [X1] [Y1]! = 'D') {vis [X1] [Y1] = 1; DIS [X1] [Y1] = dis [XX] [YY] + 1; q. push (x1 * m + Y1) ;}}// graph int Gao (INT mid) {memset (DP,-1, sizeof (DP )); DP [1 <begin] [begin] = mid; int Gg = (1 <Len); For (INT I = 0; I <GG; I ++) {for (Int J = 0; j <Len; j ++) {If (DP [I] [J] =-1) continue; if (I & (1 <j) = 0) continue; If (I & Fin) = fin) {return 1 ;} for (int K = 0; k <Len; k ++) {If (I & (1 <k) continue; if (Map [J] [k] =-1 | DP [I] [J] <map [J] [k]) continue; DP [I | (1 <K)] [k] = MA (DP [I | (1 <k)] [K], DP [I] [J]-map [J] [k]); If (State [k]) DP [I | (1 <k)] [k] = mid ;}}return 0;} int main () {While (CIN >>n> M, N | M) {memset (State, 0, sizeof (State); memset (hash,-1, sizeof (hash); memset (MAP,-1, sizeof (MAP); fin = 0; Len = 0; for (INT I = 0; I <n; I ++) {scanf ("% s", STR [I]); For (Int J = 0; j <m; j ++) {If (STR [I] [J] = 's' | STR [I] [J] = 'D') continue; hash [I * m + J] = Len; If (STR [I] [J] = 'F') Begin = Len, FIN | = (1 <Len); If (STR [I] [J] = 'G') State [Len] = 1; if (STR [I] [J] = 'y') FIN | = (1 <Len); Len ++ ;}} for (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) if (~ Hash [I * m + J]) {BFS (I, j) ;}} int L = 0; int r = N * m + 10; int Gg =-1; while (L <= r) {int mid = (L + r)> 1; if (Gao (MID) {Gg = mid; r = mid-1 ;} else l = Mid + 1;} printf ("% d \ n", GG);} return 0 ;}