#include <bits/stdc++.h>
using namespace std;
int cnt[1001] = {0};
int main() {
  int row, col, i, j, x, neg = 0, min = 0, max, y;
  char c;
  cin >> row >> col;
  int S[row][2];
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      cin >> c;
      if (c == 'S')
        S[i][0] = j;
      else if (c == 'G')
        S[i][1] = j;
    }
  }
  for (i = 0; i < row; i++) {
    x = S[i][0] - S[i][1];
    if (x < 0) {
      neg++;
      break;
    } else {
      if (cnt[x] == 0) {
        cnt[x] = 1;
        min++;
      }
    }
  }
  if (neg > 0)
    cout << "-1" << endl;
  else
    cout << min << endl;
  return 0;
}
