#include <bits/stdc++.h>
using namespace std;
set<int> s;
int solve(vector<vector<char> > arr, int i) {
  bool c = false;
  int ci = 0;
  int di = 0;
  for (int j = 0; j < arr[i].size(); j++) {
    if (arr[i][j] == 'S') {
      c = true;
      ci = j;
    } else if (arr[i][j] == 'G' && c)
      return -1;
    else if (arr[i][j] == 'G') {
      di = j;
    }
  }
  s.insert(ci - di);
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<char> > arr(n, vector<char>(m, '*'));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> arr[i][j];
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    k = solve(arr, i);
    if (k == -1) break;
  }
  if (k == -1)
    cout << -1;
  else
    cout << s.size();
}
