#include <bits/stdc++.h>
bool valid_line(std::string &s) {
  bool g = false;
  for (const auto &c : s) {
    if (c == 'G') {
      g = true;
    } else if (c == 'S') {
      return g;
    }
  }
  return g;
}
int gs_length(std::string &s) {
  if (valid_line(s)) {
    int len = 0;
    bool startcounting = false;
    for (const auto &c : s) {
      if (c == 'G') {
        startcounting = true;
      } else if (c == 'S') {
        return len;
      } else {
        if (startcounting) {
          len++;
        }
      }
    }
    return len;
  } else {
    return -1;
  }
}
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  int r, c;
  std::cin >> r >> c;
  std::unordered_set<int> len;
  len.reserve(r);
  for (int i = 0; i < r; ++i) {
    std::string s;
    std::cin >> s;
    int l = gs_length(s);
    if (l == -1) {
      std::cout << "-1" << std::endl;
      return 0;
    } else {
      len.insert(l);
    }
  }
  std::cout << len.size() << std::endl;
  return 0;
}
