#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  char a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  unordered_set<int> set;
  for (int i = 0; i < n; i++) {
    int d = -1, c = -1;
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'G') {
        d = j;
      } else if (a[i][j] == 'S') {
        c = j;
      }
    }
    if (c < d) {
      cout << -1 << "\n";
      return;
    }
    set.insert(c - d);
  }
  cout << set.size() << "\n";
}
int main() { solve(); }
