#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e6 + 50);
const double EPS = 1e-8;
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  set<int> ans;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    int a = 0, b = 0;
    for (int j = 0; j < m; ++j) {
      if (s[j] == 'G') a = j;
      if (s[j] == 'S') b = j;
    }
    if (b < a) {
      cout << -1;
      return 0;
    }
    ans.insert(b - a);
  }
  cout << ans.size();
  return 0;
}
