#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  int dis[n];
  for (int i = 0; i < n; i++) {
    cin >> s;
    int a = -1, b = -1;
    for (int i = 0; i < m; i++) {
      if (s[i] == 'S')
        b = i;
      else if (s[i] == 'G')
        a = i;
    }
    if (a > b) {
      cout << "-1" << endl;
      return (0);
    }
    dis[i] = b - a;
  }
  int f[m];
  for (int i = 0; i < m; i++) f[i] = 0;
  for (int i = 0; i < n; i++) f[dis[i]]++;
  long int ans = 0;
  for (int i = 1; i < m; i++)
    if (f[i] >= 1) ans++;
  cout << ans << endl;
  return (0);
}
