#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, p, q, i, j, k;
  cin >> n >> m;
  k = 0;
  char a[n][m];
  int arr[1005];
  int freq[1005];
  memset(freq, 0, sizeof(freq));
  int cnt = 0;
  for (i = 0; i < n; i++) {
    p = 0;
    q = 0;
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        p = j;
      }
      if (a[i][j] == 'G') {
        q = j;
      }
    }
    if (q > p) {
      cout << "-1";
      return 0;
    }
    if (p > q) {
      arr[k++] = p - q;
    }
  }
  for (i = 0; i < k; i++) {
    freq[arr[i]]++;
  }
  for (i = 0; i < 1005; i++) {
    if (freq[i] != 0) cnt++;
  }
  cout << cnt;
  return 0;
}
