#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, flag = 0, dist;
  ;
  char tmp;
  cin >> n >> m;
  set<int> s;
  for (int i = 0; i < n; i++) {
    dist = 0;
    for (int j = 0; j < m; j++) {
      cin >> tmp;
      if (dist == 0 && tmp == 'S') {
        cout << -1;
        exit(0);
      }
      if (tmp == 'G') {
        dist++;
        continue;
      }
      if (dist && tmp == '*') dist++;
      if (tmp == 'S') {
        s.insert(dist);
        int k = m - j - 1;
        while (k--) cin >> tmp;
        break;
      }
    }
  }
  int moves = 0;
  cout << s.size();
}
