#include <bits/stdc++.h>
using namespace std;
unsigned long long mod = 1000000007;
int main() {
  int rows, cols;
  cin >> rows >> cols;
  bool crap = false;
  string line;
  std::vector<int> dists;
  while (rows--) {
    cin >> line;
    int G_pos = line.find('G');
    int S_pos = line.find('S');
    if (G_pos > S_pos)
      crap = true;
    else
      dists.push_back(S_pos - G_pos);
  }
  if (crap)
    cout << "-1\n";
  else {
    sort(dists.rbegin(), dists.rend());
    int moves = 0;
    while (!dists.empty()) {
      int now = dists[int((dists).size()) - 1];
      if (now != 0) {
        for (int pos = int((dists).size()) - 2; pos >= 0; pos--)
          dists[pos] -= now;
        moves++;
      }
      dists.pop_back();
    }
    cout << moves << "\n";
  }
  return 0;
}
