#include <bits/stdc++.h>
using namespace std;
bool f[1002];
char s[1001][1001];
int main() {
  int n, m, ca, i, j, cnt, dw;
  scanf("%d %d\n", &n, &m);
  cnt = 0;
  memset(f, 0, sizeof(f));
  for (i = 0; i < n; i++) gets(s[i]);
  for (i = 0; i < n; i++) {
    dw = 0;
    ca = 0;
    for (j = 0; j < m; j++) {
      if (s[i][j] == 'S') ca = j;
      if (s[i][j] == 'G') dw = j;
    }
    if (ca < dw) {
      puts("-1");
      return 0;
    }
    f[ca - dw] = 1;
  }
  for (i = 1; i < 1002; i++)
    if (f[i]) cnt++;
  printf("%d\n", cnt);
  return 0;
}
