#include <bits/stdc++.h>
using namespace std;
int g[1000];
int s[1000];
int chk[1001];
int main() {
  int ans = 0;
  char tmp;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> tmp;
      if (tmp == 'G') g[i] = j;
      if (tmp == 'S') s[i] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] < g[i]) {
      ans = -1;
      break;
    } else {
      if (!chk[s[i] - g[i]]) {
        chk[s[i] - g[i]] = true;
        ans++;
      }
    }
  }
  cout << ans;
  return 0;
}
