#include <bits/stdc++.h>
using namespace std;
bool mark[2000];
char s[2000];
int main() {
  int n, m, flag, dwarf, candy, i, j;
  cin >> n >> m;
  memset(mark, false, sizeof(mark));
  flag = 1;
  int dist, ans;
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    for (j = 0; j < m; j++) {
      if (s[j] == 'S') candy = j;
      if (s[j] == 'G') dwarf = j;
    }
    dist = candy - dwarf;
    if (dist < 0)
      flag = 0;
    else
      mark[dist] = true;
  }
  ans = 0;
  for (i = 0; i < m + 3; i++) {
    if (mark[i]) ans++;
  }
  if (flag == 0) ans = -1;
  printf("%d\n", ans);
  return 0;
}
