#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, y;
  cin >> x >> y;
  char tab[x][y];
  for (int i = 0; i < x; ++i)
    for (int j = 0; j < y; ++j) cin >> tab[i][j];
  set<int> s;
  int ans = 0;
  for (int i = 0; i < x; ++i) {
    int G, S;
    for (int j = 0; j < y; ++j)
      if (tab[i][j] == 'G')
        G = j;
      else if (tab[i][j] == 'S')
        S = j;
    if (S < G) {
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans, S - G);
    s.insert(S - G);
  }
  cout << s.size() << endl;
  return 0;
}
