#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<vector<bool>> x(n, vector<bool>(m, 0));
  set<int> s;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'G') {
        bool f = 0;
        for (int k = j + 1; k < m; ++k) {
          if (a[i][k] == 'S' and x[i][k] != 1) {
            f = 1;
            s.insert(k - j);
            x[i][k] = 1;
            break;
          }
        }
        if (!f) {
          cout << -1;
          return;
        }
      }
    }
  }
  cout << s.size();
}
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
