#include <bits/stdc++.h>
using namespace std;
long long fastPower(long long b, long long p) {
  if (!p) return 1;
  long long answer = fastPower(b, p / 2);
  answer = ((answer % (long long)(1e9 + 7)) * (answer % (long long)(1e9 + 7))) %
           (long long)(1e9 + 7);
  if (p % 2)
    answer = ((answer % (long long)(1e9 + 7)) * (b % (long long)(1e9 + 7))) %
             (long long)(1e9 + 7);
  return answer;
}
long long GCD(long long a, long long b) {
  if (!b) return a;
  return GCD(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(9);
  ;
  set<int> ss;
  int n, m;
  cin >> n >> m;
  vector<int>* arr = new vector<int>[n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char x;
      cin >> x;
      arr[i].push_back(x);
    }
  }
  for (int i = 0; i < n; i++) {
    bool g = 0;
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == 'S' && !g) {
        cout << -1;
        return 0;
      }
      if (arr[i][j] == 'G') g = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    int s, g;
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == 'S') s = j;
      if (arr[i][j] == 'G') g = j;
    }
    ss.insert(s - g);
  }
  cout << ss.size();
}
