#include <bits/stdc++.h>
int main() {
  char a[1002];
  int t[1002], k = 0;
  int n, m, i, j, g = 0, s = 0, c = 0, q, f = 1;
  scanf("%d%d", &n, &m);
  getchar();
  for (i = 0; i < n; i++) {
    gets(a);
    for (j = 0; j < m; j++) {
      if (a[j] == 'G') {
        g = j;
      }
      if (a[j] == 'S') {
        s = j;
      }
    }
    if (s < g) {
      f = 0;
    } else if (f) {
      t[k] = s - g;
      for (q = 0; q < k; q++) {
        if (t[q] == t[k]) {
          c--;
          break;
        }
      }
      c++;
      k++;
    }
  }
  if (f) {
    printf("%d", c);
  } else {
    printf("-1");
  }
  return 0;
}
