#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll mxr = 5e5 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  unordered_set<int> mov;
  int n, m;
  cin >> n >> m;
  char ar[m];
  for (int i = 0; i < n; i++) {
    int dw, cn;
    for (int j = 0; j < m; j++) {
      cin >> ar[j];
      if (ar[j] == 'G')
        dw = j;
      else if (ar[j] == 'S')
        cn = j;
    }
    if (cn < dw) {
      cout << -1 << "\n";
      return 0;
    } else {
      mov.insert(cn - dw);
    }
  }
  cout << mov.size();
  return 0;
}
