#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
int gap[1005];
int main() {
  int n, m, i, j;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", a[i]);
  }
  int count = 0;
  for (i = 0; i < n; i++) {
    int start = 0;
    int end = 0;
    for (j = 0; j < m; j++) {
      if (a[i][j] == 'G') start = j;
      if (a[i][j] == 'S') end = j;
    }
    gap[i] = end - start;
  }
  sort(gap, gap + n);
  if (gap[0] < 0) {
    printf("-1\n");
    return 0;
  }
  i = 0;
  while (i < n && gap[i] == 0) i++;
  if (i == n) {
    printf("%d\n", 0);
  } else {
    int same = 0;
    while (i < (n - 1)) {
      if (gap[i] == gap[i + 1]) same++;
      i++;
    }
    printf("%d\n", n - same);
  }
  return 0;
}
