#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char D[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> D[i][j];
    }
  }
  vector<int> F;
  for (int i = 0; i < n; i++) {
    int a = -1, b = -1;
    for (int j = 0; j < m; j++) {
      if (D[i][j] == 'S') {
        if (a == -1) {
          cout << -1;
          return 0;
        } else {
          b = j;
        }
      } else if (D[i][j] == 'G') {
        a = j;
      }
    }
    F.push_back(b - a);
  }
  sort(F.begin(), F.end());
  int ans = 1;
  for (int i = 1; i < F.size(); i++) {
    if (F[i] != F[i - 1]) {
      ans++;
    }
  }
  cout << ans;
  return 0;
}
