#include <bits/stdc++.h>
using namespace std;
int n, m;
char arr[1001][1001];
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  srand(time(NULL));
  cin >> n >> m;
  vector<pair<int, int> > v(n);
  bool fk = 0;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    bool f = 0;
    int s = 0;
    int g = 0;
    bool g1 = 0;
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        f = 1;
        s = j;
        if (g1) {
          mp[s - g]++;
        }
      }
      if (arr[i][j] == 'G' && !f) {
        g = j;
        g1 = 1;
      }
      if (arr[i][j] == 'G' && f) fk = 1;
    }
  }
  if (fk)
    cout << -1;
  else
    cout << mp.size();
}
