#include <bits/stdc++.h>
using namespace std;
int n, m;
string x;
set<int> s;
int lmax = 0;
int rmin = 10000;
int main() {
  cin >> n >> m;
  while (n--) {
    cin >> x;
    int d = 0;
    int c = 0;
    for (int i = 0; i < m; i++) {
      if (x[i] == 'G')
        d = i;
      else if (x[i] == 'S')
        c = i;
    }
    if (c - d > 0) {
      s.insert(c - d);
      lmax = max(lmax, c - d);
    } else {
      rmin = min(rmin, m - d);
    }
  }
  if (s.size() == 0 || rmin - 1 < lmax) {
    cout << -1;
    return 0;
  } else
    cout << s.size();
  return 0;
}
