#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, f = 0, j, maxi, i, s, d, c;
  char x;
  set<int> s1;
  cin >> n >> m;
  maxi = INT_MIN;
  for (i = 0; i < n; i++) {
    c = 0;
    s = -1;
    d = -1;
    for (j = 0; j < m; j++) {
      cin >> x;
      if (x == 'G')
        s = j;
      else if (x == 'S')
        d = j;
      else if (s != -1 && d == -1)
        c++;
    }
    if (d < s)
      f = 1;
    else
      s1.insert(d - s);
  }
  if (f == 1)
    cout << -1;
  else
    cout << s1.size();
}
