#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, cnt = 0;
  set<int> v;
  string A[1001];
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> A[i];
  for (int i = 0; i < n; ++i) {
    int x = A[i].find('G');
    int y = A[i].find('S');
    if (x > y) {
      cout << "-1" << endl;
      return 0;
    }
    v.insert(y - x);
  }
  cout << v.size() << endl;
  return 0;
}
