#include <bits/stdc++.h>
using namespace std;
int dist[1010];
int main() {
  int n, m, gg, ss, ans = 0;
  string mat;
  bool flag = true;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> mat;
    gg = ss = 0;
    for (int j = 0; j < m; j++) {
      if (mat[j] == 'G')
        gg = j;
      else if (mat[j] == 'S')
        ss = j;
      else if (gg & ss)
        break;
    }
    if (ss - gg < 0)
      flag = false;
    else {
      if (dist[ss - gg] == 0) ans++;
      dist[ss - gg]++;
    }
  }
  if (flag)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
