#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  char array[n][m + 3];
  int S, G;
  int min = 200000;
  int curMoves;
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    scanf("%s", array[i]);
    for (int j = 0; j < m; j++) {
      if (array[i][j] == 'S')
        S = j;
      else if (array[i][j] == 'G')
        G = j;
    }
    if (S < G) {
      printf("-1\n");
      return 0;
    }
    M[S - G] = 1;
  }
  int count = 0;
  vector<int> v;
  for (std::map<int, int>::iterator it = M.begin(); it != M.end(); it++)
    v.push_back(it->first);
  bool breakFlag = true;
  printf("%d\n", v.size());
  return 0;
}
