#include <bits/stdc++.h>
using namespace std;
int arrr[1000];
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> arr;
  int flag = 0;
  int M = 0;
  for (int i = 0; i < n; i++) {
    arr.push_back(vector<char>(m));
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
    if (find(arr[i].begin(), arr[i].end(), 'G') - arr[i].begin() >
        find(arr[i].begin(), arr[i].end(), 'S') - arr[i].begin()) {
      cout << -1;
      exit(0);
    }
    if (arrr[distance(find(arr[i].begin(), arr[i].end(), 'G'),
                      find(arr[i].begin(), arr[i].end(), 'S'))] == 0) {
      M++;
      arrr[distance(find(arr[i].begin(), arr[i].end(), 'G'),
                    find(arr[i].begin(), arr[i].end(), 'S'))] = 1;
    }
  }
  cout << M;
}
