#include <bits/stdc++.h>
using namespace std;
vector<int> a, b;
int main() {
  int n, m;
  cin >> n >> m;
  getchar();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char ch = getchar();
      if (ch == 'G') a.push_back(j);
      if (ch == 'S') b.push_back(j);
    }
    getchar();
  }
  for (int i = 0; i < n; i++) {
    if (b[i] <= a[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  int ans = 0, cp = 0;
  while (true) {
    int mp = 1 << 30;
    for (int i = 0; i < n; i++) {
      int x = b[i] - a[i] - cp;
      if (x > 0) mp = min(mp, x);
    }
    if (mp == 1 << 30) break;
    ans++;
    cp += mp;
  }
  cout << ans << endl;
  return 0;
}
