#include <bits/stdc++.h>
using namespace std;
const int OO = 1e9;
const double EPS = 1e-9;
int main() {
  int n, m;
  cin >> n >> m;
  set<int> distances;
  while (n--) {
    string s;
    cin >> s;
    int dwarf_index = 0;
    for (; dwarf_index < m; dwarf_index++) {
      if (s[dwarf_index] == 'G') {
        break;
      }
    }
    int candy_index = dwarf_index + 1;
    for (; candy_index < m; candy_index++) {
      if (s[candy_index] == 'S') {
        break;
      }
    }
    if (candy_index == m) {
      cout << -1;
      return 0;
    }
    distances.insert(candy_index - dwarf_index);
  }
  cout << distances.size();
  return 0;
}
