#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int INF = 9999999;
int main() {
  int n, m;
  cin >> n >> m;
  string all;
  int ans = 0;
  int k = 0;
  set<int> ok;
  for (int i = 0; i < n; i++) {
    cin >> all;
    int cur = 0;
    int x = 0;
    for (int j = 0; j < m; j++) {
      if (all[j] == 'S') cur = j;
      if (all[j] == 'G') x = j;
    }
    if (cur > x) {
      ok.insert(cur - x);
    } else
      k++;
  }
  if (k != 0)
    cout << -1 << endl;
  else
    cout << ok.size() << endl;
  return 0;
}
