#include <bits/stdc++.h>
char a[1001][1001];
int n, m;
int findG(int r) {
  int i;
  for (i = 0; i < m; i = i + 1)
    if (a[r][i] == 'G') return i;
}
int findS(int r) {
  int i;
  for (i = 0; i < m; i = i + 1)
    if (a[r][i] == 'S') return i;
}
int main() {
  scanf("%d%d", &n, &m);
  int N = n;
  while (N--) {
    scanf("%s", &a[N]);
  }
  int f[1001] = {0};
  for (int l = 0; l < n; l++) {
    int dif = findS(l) - findG(l);
    if (dif < 0) {
      printf("-1");
      return 0;
    } else
      f[dif] = 1;
  }
  int s = 0;
  for (int d = 0; d < m; d++) s = s + f[d];
  printf("%d", s);
  return 0;
}
