#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ar1, ar2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  char mat[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> mat[i][j];
  }
  bool possible = true;
  for (int i = 0; i < n; i++) {
    int clown = -1;
    for (int j = 0; j < m; j++) {
      if (mat[i][j] == 'S' && clown == -1) {
        possible = false;
        break;
      } else if (mat[i][j] == 'G') {
        clown = j;
      }
    }
  }
  if (!possible)
    cout << -1 << '\n';
  else {
    int count = 0;
    vector<int> dif;
    int c = 0;
    for (int i = 0; i < n; i++) {
      c = 0;
      for (int j = 0; j < m; j++) {
        if (mat[i][j] == 'G')
          c -= j;
        else if (mat[i][j] == 'S')
          c += j;
      }
      dif.push_back(c);
    }
    sort(dif.begin(), dif.end());
    queue<int> q;
    for (auto i : dif) q.push(i);
    while (!q.empty()) {
      count++;
      int sub = q.front();
      q.pop();
      int len = q.size();
      for (int i = 0; i < len; i++) {
        int f = q.front();
        q.pop();
        f -= sub;
        if (f > 0) q.push(f);
      }
    }
    cout << count << '\n';
  }
}
