#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 10;
const int MOD = (int)1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  string arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  set<int> s;
  for (int i = 0; i < n; i++) {
    int ctr = 0, seen = 0;
    for (int j = 0; j < m; j++) {
      if (seen) ctr++;
      if (arr[i][j] == 'S') {
        if (!seen)
          return cout << -1, 0;
        else
          s.insert(ctr);
      }
      if (arr[i][j] == 'G') seen = true;
    }
  }
  cout << s.size();
  return 0;
}
