#include <bits/stdc++.h>
using namespace std;
char a[1000 + 5][1000 + 5];
int b[1000 + 5];
set<int> q;
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    int flag = 0;
    int p, q;
    for (int i = 1; i <= n; i++) {
      p = 0, q = 0;
      getchar();
      for (int j = 1; j <= m; j++) {
        scanf("%c", &a[i][j]);
        if (a[i][j] == 'G') {
          p = j;
        } else if (a[i][j] == 'S') {
          q = j;
        }
      }
      if (p > q) flag = 1;
      b[i] = q - p;
    }
    if (flag == 1)
      printf("-1\n");
    else {
      sort(b + 1, b + n + 1);
      int sum = unique(b + 1, b + n + 1) - b - 1;
      printf("%d\n", sum);
    }
  }
}
