#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, g[N], s[N];
char a[N][N];
bool f[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", a[i]);
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'G') {
        g[i] = j;
      } else if (a[i][j] == 'S') {
        s[i] = j;
      }
    }
    if (s[i] - g[i] < 0) {
      puts("-1");
      return 0;
    }
    f[s[i] - g[i]] = true;
  }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    if (f[i]) ans++;
  }
  printf("%d\n", ans);
}
