#include <bits/stdc++.h>
int main() {
  int n, m, i, j, ok = 1, ans = 0;
  int S, G;
  char s[1001];
  int a[1001] = {0};
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    for (j = 0; j < m; j++)
      if (s[j] == 'G')
        G = j;
      else if (s[j] == 'S')
        S = j;
    if (S < G) {
      ok = 0;
      break;
    }
    a[S - G] = 1;
  }
  for (i = 0; i <= 1000; i++)
    if (a[i]) ans++;
  printf("%d\n", ok ? ans : -1);
  return 0;
}
