#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
int main() {
  fast();
  int n, m, c = 1;
  cin >> n >> m;
  string s[n];
  for (int i = 0; i < (n); i++) cin >> s[i];
  string::iterator it, it1;
  map<int, int> dist;
  for (int i = 0; i < (n); i++) {
    it = (find(s[i].begin(), s[i].end(), 'G'));
    it1 = (find(s[i].begin(), s[i].end(), 'S'));
    int pos = it - s[i].begin();
    int pos1 = it1 - s[i].begin();
    if (pos > pos1) {
      cout << "-1";
      return 0;
    }
    dist[pos1 - pos] = 1;
  }
z:;
  cout << dist.size();
  return 0;
}
