#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) { return (a) ? GCD(b % a, a) : b; }
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
double power(double a, long long b) {
  double x = 1;
  for (int i = 0; i < b; i++) {
    x = x * a;
  }
  return x;
}
int main() {
  int n, m;
  cin >> n >> m;
  int index_g, index_s;
  char a;
  set<int> s;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a;
      if (a == 'G') {
        index_g = j;
      } else if (a == 'S') {
        index_s = j;
      }
    }
    if (index_s > index_g) {
      s.insert(index_s - index_g);
    } else if (index_s < index_g) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (s.size() == 0) {
    cout << -1 << endl;
  } else {
    cout << s.size() << endl;
  }
  return 0;
}
