#include <bits/stdc++.h>
using namespace std;
int n, m;
char c;
int vis[1005];
int main() {
  cin >> n >> m;
  int pre = 0, aft = 0, tot = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c;
      if (c == 'G') pre = j;
      if (c == 'S') aft = j;
    }
    if (aft - pre < 0) {
      cout << -1 << endl;
      return 0;
    }
    if (!vis[aft - pre]) {
      tot++;
      vis[aft - pre] = 1;
    }
  }
  cout << tot << endl;
  return 0;
}
