#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long llu;
const unsigned int LIM = 1005;
int main() {
  int n, m;
  bool can = true;
  cin >> n >> m;
  char cmatrix[LIM][LIM];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> cmatrix[i][j];
  int dist[LIM], Gpos[LIM], Spos[LIM], count[LIM] = {0}, ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (cmatrix[i][j] == 'G') Gpos[i] = j;
      if (cmatrix[i][j] == 'S') Spos[i] = j;
    }
    dist[i] = Spos[i] - Gpos[i];
    if (dist[i] < 0) can = false;
  }
  if (can) {
    for (int i = 0; i < n; i++) count[dist[i]]++;
    for (int i = 1; i < LIM; i++)
      if (count[i]) ans++;
    cout << ans << "\n";
  } else {
    cout << "-1\n";
  }
  return 0;
}
