#include <bits/stdc++.h>
using namespace std;
char mz[1000][1000];
int d[1000];
int num[1000], k;
int vis[1001];
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m) {
    bool ok = true;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; i++) {
      cin >> mz[i];
      int x, y;
      for (int j = 0; j < m; j++)
        if (mz[i][j] == 'G')
          x = j;
        else if (mz[i][j] == 'S')
          y = j;
      if (x > y)
        ok = false;
      else
        vis[y - x] = 1;
      d[i] = y - x;
    }
    if (!ok) {
      cout << -1 << endl;
      continue;
    }
    sort(d, d + n);
    num[0] = d[0];
    k = 1;
    for (int i = 1; i < n; i++) {
      if (d[i] != d[i - 1]) num[k++] = d[i];
    }
    int ans = k;
    cout << ans << endl;
  }
  return 0;
}
