#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, g, s, k = 0, ans[1001], num = 0;
  char Map[1001];
  scanf("%d%d", &n, &m);
  getchar();
  memset(ans, 0, sizeof(ans));
  while (n--) {
    scanf("%s", Map);
    for (i = 0; i < m; i++) {
      if (Map[i] == 'G') g = i;
      if (Map[i] == 'S') s = i;
    }
    if (s > g) {
      j = s - g;
      if (ans[j] == 0) {
        ans[j] = 1;
        num++;
      }
    } else
      k = 1;
  }
  if (!k && num)
    printf("%d\n", num);
  else
    printf("-1\n");
  return 0;
}
