#include <bits/stdc++.h>
using namespace std;
int n, m, G, S, d, ans;
char a[1005][1005];
bool v[1005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'G') G = j;
      if (a[i][j] == 'S') S = j;
    }
    if (S < G) {
      printf("-1\n");
      return 0;
    }
    d = S - G;
    v[d] = 1;
  }
  for (int i = 0; i <= 1000; i++)
    if (v[i]) ans++;
  printf("%d\n", ans);
  return 0;
}
