#include <bits/stdc++.h>
using namespace std;
int main() {
  std::iostream::sync_with_stdio(false);
  int n, m, i, j, u, flag = 1;
  cin >> n >> m;
  char arr[n][m];
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) cin >> arr[i][j];
  int a[n], g, s, g1, s1;
  for (i = 0; i < n; i++) {
    g = s = 0;
    for (j = 0; j < m; j++) {
      if (arr[i][j] == 'G') {
        if (s == 1) {
          flag = 0;
          break;
        } else {
          g1 = j;
          g++;
        }
      } else if (arr[i][j] == 'S') {
        s1 = j;
        s++;
      }
    }
    a[i] = s1 - g1;
  }
  if (flag == 0)
    cout << "-1" << endl;
  else {
    sort(a, a + n);
    u = 1;
    for (i = 0; i < n - 1; i++) {
      if (a[i] == a[i + 1])
        continue;
      else
        u++;
    }
    cout << u << endl;
  }
  return 0;
}
