#include <bits/stdc++.h>
template <class T>
typename T::value_type arr_sum(const T& v, int n) {
  typename T::value_type sum = 0;
  for (int i = 0; i < n; ++i) sum += v[i];
  return sum;
}
template <class T>
typename T::value_type arr_count(const T& v, int n, int t) {
  typename T::value_type count = 0;
  for (int i = 0; i < n; ++i) count += v[i] == t;
  return count;
}
template <class T>
typename T::value_type arr_maxi(const T& v, int n) {
  typename T::value_type maxi = 0;
  for (int i = 1; i < n; ++i)
    if (v[maxi] < v[i]) maxi = i;
  return maxi;
}
template <class T>
typename T::value_type arr_mini(const T& v, int n) {
  typename T::value_type mini = 0;
  for (int i = 1; i < n; ++i)
    if (v[mini] > v[i]) mini = i;
  return mini;
}
template <class T>
typename T::value_type arr_max(const T& v, int n) {
  typename T::value_type max = v[0];
  for (int i = 1; i < n; ++i)
    if (max < v[i]) max = v[i];
  return max;
}
template <class T>
typename T::value_type arr_min(const T& v, int n) {
  typename T::value_type min = v[0];
  for (int i = 1; i < n; ++i)
    if (min > v[i]) min = v[i];
  return min;
}
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    int tmp = strchr(s.c_str(), 'S') - strchr(s.c_str(), 'G');
    if (tmp < 0) {
      cout << -1;
      return 0;
    }
    v.push_back(tmp);
  }
  sort(v.begin(), v.end());
  int res = v.size();
  for (int i = 1; i < n; ++i) {
    if (v[i] == v[i - 1]) {
      --res;
    }
  }
  cout << res;
}
