#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  char s[10005];
  set<int> S;
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    gets(s);
    int a, b;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'G') a = j;
      if (s[j] == 'S') b = j;
    }
    S.insert(b - a);
    if (b < a) {
      cout << -1;
      return 0;
    }
  }
  printf("%d", S.size());
}
