#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char x[1010][1010];
  set<int> s;
  int flag = 1, ans = 0;
  for (int i = 0; i < n; i++) {
    int g = -1, ss = -1;
    for (int j = 0; j < m; j++) {
      cin >> x[i][j];
      if (x[i][j] == 'G') {
        g = j;
      }
      if (x[i][j] == 'S') {
        ss = j;
      }
    }
    if (g > ss)
      flag = 0;
    else
      s.insert(ss - g);
  }
  if (!flag)
    cout << -1 << endl;
  else
    cout << s.size() << endl;
  return 0;
}
