#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  set<int> s;
  bool lost = false;
  while (n--) {
    string str;
    cin >> str;
    int pos = -1;
    for (int i = 0; i < m; i++) {
      if (str[i] == 'S') {
        lost = true;
        break;
      } else if (str[i] == 'G') {
        pos = i;
        break;
      }
    }
    if (!lost) {
      int cnt = 0;
      for (int i = pos; i < m; i++)
        if (str[i] == 'S')
          break;
        else if (str[i] == '*')
          cnt++;
      s.insert(cnt + 1);
    }
  }
  if (lost)
    cout << -1 << endl;
  else
    cout << s.size() << endl;
  return 0;
}
