#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d\n", &n, &m);
  int flag = 1;
  int a[5000] = {};
  for (int i = 1; i <= n; i++) {
    char s[5000];
    gets(s);
    char *s1 = strchr(s, 'G');
    char *s2 = strchr(s, 'S');
    if (s2 - s1 < 0)
      flag = 0;
    else
      a[s2 - s1] = 1;
  }
  if (!flag)
    cout << -1 << endl;
  else
    printf("%d\n", accumulate(a, a + m, 0));
  return 0;
}
