#include <bits/stdc++.h>
using namespace std;
bool fl[1001];
int main() {
  int n, m;
  cin >> n >> m;
  char A[n][m];
  set<int> s;
  for (int i = 0; i < n; i++) {
    int posG, posS;
    for (int j = 0; j < m; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'G') posG = j;
      if (A[i][j] == 'S') posS = j;
    }
    if (posS - posG < 0) {
      cout << "-1" << endl;
      return 0;
    }
    if (!fl[posS - posG]) {
      fl[posS - posG] = true;
      s.insert(posS - posG);
    }
  }
  cout << s.size() << endl;
  return 0;
}
