#include <bits/stdc++.h>
using namespace std;
int n, m, b, e, Min;
char s[9000];
int a[2000];
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Min = 99999;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      scanf("%s", &s);
      for (int j = 0; s[j]; j++) {
        if (s[j] == 'G') b = j;
        if (s[j] == 'S') e = j;
      }
      if (e < b) flag = 1;
      a[i] = e - b;
    }
    if (flag)
      printf("-1\n");
    else {
      sort(a, a + n);
      Min = 0;
      int jian = 0;
      for (int i = 0; i < n; ++i) {
        if (a[i] - jian > 0) {
          jian += a[i] - jian;
          Min++;
        }
      }
      printf("%d\n", Min);
    }
  }
  return 0;
}
