#include <bits/stdc++.h>
char map[1005][1005];
int hash[1005];
int main(void) {
  int n, m, x, ok, cnt, i, j;
  while (scanf("%d%d", &n, &m) != EOF) {
    getchar();
    memset(hash, 0, sizeof(hash));
    ok = 0;
    for (i = 0; i < n; i++) {
      x = 10000;
      for (j = 0; j < m; j++) {
        scanf("%c", &map[i][j]);
        if (map[i][j] == 'G') {
          x = j;
        }
        if (map[i][j] == 'S') {
          if (j < x) {
            ok = 1;
          } else {
            hash[j - x] = 1;
          }
        }
      }
      getchar();
    }
    if (ok) {
      printf("-1\n");
    } else {
      cnt = 0;
      for (i = 0; i < 1005; i++) {
        if (hash[i] == 1) {
          cnt++;
        }
      }
      printf("%d\n", cnt);
    }
  }
  return 0;
}
