#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int MAX = 1e5;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  char s[n][m];
  for (long long int i = 0; i < (n); i++) {
    for (long long int j = 0; j < (m); j++) {
      cin >> s[i][j];
    }
  }
  long long int vis = 0;
  set<long long int> se;
  for (long long int i = 0; i < (n); i++) {
    vis = 0;
    for (long long int j = 0; j < (m); j++) {
      if (s[i][j] == 'S' && vis == 0) {
        return cout << "-1", 0;
      } else if (s[i][j] == 'G') {
        long long int k = j;
        while (s[i][k] != 'S') {
          k++;
        }
        se.insert(k - j);
        vis = 1;
      }
    }
  }
  cout << se.size();
  return 0;
}
