#include <bits/stdc++.h>
using namespace std;
char c[2222][2222];
int main() {
  int nn, mm, n, m;
  cin >> nn >> mm;
  n = nn, m = mm;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  bool used = false;
  set<int> se;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 'G') {
        for (int h = j; h <= m; h++) {
          if (c[i][h] == 'S') {
            se.insert(h - j);
            break;
          }
          if (h == m) used = true;
        }
      }
    }
    if (used) {
      cout << -1;
      return 0;
    }
  }
  cout << se.size();
  return 0;
}
