#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159365;
int n, m;
char a[1100][1100];
vector<int> v;
int check(int x, int y) {
  int ans = 0;
  while (y <= m - 1) {
    if (a[x][y++] == 'S') return ans;
    ans++;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int res;
      if (a[i][j] == 'G') {
        res = check(i, j);
        if (!res) {
          puts("-1");
          return 0;
        } else
          v.push_back(res);
      }
    }
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    if (!v[i]) continue;
    ans++;
    for (int j = i + 1; j < v.size(); j++) v[j] -= v[i];
  }
  cout << ans;
  return 0;
}
