#include <bits/stdc++.h>
using namespace std;
long long limit = 4294967296;
int MOD = 1000000007;
template <typename T>
void print(const T& x) {
  cerr << x << ' ';
}
template <typename T1, typename... T2>
void print(const T1& first, const T2&... rest) {
  print(first);
  print(rest...);
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m;
  cin >> n >> m;
  set<int> s;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    if (!flag) continue;
    int g = -1, c = -1;
    for (int i = 0; i < m; i++) {
      if (g == -1 && str[i] == 'S') {
        flag = false;
        break;
      }
      if (c == -1 && g == -1 && str[i] == 'G') {
        g = i;
      } else if (c == -1 && g != -1 && str[i] == 'S') {
        c = i;
      }
    }
    s.insert(c - g);
  }
  if (!flag)
    cout << -1 << endl;
  else
    cout << s.size() << endl;
  return 0;
}
