#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  set<int> distances;
  for (int i = 0; i < n; ++i) {
    string line;
    cin >> line;
    int dwarf = 0, candy = 0;
    for (int j = 0; j < m; ++j) {
      if (line[j] == 'G') {
        dwarf = j;
      }
      if (line[j] == 'S') {
        candy = j;
      }
    }
    if (dwarf > candy) {
      cout << -1;
      return 0;
    }
    distances.insert(candy - dwarf);
  }
  cout << distances.size();
  return 0;
}
