#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
const int INF = 1000 * 1000 * 1000;
int n, m;
vector<int> ans;
int used[MAXN], cnt;
set<int> st;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    int cur1 = -1, cur2 = -1;
    for (int j = 0; j < s.length(); ++j) {
      if (s[j] == 'G') cur1 = j;
      if (s[j] == 'S') cur2 = j;
    }
    if (cur2 < cur1) {
      cout << "-1";
      exit(0);
    } else {
      st.insert(cur2 - cur1);
    }
  }
  cout << st.size();
  return 0;
}
