#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  vector<int> v1, v2, v3;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (v[i][j] == 'G')
        v1.push_back(j);
      else if (v[i][j] == 'S')
        v2.push_back(j);
    }
  }
  int flag = 0;
  for (int i = 0; i < n; i++) {
    if (v1[i] > v2[i]) {
      flag = 1;
      break;
    } else {
      v3.push_back(v2[i] - v1[i]);
    }
  }
  if (flag == 1) {
    cout << "-1";
  } else {
    int count = 0, count1 = 0;
    sort(v3.begin(), v3.end());
    for (int i = 0; i < n; i++) {
      if (count != v3[i]) {
        count = v3[i];
        count1++;
      }
    }
    cout << count1;
  }
  return 0;
}
