#include <bits/stdc++.h>
char le[1009];
int i, j, n, m, mi, cnt, f, jl[1009], x, y;
int main() {
  scanf("%d%d%*c", &n, &m);
  memset(jl, 0, sizeof(0));
  f = 1;
  for (i = 0; i < n; i++) {
    scanf("%s", le);
    x = 1009, y = 0;
    for (j = 0; j < m; j++) {
      if (le[j] == 'G') x = j;
      if (le[j] == 'S') {
        y = j;
        if ((y - x) < 0)
          f = 0;
        else
          jl[y - x]++;
        break;
      }
    }
    memset(le, 0, sizeof(le));
    if (!f) break;
  }
  if (!f)
    printf("%d\n", -1);
  else {
    cnt = 0;
    for (i = 0; i <= m; i++)
      if (jl[i]) cnt++;
    printf("%d\n", cnt);
  }
  return 0;
}
