#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
long long po(long long x, long long p) {
  if (p == 0) return 1;
  if (p % 2 == 0)
    return po((x * x), p / 2);
  else
    return (po(x, p - 1) * x);
}
int main() {
  int n, m;
  string s;
  cin >> n >> m;
  set<int> ans;
  for (int i = 0; i < n; i++) {
    cin >> s;
    int len = 0;
    bool isda = 0, iss = 0;
    for (auto x : s) {
      if (x == 'G')
        isda = 1;
      else if (isda && x == '*') {
        len++;
      } else if (x == 'S' && isda) {
        iss = 1;
        break;
      }
    }
    if (!iss) exit(puts("-1"));
    ans.insert(len);
  }
  cout << ans.size() << endl;
}
