#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int dwarf[n];
  int candy[n];
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == 'G')
        dwarf[i] = j;
      else if (c == 'S')
        candy[i] = j;
    }
  }
  vector<int> distance(n);
  for (int i = 0; i < n; i++) {
    distance[i] = candy[i] - dwarf[i];
    if (distance[i] < 0) {
      cout << -1;
      return 0;
    }
  }
  sort(distance.begin(), distance.end());
  distance.erase(unique(distance.begin(), distance.end()), distance.end());
  cout << distance.size();
}
