#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  char c;
  set<int> s;
  for (int i = 0; i < n; ++i) {
    int G = 0, S = 0;
    int last_g = -1, first_s = -1;
    for (int j = 0; j < m; ++j) {
      cin >> c;
      if (c == 'G') ++G, last_g = j;
      if (c == 'S') ++S, first_s = (first_s == -1) ? j : first_s;
      if (first_s <= last_g) first_s = -1;
    }
    if (G > 1 || G > S || last_g > first_s) {
      cout << -1 << endl;
      return;
    }
    if (G == 0) continue;
    s.insert(first_s - last_g);
  }
  cout << s.size() << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  solve();
  return 0;
}
