#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
const int INF = (int)1e9;
int s[N];
int g[N];
int d[N];
char a[N][N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  bool ok = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  for (int i = 0; i < n; i++) g[i] = s[i] = -1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'G') g[i] = j;
      if (a[i][j] == 'S' && s[i] == -1 && g[i] != -1) s[i] = j;
    }
  for (int i = 0; i < n; i++) {
    if (s[i] == -1) {
      puts("-1");
      return 0;
    }
  }
  while (true) {
    int mn = INF;
    for (int i = 0; i < n; i++)
      if (!d[i]) mn = min(mn, s[i] - g[i]);
    if (mn == INF) break;
    for (int i = 0; i < n; i++) {
      if (!d[i]) {
        g[i] += mn;
        if (s[i] == g[i]) d[i] = 1;
      }
    }
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
