#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[1010];
map<int, bool> mark;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> s[i];
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    bool st = 0;
    int cnt = 0, pos1, pos2;
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') {
        st = 0;
        pos2 = j;
      }
      if (st) cnt++;
      if (s[i][j] == 'G') {
        st = 1;
        pos1 = j;
      }
    }
    if (pos1 > pos2) {
      cout << -1 << endl;
      return 0;
    }
    if (!mark[cnt]) {
      ans++;
      mark[cnt] = 1;
    }
  }
  cout << ans << endl;
  return 0;
}
