#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

inline int A(int a) {
	return a < 0 ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		dp[1][i][1] = 0;
	int ans = -inf;
	for(int it = 1; it < k; ++it) {
		for(int R = 1; R <= n; ++R) {
			for(int L = 1; L <= R; ++L) {
				int S = F(L, R);
				if(dp[L][R][it] > -inf) {
					int mn = R, mx = R;
					for(int N = R + 1; N <= n; ++N) {
						relax(dp[mn + 1][N][it + 1], dp[L][R][it] + A(S - F(mn + 1, N)));
						relax(dp[mx + 1][N][it + 1], dp[L][R][it] + A(S - F(mx + 1, N)));
						if(sum[N] < sum[mn]) mn = N;
						if(sum[N] > sum[mx]) mx = N;
					}
					for(int NL = R + 1; NL <= n; ++NL)
						for(int NR = NL; NR <= NL + 10 && NR <= n; ++NR)
							relax(dp[NL][NR][it + 1], dp[L][R][it] + A(S - F(NL, NR)));
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			ans = max(ans, dp[i][j][k]);
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

inline int A(int a) {
	return a < 0 ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		dp[1][i][1] = 0;
	int ans = -inf;
	for(int it = 1; it < k; ++it) {
		for(int R = 1; R <= n; ++R) {
			for(int L = 1; L <= R; ++L) {
				int S = F(L, R);
				if(dp[L][R][it] > -inf) {
					int mn = R, mx = R;
					for(int N = R + 1; N <= n; ++N) {
						relax(dp[mn + 1][N][it + 1], dp[L][R][it] + A(S - F(mn + 1, N)));
						relax(dp[mx + 1][N][it + 1], dp[L][R][it] + A(S - F(mx + 1, N)));
						if(sum[N] < sum[mn]) mn = N;
						if(sum[N] > sum[mx]) mx = N;
					}
					for(int NL = R + 1; NL <= R + 20; ++NL)
						for(int NR = NL; NR <= R + 20; ++NR)
							relax(dp[NL][NR][it + 1], dp[L][R][it] + A(S - F(NL, NR)));
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			ans = max(ans, dp[i][j][k]);
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

inline int A(int a) {
	return a < 0 ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		dp[1][i][1] = 0;
	int ans = -inf;
	for(int it = 1; it < k; ++it) {
		for(int L = 1; L <= n; ++L) {
			for(int R = L; R <= n; ++R) {
				int S = F(L, R);
				if(dp[L][R][it] > -inf) {
					int mn = R, mx = R;
					for(int N = R + 1; N <= n; ++N) {
						relax(dp[mn + 1][N][it + 1], dp[L][R][it] + A(S - F(mn + 1, N)));
						relax(dp[mx + 1][N][it + 1], dp[L][R][it] + A(S - F(mx + 1, N)));
						if(sum[N] < sum[mn]) mn = N;
						if(sum[N] > sum[mx]) mx = N;
					}
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			ans = max(ans, dp[i][j][k]);
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		dp[1][i][1] = 0;
	int ans = -inf;
	for(int L = 1; L <= n; ++L) {
		for(int R = L; R <= n; ++R) {
			int S = F(L, R);
			for(int it = 1; it < k; ++it) {
				if(dp[L][R][it] > -inf) {
					int mn = R, mx = R;
					for(int N = R + 1; N <= n; ++N) {
						relax(dp[mn + 1][N][it + 1], dp[L][R][it] + abs(S - F(mn + 1, N)));
						relax(dp[mx + 1][N][it + 1], dp[L][R][it] + abs(S - F(mx + 1, N)));
						if(sum[N] < sum[mn]) mn = N;
						if(sum[N] > sum[mx]) mx = N;
					}
				}
			}
			ans = max(ans, dp[L][R][k]);
		}
	}
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

inline int A(int a) {
	return a < 0 ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		dp[1][i][1] = 0;
	int ans = -inf;
	for(int it = 1; it < k; ++it) {
		for(int R = 1; R <= n; ++R) {
			for(int L = 1; L <= R; ++L) {
				int S = F(L, R);
				if(dp[L][R][it] > -inf) {
					vector < int > mx, mn;
					mx.push_back(R); mn.push_back(R);
					for(int N = R + 1; N <= n; ++N) {
						for(int i = mx.size() - 1; i >= 0 && i >= (int)mx.size() - 7; --i) {
							relax(dp[mx[i] + 1][N][it + 1], dp[L][R][it] + A(S - F(mx[i] + 1, N)));
							//if(i == 0) break;
						}
						for(int i = mn.size() - 1; i >= 0 && i >= (int)mn.size() - 7; --i) {
							relax(dp[mn[i] + 1][N][it + 1], dp[L][R][it] + A(S - F(mn[i] + 1, N)));
							//if(i == 0) break;
						}
						if(sum[N] < sum[mn.back()]) mx.push_back(N);
						if(sum[N] > sum[mx.back()]) mn.push_back(N);
					}
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			ans = max(ans, dp[i][j][k]);
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;
template<class T> inline T Abs(const T& x) { return x < 0 ? -x : x; }
template<class T> inline T Sqr(const T& x) { return x * x; }

const int maxn = 410;
const int maxk = 52;

int n, k;
int a[maxn];
int dp[maxn][maxn][maxk];
int mxdp[maxn][maxk];
int mndp[maxn][maxk];
int sum[maxn];
int ss[maxn][maxn];

inline int Get(int l, int r) {
    return sum[r] - (l > 0 ? sum[l-1] : 0);
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum[i] = a[i];
        if (i > 0) {
            sum[i] += sum[i-1];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            ss[i][j] = Get(i, j);
        }
    }

    memset(dp, -1, sizeof(dp));
    memset(mndp, -1, sizeof(mndp));
    memset(mxdp, -1, sizeof(mxdp));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            dp[i][j][1] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        mxdp[i][1] = dp[i][0][1] + Get(0, i);
        mndp[i][1] = dp[i][0][1] - Get(0, i);
        for (int j = 1; j <= i; ++j) {
            mxdp[i][1] = max(mxdp[i][1], dp[i][j][1] + Get(j, i));
            mndp[i][1] = max(mndp[i][1], dp[i][j][1] - Get(j, i));
        }
    }

    int best = -1;
    for (int kk = 2; kk <= k; ++kk) {
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= i; ++j) {
                if (mxdp[j-1][kk-1] != -1) {
                    dp[i][j][kk] = mxdp[j-1][kk-1] - Get(j, i);
                }
                if (mndp[j-1][kk-1] != -1 && mndp[j-1][kk-1] + Get(j, i) > dp[i][j][kk]) {
                    dp[i][j][kk] = mndp[j-1][kk-1] + Get(j, i);
                }
                if (mxdp[i][kk] == -1 || mxdp[i][kk] < dp[i][j][kk] + Get(j, i)) {
                    mxdp[i][kk] = dp[i][j][kk] + Get(j, i);
                }
                if (mndp[i][kk] == -1 || mndp[i][kk] < dp[i][j][kk] - Get(j, i)) {
                    mndp[i][kk] = dp[i][j][kk] - Get(j, i);
                }
                if (kk == k && best < dp[i][j][kk]) {
                    best = dp[i][j][kk];
                }
            }
        }
    }

    cout << best << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;
template<class T> inline T Abs(const T& x) { return x < 0 ? -x : x; }
template<class T> inline T Sqr(const T& x) { return x * x; }

const int maxn = 410;
const int maxk = 52;
const int inf = 1000 * 1000 * 1000;

int n, k;
int a[maxn];
int dp[maxn][maxn][maxk];
int mxdp[maxn][maxk];
int mndp[maxn][maxk];
int sum[maxn];
int ss[maxn][maxn];

inline int Get(int l, int r) {
    return sum[r] - (l > 0 ? sum[l-1] : 0);
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum[i] = a[i];
        if (i > 0) {
            sum[i] += sum[i-1];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            ss[i][j] = Get(i, j);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k; ++j) {
            mxdp[i][j] = -inf;
            mndp[i][j] = -inf;
        }
    }
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            dp[i][j][1] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        mxdp[i][1] = dp[i][0][1] + Get(0, i);
        mndp[i][1] = dp[i][0][1] - Get(0, i);
        for (int j = 1; j <= i; ++j) {
            mxdp[i][1] = max(mxdp[i][1], dp[i][j][1] + Get(j, i));
            mndp[i][1] = max(mndp[i][1], dp[i][j][1] - Get(j, i));
        }
    }

    int best = -1;
    for (int kk = 2; kk <= k; ++kk) {
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= i; ++j) {
                if (mxdp[j-1][kk-1] != -inf) {
                    dp[i][j][kk] = mxdp[j-1][kk-1] - Get(j, i);
                }
                if (mndp[j-1][kk-1] != -inf && mndp[j-1][kk-1] + Get(j, i) > dp[i][j][kk]) {
                    dp[i][j][kk] = mndp[j-1][kk-1] + Get(j, i);
                }
                if (dp[i][j][kk] != -1 && (mxdp[i][kk] == -inf || mxdp[i][kk] < dp[i][j][kk] + Get(j, i))) {
                    mxdp[i][kk] = dp[i][j][kk] + Get(j, i);
                }
                if (dp[i][j][kk] != -1 && (mndp[i][kk] == -inf || mndp[i][kk] < dp[i][j][kk] - Get(j, i))) {
                    mndp[i][kk] = dp[i][j][kk] - Get(j, i);
                }
                if (kk == k && best < dp[i][j][kk]) {
                    best = dp[i][j][kk];
                }
            }
        }
    }

    cout << best << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;
template<class T> inline T Abs(const T& x) { return x < 0 ? -x : x; }
template<class T> inline T Sqr(const T& x) { return x * x; }

const int maxn = 410;
const int maxk = 52;
const int inf = 1000000000;

int n, k;
int a[maxn];
int dp[maxn][maxn][maxk];
int mxdp[maxn][maxk];
int mndp[maxn][maxk];
int sum[maxn];
int ss[maxn][maxn];

inline int Get(int l, int r) {
    return sum[r] - (l > 0 ? sum[l-1] : 0);
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum[i] = a[i];
        if (i > 0) {
            sum[i] += sum[i-1];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            ss[i][j] = Get(i, j);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k; ++j) {
            mxdp[i][j] = -inf;
            mndp[i][j] = -inf;
        }
    }
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            dp[i][j][1] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        mxdp[i][1] = dp[i][0][1] + Get(0, i);
        mndp[i][1] = dp[i][0][1] - Get(0, i);
        for (int j = 1; j <= i; ++j) {
            mxdp[i][1] = max(mxdp[i][1], dp[i][j][1] + Get(j, i));
            mndp[i][1] = max(mndp[i][1], dp[i][j][1] - Get(j, i));
        }
    }

    int best = -1;
    for (int kk = 2; kk <= k; ++kk) {
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= i; ++j) {
                if (mxdp[j-1][kk-1] != -inf) {
                    dp[i][j][kk] = mxdp[j-1][kk-1] - Get(j, i);
                }
                if (mndp[j-1][kk-1] != -inf && mndp[j-1][kk-1] + Get(j, i) > dp[i][j][kk]) {
                    dp[i][j][kk] = mndp[j-1][kk-1] + Get(j, i);
                }
                if (mxdp[i][kk] == -inf || mxdp[i][kk] < dp[i][j][kk] + Get(j, i)) {
                    mxdp[i][kk] = dp[i][j][kk] + Get(j, i);
                }
                if (mndp[i][kk] == -inf || mndp[i][kk] < dp[i][j][kk] - Get(j, i)) {
                    mndp[i][kk] = dp[i][j][kk] - Get(j, i);
                }
                if (kk == k && best < dp[i][j][kk]) {
                    best = dp[i][j][kk];
                }
            }
        }
    }

    cout << best << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;
template<class T> inline T Abs(const T& x) { return x < 0 ? -x : x; }
template<class T> inline T Sqr(const T& x) { return x * x; }

const int maxn = 410;
const int maxk = 52;

int n, k;
int a[maxn];
int dp[maxn][maxn][maxk];
int sum[maxn];

inline int Get(int l, int r) {
    return sum[r] - (l > 0 ? sum[l-1] : 0);
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum[i] = a[i];
        if (i > 0) {
            sum[i] += sum[i-1];
        }
    }

    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            dp[i][j][1] = 0;
        }
    }

    int best = -1;
    for (int kk = 2; kk <= k; ++kk) {
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= i; ++j) {
                int s = Get(j, i);
                for (int q = 0; q <= j - 1; ++q) {
                    if (dp[j-1][q][kk-1] != -1 && (dp[i][j][kk] == -1 || dp[i][j][kk] < dp[j-1][q][kk-1] + Abs(Get(q, j-1) - s))) {
                        dp[i][j][kk] = dp[j-1][q][kk-1] + Abs(Get(q, j-1) - s);
                        if (kk == k && dp[i][j][kk] > best) {
                            best = dp[i][j][kk];
                        }
                    }
                }
            }
        }
    }

    cout << best << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;
template<class T> inline T Abs(const T& x) { return x < 0 ? -x : x; }
template<class T> inline T Sqr(const T& x) { return x * x; }

const int maxn = 410;
const int maxk = 52;
const int inf = 1000000000;

int n, k;
int a[maxn];
int dp[maxn][maxn][maxk];
int mxdp[maxn][maxk];
int mndp[maxn][maxk];
int sum[maxn];
int ss[maxn][maxn];

inline int Get(int l, int r) {
    return sum[r] - (l > 0 ? sum[l-1] : 0);
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum[i] = a[i];
        if (i > 0) {
            sum[i] += sum[i-1];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            ss[i][j] = Get(i, j);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= k; ++j) {
            mxdp[i][j] = -inf;
            mndp[i][j] = -inf;
        }
    }
    memset(dp, -1, sizeof(dp));
    memset(mndp, -1, sizeof(mndp));
    memset(mxdp, -1, sizeof(mxdp));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= i; ++j) {
            dp[i][j][1] = 0;
        }
    }
    for (int i = 0; i < n; ++i) {
        mxdp[i][1] = dp[i][0][1] + Get(0, i);
        mndp[i][1] = dp[i][0][1] - Get(0, i);
        for (int j = 1; j <= i; ++j) {
            mxdp[i][1] = max(mxdp[i][1], dp[i][j][1] + Get(j, i));
            mndp[i][1] = max(mndp[i][1], dp[i][j][1] - Get(j, i));
        }
    }

    int best = -1;
    for (int kk = 2; kk <= k; ++kk) {
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= i; ++j) {
                if (mxdp[j-1][kk-1] != -inf) {
                    dp[i][j][kk] = mxdp[j-1][kk-1] - Get(j, i);
                }
                if (mndp[j-1][kk-1] != -inf && mndp[j-1][kk-1] + Get(j, i) > dp[i][j][kk]) {
                    dp[i][j][kk] = mndp[j-1][kk-1] + Get(j, i);
                }
                if (mxdp[i][kk] == -inf || mxdp[i][kk] < dp[i][j][kk] + Get(j, i)) {
                    mxdp[i][kk] = dp[i][j][kk] + Get(j, i);
                }
                if (mndp[i][kk] == -inf || mndp[i][kk] < dp[i][j][kk] - Get(j, i)) {
                    mndp[i][kk] = dp[i][j][kk] - Get(j, i);
                }
                if (kk == k && best < dp[i][j][kk]) {
                    best = dp[i][j][kk];
                }
            }
        }
    }

    cout << best << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:640000000")

int n, k, a[20000], part[20000];
int dp[60][500][500], table[500][2];
int main()
{
 srand(time(0));
 //freopen("input.txt","r",stdin);
 //freopen("output.txt","w",stdout);
 scanf("%d %d",&n,&k);
 for (int i = 1 ; i <= n ; i ++ ) {
	 scanf("%d",&a[i]);
	 part[i] = part[i - 1] + a[i];
 };
 for (int i = 0 ; i < 500 ; i ++ )
	 table[i][0] = table[i][1] = -2e9;

 for (int i = 1 ; i <= n ; i ++ )
	 for (int j = i ; j <= n ; j ++ ) {
		 dp[1][i][j] = 0;
		 table[j][0] = max(table[j][0], dp[1][i][j] + part[j] - part[i - 1]);
		 table[j][1] = max(table[j][1], dp[1][i][j] - part[j] + part[i - 1]);
	 };

 for (int t = 2 ; t <= k ; t ++ ) {
	 for (int i = 1 ; i <= n ; i ++ )
		 for (int j = i ; j <= n ; j ++ )
			 dp[t][i][j] = max(table[i - 1][0] - part[j] + part[i - 1], table[i - 1][1] - part[i - 1] + part[j]);
	 for (int i = 0 ; i < n ; i ++ )
		 table[i][0] = table[i][1] = -2e9;
	 for (int i = 1 ; i <= n ; i ++ )
		 for (int j = i ; j <= n ; j ++ ) {
			 table[j][0] = max(table[j][0], dp[t][i][j] + part[j] - part[i - 1]);
			 table[j][1] = max(table[j][1], dp[t][i][j] - part[j] + part[i - 1]);
		 };
 };
 int answer = 0;
 for (int i = 1 ; i <= n ; i ++ )
	 for (int j = i ; j <= n ; j ++ )
		 answer = max(answer, dp[k][i][j]);
 cout << answer << endl;
 return 0;
};


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 444;
const int MAXK = 55;
const int INF = 1e9;

int dp[MAXN][MAXN][MAXK];
int a[MAXN],s[MAXN];

int main(){
  std::ios::sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif
  memset(dp,0,sizeof(dp));

  int N,K;
  cin >> N >> K;
  ++N;++K;
  s[0] = 0;
  for(int i=1;i<N;++i){
    cin >> a[i];
    s[i] = s[i-1] + a[i];
  }

  for(int i=1;i<N;++i){
    for(int j=i;j<N;++j){
      for(int k=2;k<K;++k){
        dp[i][j][k] = -INF;
      }
    }
  }

  for(int i=2;i<N;++i){
    for(int j=i;j<N;++j){
      for(int k=2;k<K;++k){
        for(int l=1;l<i;++l){
          dp[i][j][k] = max(dp[i][j][k], dp[l][i-1][k-1] + abs(s[j] - 2*s[i-1] + s[l-1]));
        }
        //cout << i << " " << j << " " <<  k << " " << dp[i][j][k] << endl;
      }
    }
  }

  int res = 0;
  for(int i=1;i<N;++i){
    for(int j=i;j<N;++j){
      res = max(res,dp[i][j][K-1]);
    }
  }

  cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 405;
const int K = 55;

long long a[N],dis[N];
long long dp[N][N][K];

inline long long get(int x, int y){
  return dis[y] - dis[x-1];
}

int main(){
  std::ios::sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif
  memset(dp,-1,sizeof(dp));

  int n,k;
  long long res=0;
  cin >> n >> k;
  dis[0] = 0;
  for(int i=1;i<=n;++i){
    cin >> a[i];
    dis[i] = dis[i-1] + a[i];
  }

  for(int i=1;i<=n;++i){
    for(int j=i;j<=n;++j){
      dp[i][j][1] = 0;
    }
  }

  for(int kk=1;kk<k;++kk){
    for(int i=1;i<n;++i){
      for(int j=i;j<n;++j){
        if(dp[i][j][kk] != -1){
          for(int l=j+1;l<=n;++l){
            dp[j+1][l][kk+1] = max(dp[j+1][l][kk+1],dp[i][j][kk] + abs(get(i,j) - get(j+1,l)));
            if(kk+1 == k)
              res = max(dp[j+1][l][k],res);
          }
        }
      }
    }
  }

  cout << res << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 405;
const int K = 55;

long long a[N],dis[N];
long long dp[N][N][K];

long long get(int x, int y){
  return dis[y] - dis[x-1];
}

int main(){
  std::ios::sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif
  memset(dp,-1,sizeof(dp));

  int n,k;
  long long res=0;
  cin >> n >> k;
  dis[0] = 0;
  for(int i=1;i<=n;++i){
    cin >> a[i];
    dis[i] = dis[i-1] + a[i];
  }

  for(int i=1;i<=n;++i){
    for(int j=i;j<=n;++j){
      dp[i][j][1] = 0;
    }
  }

  for(int kk=1;kk<k;++kk){
    for(int i=1;i<n;++i){
      for(int j=i;j<n;++j){
        if(dp[i][j][kk] != -1){
          for(int l=j+1;l<=n;++l){
            if(dp[i][j][kk] != -1)
            dp[j+1][l][kk+1] = max(dp[j+1][l][kk+1],dp[i][j][kk] + abs(get(i,j) - get(j+1,l)));
          }
        }
      }
    }
  }

  for(int i=1;i<=n;++i){
    for(int j=i;j<=n;++j){
      res = max(res,dp[i][j][k]);
    }
  }

  cout << res << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define pb push_back
#define pii pair<int,int>

ll dx4[4] = {0, 1, 0, -1};
ll dy4[4] = {1, 0, -1, 0};
ll dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

ll power(ll b, ll p){
    if(p==0) return 1;
    ll t = power(b,p/2);
    t = t*t;
    if(p%2 == 1) t = t*b;
    return t;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    string mat[n];

    for(int i=0; i<n; i++){
        cin >> mat[i];
    }

    bool isPossible = true;

    int ans = INT_MAX;

    for(int i=0; i<n; i++){
        int s, g;
        for(int j=0; j<m; j++){
            if(mat[i][j] == 'S') s = j;
            if(mat[i][j] == 'G') g = j;
            if(g>s) isPossible = false;
        }
        ans = min(ans, s-g);
    }

    if(isPossible) cout << ans;
    else cout << -1;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define pb push_back
#define pii pair<int,int>

ll dx4[4] = {0, 1, 0, -1};
ll dy4[4] = {1, 0, -1, 0};
ll dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

ll power(ll b, ll p){
    if(p==0) return 1;
    ll t = power(b,p/2);
    t = t*t;
    if(p%2 == 1) t = t*b;
    return t;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    string mat[n];

    for(int i=0; i<n; i++){
        cin >> mat[i];
    }

    bool isPossible = true;

    int ans = 0;

    for(int i=0; i<n; i++){
        int s, g;
        for(int j=0; j<m; j++){
            if(mat[i][j] == 'S') s = j;
            if(mat[i][j] == 'G') g = j;
            if(g>s) isPossible = false;
            ans = max(ans, s-g);
        }
    }

    if(isPossible) cout << ans;
    else cout << -1;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define pb push_back
#define pii pair<int,int>

ll dx4[4] = {0, 1, 0, -1};
ll dy4[4] = {1, 0, -1, 0};
ll dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

ll power(ll b, ll p){
    if(p==0) return 1;
    ll t = power(b,p/2);
    t = t*t;
    if(p%2 == 1) t = t*b;
    return t;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    string mat[n];

    for(int i=0; i<n; i++){
        cin >> mat[i];
    }

    bool isPossible = true;

    int ans = INT_MAX;

    for(int i=0; i<n; i++){
        int s, g;
        for(int j=0; j<m; j++){
            if(mat[i][j] == 'S') s = j;
            if(mat[i][j] == 'G') g = j;
        }
        if(g>s) isPossible = false;
        ans = min(ans, s-g);
    }

    if(isPossible) cout << ans;
    else cout << -1;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define pb push_back
#define pii pair<int,int>

ll dx4[4] = {0, 1, 0, -1};
ll dy4[4] = {1, 0, -1, 0};
ll dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

ll power(ll b, ll p){
    if(p==0) return 1;
    ll t = power(b,p/2);
    t = t*t;
    if(p%2 == 1) t = t*b;
    return t;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    string mat[n];

    for(int i=0; i<n; i++){
        cin >> mat[i];
    }

    bool isPossible = true;

    int ans = 0;

    for(int i=0; i<n; i++){
        int s, g;
        for(int j=0; j<m; j++){
            if(mat[i][j] == 'S') s = j;
            if(mat[i][j] == 'G') g = j;
            if(g>s) isPossible = false;
        }
        ans = max(ans, s-g);
    }

    if(isPossible) cout << ans;
    else cout << -1;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007
#define pb push_back
#define pii pair<int,int>

ll dx4[4] = {0, 1, 0, -1};
ll dy4[4] = {1, 0, -1, 0};
ll dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

ll power(ll b, ll p){
    if(p==0) return 1;
    ll t = power(b,p/2);
    t = t*t;
    if(p%2 == 1) t = t*b;
    return t;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    string mat[n];

    for(int i=0; i<n; i++){
        cin >> mat[i];
    }

    bool isPossible = true;

    vector<int> arr;

    for(int i=0; i<n; i++){
        int s, g;
        for(int j=0; j<m; j++){
            if(mat[i][j] == 'S') s = j;
            if(mat[i][j] == 'G') g = j;
        }
        if(g>s){
            isPossible = false;
            break;
        }
        arr.pb(s-g);
    }

    if(!isPossible){
        cout << -1;
        return 0;
    }

    sort(arr.begin(), arr.end());

    unordered_map<int,int> hash;
    for(int i=0; i<arr.size(); i++) hash[arr[i]]++;

    cout << hash.size();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>G;
    vector<int>S;
    for(int i=0; i<n; i++)
    {
        for(int x=0; x<m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
                G.push_back(x);
            else if(arr[i][x]=='S')
                S.push_back(x);
        }
    }
    set<int>se;
    for(int i=0; i<S.size();i++)
    {
        if(S[i]<G[i])
        {
            cout<<"-1"<<endl;
            return 0;
            //system("pause");
        }
        else
        {

            se.insert(S[i]-G[i]);
        }
    }
    cout<<se.size()<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>v;
    vector<int>ans;
    bool ma=0;
     set<long long>se;
    for(int i=1; i<=n; i++)
    {
bool G=0;
        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
            {
                G=1;
                v.push_back(x);
            }
            if(arr[i][x]=='S'&&G==1)
            {
                ans.push_back(x);
                se.insert(ans.back()-v.back());

            }
            else
            {
                cout<<"-1"<<endl;
                return 0;
            }
        }
    }

    cout<<se.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>G;
    vector<int>S;
    for(int i=1; i<=n; i++)
    {
        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
                G.push_back(x);
            else if(arr[i][x]=='S')
                S.push_back(x);
        }
    }
    set<int>se;
    for(int i=0; i<S.size();i++)
    {
        if(S[i]<G[i])
        {
            cout<<"-1"<<endl;
            return 0;
            //system("pause");
        }
        else
        {

            se.insert(S[i]-G[i]);
        }
    }
    cout<<se.size()<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>v;
    vector<int>ans;
    bool ma=0;
    for(int i=1; i<=n; i++)
    {

        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
            {

                v.push_back(x);
            }
            if(arr[i][x]=='S')
            {

                ans.push_back(x);
            }
        }
    }

    set<int>se;
            for(int i=0; i<v.size(); i++)
            {
                se.insert(ans[i]-v[i]);
            }
            set<int>::iterator it;

       for(it=se.begin();it!=se.end();it++)
       {
           if((*it)<0)
           {
               ma=1;
               break;
           }
       }

if(ma==1)
    cout<<"-1"<<endl;
else
    cout<<se.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>v;
    vector<int>ans;
    bool ma=0;
     set<int>se;
    for(int i=1; i<=n; i++)
    {
bool G=0;
        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
            {
                G=1;
                v.push_back(x);
            }
            if(arr[i][x]=='S'&&G==1)
            {
                ans.push_back(x);
                se.insert(ans.back()-v.back());

            }
        }
    }

if(ans.size()!=v.size())
    cout<<"-1"<<endl;
else
    cout<<se.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>v;
    vector<int>ans;
    bool ma=0;
    for(int i=1; i<=n; i++)
    {
        bool found=0;
        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
            {
                found=1;
                v.push_back(x);
            }
            if(arr[i][x]=='S')
            {
                if(found==0)
                {
                    ma=1;
                }
                ans.push_back(x);
            }
        }
    }

    if(ma==1)
        cout<<"-1"<<endl;
    else
    {
       int counter=0;
        while(true)
        {

            for(int i=0; i<v.size(); i++)
            v[i]++;
            bool s=0;
             for(int i=0; i<v.size(); i++)
             {
                 if(v[i]==ans[i])
                 {s=1;
                 v.erase(v.begin()+i);
                 ans.erase(ans.begin()+i);
                 i--;
                 }
             }
             if(s==1)
                counter++;
                if(v.size()==0)
                    break;

        }
        cout<<counter<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n,m;
   cin>>n>>m;
   char arr[n][m];
   vector<pair<int,int> >v;
   vector<int>ans;
   bool ma=0;
   for(int i=1;i<=n;i++)
   {
       bool found=0;
       for(int x=1;x<=m;x++)
       {
           cin>>arr[i][x];
           if(arr[i][x]=='G')
           {
               found=1;
               v.push_back(make_pair(i,x));
           }
           if(arr[i][x]=='S')
           {
               if(found==0)
               {
                   ma=1;
               }
               v.push_back(make_pair(i,x));
           }
       }
   }
   for(int i=0;i<v.size();i++)
   {
       if(i+1<v.size())
       {
           ans.push_back(v[i+1].second-v[i].second);
       }
   }
if(ma==1)
    cout<<"-1"<<endl;
else
{
    sort(ans.begin(),ans.end());
    cout<<ans[ans.size()-1]-1<<endl;
}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    char arr[n][m];
    vector<int>v;
    vector<int>ans;
    bool ma=0;
     set<long long>se;
    for(int i=1; i<=n; i++)
    {
bool G=0;
        for(int x=1; x<=m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
            {
                G=1;
                v.push_back(x);
            }
            if(arr[i][x]=='S'&&G==1)
            {
                ans.push_back(x);
                se.insert(ans.back()-v.back());

            }
        }
    }

if(ans.size()!=v.size())
    cout<<"-1"<<endl;
else
    cout<<se.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;

    cin>>n>>m;
    char arr[n][m];
    vector<int>G;
    vector<int>S;
    for(int i=0; i<n; i++)
    {
        for(int x=0; x<m; x++)
        {
            cin>>arr[i][x];
            if(arr[i][x]=='G')
                G.push_back(x);
            else if(arr[i][x]=='S')
                S.push_back(x);
        }
    }
    set<int>se;
    for(int i=0; i<S.size();i++)
    {
        if(S[i]<G[i])
        {
            cout<<"-1"<<endl;
            return 0;
            //system("pause");
        }
        else
        {

            se.insert(S[i]-G[i]);
        }
    }
    cout<<se.size()<<endl;
}


#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,nxt[1010],cnt,tot[1010],ans;
char c[1010][1010];
map<int,int> mp;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++)
	{
		scanf("\n");
		for(int j=1; j<=m; j++)
		{
			scanf("%c",&c[i][j]);
		}
	}
	for(int i=1; i<=n; i++)
	{
		int pos=0x3f3f3f3f;
		for(int j=1; j<=m; j++)
		{
			if(c[i][j]=='G')
			{
				pos=min(pos,j);
			}
			if(c[i][j]=='S')
			{
				if(pos>2000)
				{
					puts("-1");
					return 0;
				}
				if(mp.count(j-pos)==0)
				{
					ans++;
					mp[j-pos]=1;
				}
			}
		}
	}
	printf("%d\n",ans);
}


#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m;
map<int,int> mp;
char c[1010][1010];
vector<int> ans;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++)
	{
		scanf("\n");
		for(int j=1; j<=m; j++)
		{
			scanf("%c",&c[i][j]);
		}
	}
	for(int i=1; i<=n; i++)
	{
		int pos=0x3f3f3f3f;
		for(int j=1; j<=m; j++)
		{
			if(c[i][j]=='G')
			{
				pos=min(pos,j);
			}
			if(c[i][j]=='S')
			{
				if(pos>2000)
				{
					puts("-1");
					return 0;
				}
				if(!mp.count(pos))
				{
					mp[pos]=1;
					ans.push_back(pos);
				}
			}
		}
	}
	printf("%d\n",ans.size());
}


#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,nxt[1010],cnt,tot[1010],ans;
char c[1010][1010];
map<int,int> mp;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++)
	{
		scanf("\n");
		for(int j=1; j<=m; j++)
		{
			scanf("%c",&c[i][j]);
		}
	}
	for(int i=1; i<=n; i++)
	{
		int pos=0x3f3f3f3f;
		for(int j=1; j<=m; j++)
		{
			if(c[i][j]=='G')
			{
				pos=min(pos,j);
			}
			if(c[i][j]=='S')
			{
				if(pos>2000)
				{
					puts("-1");
					return 0;
				}
			}
		}
	}
	for(int i=1; i<=m; i++)
	{
		for(int j=1; j<=n; j++)
		{
			if(c[j][i]=='S'&&mp.count(i)==0)
			{
				nxt[++cnt]=i;
				mp[i]=1;
			}
			if(c[j][i]=='G')
			{
				tot[i]++;
			}
		}
	}
	for(int i=1; i<=cnt; i++)
	{
		for(int j=1; j<nxt[i]; j++)
		{
			if(tot[j])
			{
				tot[nxt[i]]+=tot[j];
				ans++;
				tot[j]=0;
			}
		}
	}
	printf("%d\n",ans);
}


#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m;
map<int,int> mp;
char c[1010][1010];
vector<int> ans;

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++)
	{
		scanf("\n");
		for(int j=1; j<=m; j++)
		{
			scanf("%c",&c[i][j]);
		}
	}
	for(int i=1; i<=n; i++)
	{
		int pos=0x3f3f3f3f;
		for(int j=1; j<=m; j++)
		{
			if(c[i][j]=='G')
			{
				pos=min(pos,j);
			}
			if(c[i][j]=='S')
			{
				if(pos>2000)
				{
					puts("-1");
					return 0;
				}
				if(!mp.count(pos))
				{
					mp[pos]=1;
					ans.push_back(pos);
				}
				if(!mp.count(j))
				{
					mp[j]=1;
					ans.push_back(j);
				}
			}
		}
	}
	printf("%d\n",ans.size()-1);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,i,l,r,j;
char p[1005][1005];
set<int> s;
int main(){
    cin>>n>>m;
    for(i=0;i<n;i++) cin>>p[i];
    bool  flag=0;
    for(i=0;i<n;i++){
        l=r=0;
        for(j=0;j<m;j++){
            if(p[i][j]=='G')  l=j;
            if(p[i][j]=='S')  r=j;
        }
        if(l<r)s.insert(r-l);
        else flag=1;
    }
    if(flag==0) cout<<s.size()<<endl;
    else cout<<-1<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int ans,n,m;
char a[2005];
int main(){
	scanf("%d%d",&n,&m);
	ans=m;
	for (int i=1;i<=n;i++){
		int s,t;s=t=0;
		scanf("%s",a);
		for (int j=0;j<m;j++){
			if (a[j]=='G')s=j;
			if (a[j]=='S')t=j;
		}
		if (s<t)ans=min(ans,t-s);
	}
	printf("%d\n",ans==m?-1:ans);
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9+7;
const int MOD = 1e4+7;
const int maxn = 100005;
const int MAXN = 100010;
const int INF=0x3f3f3f3f;
int n,m,s,t,sum;
int b[1001];
char a[1001];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        s=0;
        scanf("%s",a);
        for(int j=1;j<=m;j++)
            if(a[j-1]=='G')
                s=j;
            else if(a[j-1]=='S')
            {
                if(s==0)
                {
                    printf("-1");
                    return 0;
                }
                else
                {
                    t=j;
                    b[t-s]=1;
                    break;
                }
            }
    }
    for(int i=1;i<=m;i++)
        if(b[i])
            sum++;
    printf("%d",sum);
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
using namespace std;
vector<int> v[1005];
int minn[1005];
char c[1005];
int main()
{
          for(int i=0;i<=1000;i++)
                    minn[i]=2000;
          int n,m,f=0;
          cin >> n >> m;
          for(int i=0;i<n;i++)
          {
                    cin >> c;
                    int g,s;
                    for(int j=0;j<m;j++)
                              if(c[j]=='G')
                                        g=j;
                              else if(c[j]=='S')
                                        s=j;
                    if(g>s)
                              f=1;
                    else
                    {
                              v[g].push_back(s);
                              minn[i]=min(minn[i],s);
                    }
          }
          if(f)
                    cout << -1 << endl;
          else
          {
                    int ans=0;
                    for(int i=0;i<m;i++)
                    {
                              if(!v[i].empty())
                                        ans++;
                              int cnt=0,t=minn[i];
                              while(!v[i].empty())
                              {
                                        int tp=v[i].back();
                                        v[i].pop_back();
                                        if(tp>t)
                                        {
                                                  v[t].push_back(tp);
                                                  minn[t]=min(minn[t],tp);
                                        }
                              }
                    }
                    cout << ans << endl;
          }
}


#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <random>
#include <ctime>
using namespace std;
#define CLR(a) memset(a, 0, sizeof a)
#define _unique(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end());
#define _all(v) (v).begin(),(v).end()
#define _endl putchar('\n')
#define _space putchar(' ')
const int32_t Accepted = 0;
template<typename T>void in(T& x) { static char ch; static bool neg; for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar()); for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar()); x = neg ? -x : x; }
template<typename T, typename... Types>void in(T& x, Types& ... oth) { static char ch; static bool neg; for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar()); for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar()); x = neg ? -x : x; in(oth...); }
template<typename T>T _pow_mod(T a, T b, T mod) { T base = a, res = 1; while (b) { if (b & 1)res = (res * base) % mod; base = (base * base) % mod; b >>= 1; }return res; }
template<typename T>T _pow(T a, T b) { T base = a, res = 1; while (b) { if (b & 1)res = (res * base); base = (base * base); b >>= 1; }return res; }
template<typename T>void out(T x) { char s[65]; int32_t cnt = 0; if (x > 0)while (x > 0)s[cnt++] = (x - (x / 10 * 10)) + '0', x /= 10; else if (x < 0) { while (x < 0)s[cnt++] = (x / 10 * 10) - x + '0', x /= 10; s[cnt++] = '-'; }if (cnt == 0)putchar('0'); else for (int32_t i = cnt - 1; ~i; i--)putchar(s[i]); }
template<typename T>void print(T x) { out(x); _endl; }
template<typename T, typename... types >void print(T x, types... oth) { out(x); _space; print(oth...); }
typedef long long ll;
//mt19937_64 _r(random_device{}());
const int32_t MAXN = 1e6 + 5;
//char Author[] = { -42, -20 ,-79 ,-72 ,-67 ,-88 ,0 };

char s[1123][1123];
int32_t main()
{
	//   freopen("1.in","r",stdin);
	int n, m;
	in(n, m);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", s[i]);
	}
	int ans = 0;
	bool f = false;
	set<int>SS;
	for (int i = 0; i < n; i++)
	{
		int j = 0;
		while (s[i][j] != 'G')j++;
		int G = j;
		j = 0;
		while (s[i][j] != 'S')j++;
		int S = j;
		if (S < G) {
			f = true;
		}
		else
			SS.insert(S - G);
	}
	if (!f)
		print(SS.size());
	else
		print(-1);

	return Accepted;
}


#include <bits/stdc++.h>
using namespace std;
int mp[1005];
char c[1005];
int main()
{
          ios::sync_with_stdio(0);
          int n,m,f=0;
          cin >> n >> m;
          for(int i=0;i<n;i++)
          {
                    cin >> c;
                    int g,s;
                    for(int j=0;j<m;j++)
                              if(c[j]=='G')
                                        g=j;
                              else if(c[j]=='S')
                                        s=j;
                    if(g>s)
                              f=1;
                    else
                              mp[s-g]=1;
          }
          int ans=0;
          for(int i=0;i<m;i++)
          {
                    if(mp[i])
                    ans++;
          }
          if(f)
                    cout << -1 << endl;
          else
                    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
int ans,n,m;
char a[2005],f[2005];
int main(){
	scanf("%d%d",&n,&m);
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++){
		int s,t;s=t=0;
		scanf("%s",a);
		for (int j=0;j<m;j++){
			if (a[j]=='G')s=j;
			if (a[j]=='S')t=j;
		}
		if (s>t){
			printf("-1");return 0;
		}
		f[t-s]++;
	}
	ans=0;
	for (int i=1;i<=m;i++)if (f[i])ans++;
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> > q[1005];
int mp[1005][1005];
char c[1005];
int main()
{
          ios::sync_with_stdio(0);
          int n,m,f=0;
          cin >> n >> m;
          for(int i=0;i<n;i++)
          {
                    cin >> c;
                    int g,s;
                    for(int j=0;j<m;j++)
                              if(c[j]=='G')
                                        g=j;
                              else if(c[j]=='S')
                                        s=j;
                    if(g>s)
                              f=1;
                    else
                              q[g].push(s);
          }
          for(int i=0;i<m;i++)
          {
                    int t=i,s;
                    while(!q[i].empty())
                    {
                              s=q[i].top();
                              mp[t][s]=1;
                              t=s;
                              q[i].pop();
                    }
          }
          int ans=0;
          for(int i=0;i<m;i++)
                    for(int j=i+1;j<m;j++)
                    if(mp[i][j])
                    ans++;
          if(f)
                    cout << -1 << endl;
          else
                    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

char s[1010];
int a[1010], b[1010], c[1010];

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n;i++){
        scanf("%s", s + 1);
        for (int j = 1; j <= m;j++){

            if(s[j]=='G')
                a[i] = j;
            if(s[j]=='S')
                b[i] = j;
        }
        c[i] = b[i] - a[i];
        if(c[i]<0){
            printf("-1\n");
            return 0;
        }
    }

    sort(c + 1, c + 1 + n);
    int num = (unique(c + 1, c + 1 + n) - (c + 1));
    if(c[1]==0)
        num--;
    printf("%d\n", num);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
char s[1005][1005];
int vis[1005];
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m)){
        for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
        for(int i=1;i<=n;i++){
            int tmp=0,f=1;
            for(int j=1;j<=m;j++){
                if(s[i][j]=='G'){
                    for(int k=j+1;k<=m;k++){
                        if(s[i][k]=='*')tmp++;
                        if(s[i][k]=='S'){
                            f=0;
                            vis[i]=tmp;
                            break;
                        }
                    }
                    if(f)vis[i]=-1;
                    break;
                }
            }
        }
        sort(vis+1,vis+n+1);
        if(vis[1]==-1)printf("-1\n");
        else{
            int x=0,res=0;
            while(++x<=n){
                if(vis[x]!=0){
                    res++;
                    for(int i=x+1;i<=n;i++)vis[i]-=vis[x];
                }
            }
            printf("%d\n",res);
        }
    }
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include <iostream>
using namespace std;
typedef long long ll;
char a[1003][1003];
ll d[1003], b[1003], kl = 0;
int cmp(const void* a, const void* b)
{
	return (ll*)b - (ll*)a;
}
char sd[1003];
int as(char p)
{
	if (p == 'S')
	{
		if (sd[kl] == 'G')
		{
			kl--;
		}
	}
	else
	{
		sd[++kl] = p;
	}
	return kl;
}
int main()
{
	ll n, m, s = 0;
	char p;
	scanf("%lld%lld\n", &n, &m);
	for (ll i = 0; i < n; i++)
	{
		for (ll j = 0; j < m; j++)
		{
			scanf("%c", &a[i][j]);
		}
		scanf_s("%c", &p);
	}
	for (ll i = 0; i < n; i++)
	{
		ll x = -1, y = -1, max = 0, g = 0, s6 = 0;
		for (ll j = 0; j < m; j++)
		{
			if (a[i][j] == 'G' && x == -1)
			{
				x = j;
			}
			if (a[i][j] == 'G')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				y = j;
				if (x != -1 && y > x)
				{
					d[s] = j - x;
				}
			}
			if (j == m - 1 && g != 0)
			{
				printf("-1");
				return 0;
			}
		}
		s++;
	}
	qsort(d, s, sizeof(d[0]), cmp);
	if (d[n - 1] != 0)
	{
		ll s1 = 0;
		for (ll i = 0; i < n; i++)
		{
			b[d[i]]++;
		}
		for (ll i = 1; i < 1001; i++)
		{
			if (b[i] > 0)
			{
				s1++;
			}
		}
		printf("%lld", s1);
	}
	else
	{
		printf("-1");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,ans,l[1005],r[1005];
char mp[1005][1005];
set<int>s;
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
    for(int i=1;i<=n;i++)
    {
        for(int i0=1;i0<=m;i0++)
        {
            if(mp[i][i0]=='G')l[i]=i0;
            if(mp[i][i0]=='S')r[i]=i0;
        }
        s.insert(r[i]-l[i]);
    }
    if(*s.begin()<0)ans=-1;
    else ans=s.size();
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e3+10;
const int mod=1e9+7;
typedef long long ll;
//typedef __int128 LL;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3f;
char a[MAXN][MAXN];
set<int>se;
int main()
{

	int n,m;
	scanf("%d%d",&n,&m);
	int b,c;
	int flag=0;
	for(int i=0;i<n;i++)scanf("%s",a[i]);
	for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='G')b=j;
            else if(a[i][j]=='S')c=j;
        }
        if(c-b<0)flag=1;
        else se.insert(c-b-1);
    }
    if(flag==0)printf("%d\n",se.size());
    else printf("-1\n");

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<math.h>
#include<set>
#include<map>
#include<unordered_map>
#include<iomanip>
#include<queue>
#include<deque>
#include<bitset>
#include<stack>
#include<stdlib.h>
#include<list>
#include<fstream>
#define Flash ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
double EPS = 1e-3;
long long gcd(long long a, long long b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
int lcm(int a, int b) {
	if (b > a) swap(a, b);
	return (a*b) / gcd(a, b);
}
int power(int b, int p) {
	if (p == 0)    return 1;
	int sq = power(b, p / 2);
	sq = sq*sq;
	if (p % 2 == 1)
		sq *= b;
	return sq;
}

const int MAX = 2e5;
const int oo = 1e7;
int xDir[] = { 0 , 0  ,1 ,-1 };
int yDir[] = { 1 , -1 ,0 , 0 };
int dcomp(double a, double b) {
	if (fabs(a - b) <= EPS)    return 0;
	return ((a > b) ? 1 : -1);
}

int main() {
	Flash
		int n, m;		cin >> n >> m;
	vector<pair<int, int>>vec(n);
	string str;
	for (int i = 0; i < n; i++) {
		cin >> str;
		for (int j = 0; j < m; j++)
			if (str[j] == 'G')
				vec[i].first = j;
			else if (str[j] == 'S')
				vec[i].second = j;
	}
	int res = -1;
	bool ok = 1;
	for (int i = 0; i < n; i++) {
		if (vec[i].first > vec[i].second) {
			ok = 0;
			break;
		}
		res = max(res, vec[i].second - vec[i].first);
	}
	if (ok) {
		cout << res -1<< endl;
	}
	else {
		cout << -1 << endl;
	}
	//system("PAUSE");
}


#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<math.h>
#include<set>
#include<map>
#include<unordered_map>
#include<iomanip>
#include<queue>
#include<deque>
#include<bitset>
#include<stack>
#include<stdlib.h>
#include<list>
#include<fstream>
#define Flash ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
double EPS = 1e-3;
long long gcd(long long a, long long b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
int lcm(int a, int b) {
	if (b > a) swap(a, b);
	return (a*b) / gcd(a, b);
}
int power(int b, int p) {
	if (p == 0)    return 1;
	int sq = power(b, p / 2);
	sq = sq*sq;
	if (p % 2 == 1)
		sq *= b;
	return sq;
}

const int MAX = 2e5;
const int oo = 1e7;
int xDir[] = { 0 , 0  ,1 ,-1 };
int yDir[] = { 1 , -1 ,0 , 0 };
int dcomp(double a, double b) {
	if (fabs(a - b) <= EPS)    return 0;
	return ((a > b) ? 1 : -1);
}

int main() {
	Flash
		int n, m;		cin >> n >> m;
	vector<pair<int, int>>vec(n);
	string str;
	for (int i = 0; i < n; i++) {
		cin >> str;
		for (int j = 0; j < m; j++)
			if (str[j] == 'G')
				vec[i].first = j;
			else if (str[j] == 'S')
				vec[i].second = j;
	}
	int res = -1;
	bool ok = 1;
	set<int>st;
	for (int i = 0; i < n; i++) {
		if (vec[i].first > vec[i].second) {
			ok = 0;
			break;
		}
		st.insert(vec[i].first);
	}
	if (ok) {
		cout << st.size() << endl;
	}
	else {
		cout << -1 << endl;
	}
}


#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<math.h>
#include<set>
#include<map>
#include<unordered_map>
#include<iomanip>
#include<queue>
#include<deque>
#include<bitset>
#include<stack>
#include<stdlib.h>
#include<list>
#include<fstream>
#define Flash ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
double EPS = 1e-3;
long long gcd(long long a, long long b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
int lcm(int a, int b) {
	if (b > a) swap(a, b);
	return (a*b) / gcd(a, b);
}
int power(int b, int p) {
	if (p == 0)    return 1;
	int sq = power(b, p / 2);
	sq = sq*sq;
	if (p % 2 == 1)
		sq *= b;
	return sq;
}

const int MAX = 2e5;
const int oo = 1e7;
int xDir[] = { 0 , 0  ,1 ,-1 };
int yDir[] = { 1 , -1 ,0 , 0 };
int dcomp(double a, double b) {
	if (fabs(a - b) <= EPS)    return 0;
	return ((a > b) ? 1 : -1);
}

int main() {
	Flash
		int n, m;		cin >> n >> m;
	vector<pair<int, int>>vec(n);
	string str;
	for (int i = 0; i < n; i++) {
		cin >> str;
		for (int j = 0; j < m; j++)
			if (str[j] == 'G')
				vec[i].first = j;
			else if (str[j] == 'S')
				vec[i].second = j;
	}
	int res = -1;
	bool ok = 1;
	set<int>st;
	for (int i = 0; i < n; i++) {
		if (vec[i].first > vec[i].second) {
			ok = 0;
			break;
		}
		st.insert( vec[i].second - vec[i].first);
	}
	if (ok) {
		cout << st.size() << endl;
	}
	else {
		cout << -1 << endl;
	}

}


#include <iostream>
#include <math.h>
#include<vector>
#include<map>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<string.h>
#include<stack>
#include <iomanip>
#include<queue>
#define Flash ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int xDir[8]={0 , 0,-1 ,1 ,1 ,-1 , -1 ,1 };
int yDir[8]={1 ,-1, 0 ,0 ,1 ,-1 , 1 , -1};

int EPS = 1e-10;
int dComp(double d1 , double d2){
    if(abs(d1 - d2) <= EPS)
        return 0;
    if(d1 > d2)
        return 1;
    else
        return -1;

}

int main()
{
    Flash
    int n,m;        cin>>n>>m;
    string str;
    bool valid = 1;
    int dist = -1;
    for( int i = 0; i < n ; i ++ ){
        cin>>str;
        int p1 , p2;
        for( int j = 0 ; j < m ; j ++ ){
            if(str[j] == 'G')
                p1 = j;
            else if(str[j] == 'S')
                p2 = j;
        }
        if(p1 > p2)
            valid = 0;
        dist = max(dist , p2 - p1);
    }
    if(valid)
        cout<<dist-1<<endl;
    else
        cout<<-1<<endl;
}


#include <iostream>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

map<int,int>moves;
//vector<long long>vec;
//vector<long long>vec2;
//unsigned long long ans = 0;
//void nextlucky(long long a)
//{
//	string num = to_string(a);
//	bool shift = false;
//	bool first = false;
//	bool nextlucky = false;
//	int indx = 0;
//	for(int i = 0 ; i < num.size();i++)
//	{
//		if(num[i] != '4' && num[i] !='7')
//		{
//
//			if(num[i] < '4')
//			{
//				num[i] = '4';
//				shift = true;
//				indx = i;
//
//			}
//			else if(num[i] <'7')
//			{
//				num[i] = '7';
//				shift = true;
//				indx = i;
//			}
//			else
//			{
//				if(i == 0)
//				{
//					first = true;
//					for(int i = 0 ; i < num.size();i++)
//						num[i] = '4';
//					num+='4';
//					break;
//				}
//				else
//				{
//					nextlucky = true;
//					indx = i;
//					break;
//				}
//			}
//		}
//		if(shift == true || first == true || nextlucky == true)
//			break;
//	}
//	if(shift == true)
//	{
//		for(int i = indx+1 ; i < num.size();i++)
//		{
//			num[i] = '4';
//		}
//	}
//	else if(nextlucky == true)
//	{
//		bool shift2 = false;
//		for(int i = indx-1 ; i>=0;i--)
//		{
//			if(num[i] < '7')
//				{
//					shift2 = true;
//					num[i] = '7';
//					for(int  j = i+1; j<num.size();j++)
//						num[j] = '4';
//					break;
//				}
//
//		}
//		if(shift2 == false)
//		{
//			for(int i = 0 ; i < num.size();i++)
//				num[i] = '4';
//			num+='4';
//		}
//	}
//
//	ans+=stoll(num);
////	cout << num<<endl;
//}

int main()
{
	int n , m ;
	cin >> n >> m;
	char arr[n][m];
	int monster = -100;
	int candy = -1100;
	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{
			cin >> arr[i][j];
		}
	}

	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{

			if(arr[i][j] == 'G')
					monster = j+1;

			else if(arr[i][j] == 'S')
				candy = j+1;

		}
				if(monster > 0  &&  candy > 0  )
				{
					if(monster < candy )
					{
						int diff = candy - monster;
						moves[diff]++;

					}
				}

	}
	if(moves.size() == 0)
		cout<<"-1";
	else
		cout << moves.size();
}


#include <iostream>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

map<int,int>moves;
//vector<long long>vec;
//vector<long long>vec2;
//unsigned long long ans = 0;
//void nextlucky(long long a)
//{
//	string num = to_string(a);
//	bool shift = false;
//	bool first = false;
//	bool nextlucky = false;
//	int indx = 0;
//	for(int i = 0 ; i < num.size();i++)
//	{
//		if(num[i] != '4' && num[i] !='7')
//		{
//
//			if(num[i] < '4')
//			{
//				num[i] = '4';
//				shift = true;
//				indx = i;
//
//			}
//			else if(num[i] <'7')
//			{
//				num[i] = '7';
//				shift = true;
//				indx = i;
//			}
//			else
//			{
//				if(i == 0)
//				{
//					first = true;
//					for(int i = 0 ; i < num.size();i++)
//						num[i] = '4';
//					num+='4';
//					break;
//				}
//				else
//				{
//					nextlucky = true;
//					indx = i;
//					break;
//				}
//			}
//		}
//		if(shift == true || first == true || nextlucky == true)
//			break;
//	}
//	if(shift == true)
//	{
//		for(int i = indx+1 ; i < num.size();i++)
//		{
//			num[i] = '4';
//		}
//	}
//	else if(nextlucky == true)
//	{
//		bool shift2 = false;
//		for(int i = indx-1 ; i>=0;i--)
//		{
//			if(num[i] < '7')
//				{
//					shift2 = true;
//					num[i] = '7';
//					for(int  j = i+1; j<num.size();j++)
//						num[j] = '4';
//					break;
//				}
//
//		}
//		if(shift2 == false)
//		{
//			for(int i = 0 ; i < num.size();i++)
//				num[i] = '4';
//			num+='4';
//		}
//	}
//
//	ans+=stoll(num);
////	cout << num<<endl;
//}

int main()
{
	int n , m ;
	cin >> n >> m;
	char arr[n][m];
	int monster = -100;
	int candy = -1100;
	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{
			cin >> arr[i][j];
		}
	}

	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{

			if(arr[i][j] == 'G')
					monster = j+1;

			else if(arr[i][j] == 'S')
				candy = j+1;

		}
				if(monster > 0  &&  candy > 0  )
				{
					if(monster < candy )
					{
						int diff = candy - monster;
						moves[diff]++;

					}
					if(monster > candy )
					{
						cout << "-1";
						return 0;
					}

				}

	}
	if(moves.size() == 0)
		cout<<"-1";
	else
		cout << moves.size();
}


#include <iostream>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

map<int,int>moves;
//vector<long long>vec;
//vector<long long>vec2;
//unsigned long long ans = 0;
//void nextlucky(long long a)
//{
//	string num = to_string(a);
//	bool shift = false;
//	bool first = false;
//	bool nextlucky = false;
//	int indx = 0;
//	for(int i = 0 ; i < num.size();i++)
//	{
//		if(num[i] != '4' && num[i] !='7')
//		{
//
//			if(num[i] < '4')
//			{
//				num[i] = '4';
//				shift = true;
//				indx = i;
//
//			}
//			else if(num[i] <'7')
//			{
//				num[i] = '7';
//				shift = true;
//				indx = i;
//			}
//			else
//			{
//				if(i == 0)
//				{
//					first = true;
//					for(int i = 0 ; i < num.size();i++)
//						num[i] = '4';
//					num+='4';
//					break;
//				}
//				else
//				{
//					nextlucky = true;
//					indx = i;
//					break;
//				}
//			}
//		}
//		if(shift == true || first == true || nextlucky == true)
//			break;
//	}
//	if(shift == true)
//	{
//		for(int i = indx+1 ; i < num.size();i++)
//		{
//			num[i] = '4';
//		}
//	}
//	else if(nextlucky == true)
//	{
//		bool shift2 = false;
//		for(int i = indx-1 ; i>=0;i--)
//		{
//			if(num[i] < '7')
//				{
//					shift2 = true;
//					num[i] = '7';
//					for(int  j = i+1; j<num.size();j++)
//						num[j] = '4';
//					break;
//				}
//
//		}
//		if(shift2 == false)
//		{
//			for(int i = 0 ; i < num.size();i++)
//				num[i] = '4';
//			num+='4';
//		}
//	}
//
//	ans+=stoll(num);
////	cout << num<<endl;
//}

int main()
{
	int n , m ;
	cin >> n >> m;
	char arr[100][100];
	int monster = -100;
	int candy = -1100;
	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{
			cin >> arr[i][j];
		}
	}

	for(int i = 0 ; i < n ; i++)
	{
		for(int j = 0 ; j < m ; j++)
		{

			if(arr[i][j] == 'G')
					monster = j+1;

			else if(arr[i][j] == 'S')
				candy = j+1;

		}
				if(monster > 0  &&  candy > 0  )
				{
					if(monster < candy )
					{
						int diff = candy - monster;
						moves[diff]++;

					}
				}

	}
	if(moves.size() == 0)
		cout<<"-1";
	else
		cout << moves.size();
}


#include<bits/stdc++.h>
#include<string>
using namespace std;

char grid[1000][1000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    int x = INT_MIN;
    bool flag = false;

    set<int> s;

    for(int i = 0; i < n; i++)
    {
        int d = -1, c = -1;
        for(int j = 0; j < m; j++)
        {
            cin >> grid[i][j];
            if(grid[i][j] == 'S')
            {
                c = j;
            }
            if(grid[i][j] == 'G')
            {
                d = j;
            }
        }
        if(c < d && !flag)
        {
            flag = true;
            cout << -1 << endl;
        }
        s.insert(abs(d-c));
    }

    if(flag)
    {

    }
    else
    {
        cout << s.size() << endl;
    }
}


#include<bits/stdc++.h>
#include<string>
using namespace std;

char grid[1000][1000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    int x = INT_MAX;

    for(int i = 0; i < n; i++)
    {
        int d = -1, c = -1;
        for(int j = 0; j < m; j++)
        {
            cin >> grid[i][j];
            if(grid[i][j] == 'S')
            {
                c = j;
            }
            if(grid[i][j] == 'G')
            {
                d = j;
            }
        }
        if(c < d)
        {
            cout << -1 << endl;
            return 0;
        }
        x = min(x, abs(d - c) );
    }

    cout << x << endl;
}


#include<bits/stdc++.h>
#include<string>
using namespace std;

char grid[1000][1000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    int x = INT_MIN;

    bool flag = false;

    for(int i = 0; i < n; i++)
    {
        int d = -1, c = -1;
        for(int j = 0; j < m; j++)
        {
            cin >> grid[i][j];
            if(grid[i][j] == 'S')
            {
                c = j;
            }
            if(grid[i][j] == 'G')
            {
                d = j;
            }
        }
        if(c < d)
        {
            flag = true;
            cout << -1 << endl;
        }
        x = max(x, abs(d - c) );
    }

    if(flag)
    {

    }
    else
    {
        cout << x - 1 << endl;
    }
}


#include<bits/stdc++.h>
#include<string>
using namespace std;

char grid[1000][1000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;
    int x = INT_MIN;
    bool flag = false;

    set<int> s;

    for(int i = 0; i < n; i++)
    {
        int d = -1, c = -1;
        for(int j = 0; j < m; j++)
        {
            cin >> grid[i][j];
            if(grid[i][j] == 'S')
            {
                c = j;
            }
            if(grid[i][j] == 'G')
            {
                d = j;
            }
        }
        if(c < d)
        {
            flag = true;
            cout << -1 << endl;
        }
        s.insert(abs(d-c));
    }

    if(flag)
    {

    }
    else
    {
        cout << s.size() << endl;
    }
}


#include "iostream"
#include "vector"
#include "set"

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    set< int> st;

    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        int d, c;
        for (int j = 0; j < m; ++j) {
            if (s[j] == 'S') c = j;
            else if (s[j] == 'G') d = j;
        }
        if (c < d) return !printf("-1");
        st.emplace(c - d);
    }
    printf("%d\n", (int) st.size());
}


#include "iostream"
#include "vector"

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    int res = 0;
    vector< int> x(m + 1, 0);

    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        int d, c;
        for (int j = 0; j < m; ++j) {
            if (s[j] == 'S') c = j;
            else if (s[j] == 'G') d = j;
        }
        if (c < d) return !printf("-1");
        if (x[c - d] == 0) res ++;
        x[c - d] = 1;
    }
    printf("%d\n", res);
}


#include "iostream"
#include "vector"
#include "set"

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    vector< string> s(n);
    for (int i = 0; i < n; ++i) cin >> s[i];
    set< int> st;

    for (int i = 0; i < n; ++i) {
        int d, c;
        for (int j = 0; j < m; ++j) {
            if (s[i][j] == 'S') c = j;
            else if (s[i][j] == 'G') d = j;
        }
        if (c < d) return !printf("-1");
        st.emplace(c - d);
    }
    printf("%d\n", (int) st.size());
}


#include "iostream"
#include "vector"
#include "set"

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    vector< string> s(n);
    for (int i = 0; i < n; ++i) cin >> s[i];
    vector< int> d(n), c(n);
    set< int> st;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (s[i][j] == 'G') d[i] = j;
            if (s[i][j] == 'S') c[i] = j;
        }
        if (c[i] < d[i]) return !printf("-1");
        st.insert(c[i] - d[i]);
    }

    printf("%d\n", (int) st.size());
}


#include "iostream"
#include "vector"

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    vector< string> s(n);
    for (int i = 0; i < n; ++i) cin >> s[i];
    vector< int> d(n), c(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (s[i][j] == 'G') d[i] = j;
            if (s[i][j] == 'S') c[i] = j;
        }
    }

    int res = 0;
    for (int i = 0; i < n; ++i) {
        if (c[i] < d[i]) return !printf("-1");
        res = max(res, c[i] - d[i]);
    }
    printf("%d\n", res - 1);
}


#include "iostream"

using namespace std;

int x[1005];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    int res = 0;

    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        int d, c;
        for (int j = 0; j < m; ++j) {
            if (s[j] == 'S') c = j;
            else if (s[j] == 'G') d = j;
        }
        if (c < d) return !printf("-1");
        if (x[c - d] == 0) res ++;
        x[c - d] = 1;
    }
    printf("%d\n", res);
}


#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
#include <cmath>
#include <set>
using namespace std;

int main()
{
    int n , m , cnt = 0 , f = 0;
    cin >> n >> m;
    set <int> st;
    char x;
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < m ; j++)
        {
            cin >> x;
            if(x == 'S' && f == 0)
            {
                cout << -1 ;
                return 0;
            }

           if (x == 'G')
            {
                f = 1;
            }
           if(f == 1 )
           {
                if(x !='S')
                {
                    cnt++;
                }

           }}
           st.insert(cnt);
            cnt = 0;
            f = 0;
        }

cout << st.size();

    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
#include <cmath>
#include <set>
using namespace std;

int main()
{
    int n , m , cnt = 0 , f = 0 , mn = 1001;
    cin >> n >> m;
    char x;
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < m ; j++)
        {
           cin >> x;
           if (x == 'G')
            f = 1;
           if(f == 1)
           {
                if(x=='S' || j == m-1)
                    break;
                cnt++;
           }}

            if (mn > cnt && cnt != 0)
                mn = cnt;
            cnt = 0;
            f = 0;
        }

if(mn != 1001)
    cout << mn;
else
        cout << -1;
    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
#include <cmath>
#include <set>
using namespace std;

int main()
{
    int n , m , cnt = 0 , f = 0 , t = 1;
    cin >> n >> m;
    set <int> st;
    char x;
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < m ; j++)
        {
            cin >> x;
            if(x == 'S' && f == 0)
            {
                cout << -1 ;
                return 0;
            }

           if (x == 'G')
            {
                f = 1;
            }
           if(f == 1 )
           {
                if(x !='S' && t != 0)
                {
                    cnt++;
                }

           }
           if(x == 'S')
            t = 0;
           }
           st.insert(cnt);
           cout << cnt <<endl;
            cnt = 0;
            f = 0;
           t = 1;
        }

cout << st.size();

    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
#include <cmath>
#include <set>
using namespace std;

int main()
{
    int n , m , cnt = 0 , f = 0 , t = 1;
    cin >> n >> m;
    set <int> st;
    char x;
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < m ; j++)
        {
            cin >> x;
            if(x == 'S' && f == 0)
            {
                cout << -1 ;
                return 0;
            }

           if (x == 'G')
            {
                f = 1;
            }
           if(f == 1 )
           {
                if(x !='S' && t != 0)
                {
                    cnt++;
                }

           }
           if(x == 'S')
            t = 0;
           }
           st.insert(cnt);
           //cout << cnt <<endl;
            cnt = 0;
            f = 0;
           t = 1;
        }

cout << st.size();

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
char a[1001][1002];
int n,m,i,j,mx,nr,ok1,t,aux[1001],pg,ps,v[1002],ok;
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            //cout<<a[i][j]<<" ";
        }
        //cout<<endl;
    }
    ok=0;
    for(i=1;i<=n;i++)
    {
        ps=0;
        pg=0;
        for(j=1;j<=m;j++)
        {
            if(a[i][j]=='S')
                ps=j;
            if(a[i][j]=='G')
                pg=j;
        }
            if(ps<pg)
                ok=1;
            else
                v[ps-pg]=1;

    }
    if(ok==1)
    {
        cout<<"-1";
    }
    else
    {
       nr=0;
       for(i=1;i<=m;i++)
       {
           nr=nr+v[i];
       }
       cout<<nr;
    }

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
char a[1001][1002];
int n,m,i,j,mx,nr,ok1,t,aux[1001],pg,ps,v[1002],ok;
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>a[i][j];
            //cout<<a[i][j]<<" ";
        }
        //cout<<endl;
    }
    ok=0;
    for(i=1;i<=n;i++)
    {
        ps=0;
        pg=0;
        for(j=1;j<=m;j++)
        {
            if(a[i][j]=='S')
                ps=j;
            if(a[i][j]=='G')
                pg=j;
        }
            if(ps<pg)
                ok=1;
            else
                v[ps-pg]=1;

    }
    if(ok==1)
    {
        cout<<"-1";
    }
    else
    {
       nr=0;
       for(i=1;i<=n;i++)
       {
           nr=nr+v[i];
       }
       cout<<nr;
    }

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n]={0},cond[n]={0},posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int minD=10000,condition;
	for(int i=0;i<n;i++)
	{
	    if(min[i]<minD)
	    {
	        minD=min[i];
	        condition=cond[i];
	    }
	}
	if(condition==1)
	    cout<<"-1";
	else
	    cout<<minD;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n]={0},cond[n]={0},posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int ans=0;
	sort(min,min+n);
	if(n==1)
	{
	    if(cond[0]==1)
	        cout<<"-1";
	    else
	        cout<<"1";
	    return 0;
	}
	for(int i=0;i<n-1;i++)
	{
	    ans+=min[i+1]-min[i];
	    if(cond[i]==1)
	    {
	        cout<<"-1";
	        return 0;
	    }
	}
    cout<<ans+1;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n]={0},cond[n]={0},posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int ans=0;
	//sort(min,min+n);
	if(n==1)
	{
	    if(cond[0]==1)
	        cout<<"-1";
	    else
	        cout<<"1";
	    return 0;
	}
	int count[m-1]={0};
	for(int i=0;i<m-1;i++)
	    count[i]=0;
	for(int i=0;i<n;i++)
	{
	    count[min[i]-1]++;
	    if(cond[i]==1)
	    {
	        cout<<"-1";
	        return 0;
	    }
	}
	/*for(int i=0;i<m;i++)
	    cout<<count[i];
	cout<<endl;*/
	for(int i=0;i<m;i++)
	{
	    if(count[i]!=1 && count[i]!=0)
	    {
	        if(count[i]%2==0)
	            ans+=i+1;
	    }
	}
    cout<<ans;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n]={0},cond[n]={0},posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int ans=0;
	sort(min,min+n);
	for(int i=0;i<n-1;i++)
	{
	    ans+=min[i+1]-min[i];
	    if(cond[i]==1)
	    {
	        cout<<"-1";
	        return 0;
	    }
	}
    cout<<ans+1;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n],cond[n],posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int ans=0;
	//sort(min,min+n);
	if(n==1)
	{
	    if(cond[0]==1)
	        cout<<"-1";
	    else
	        cout<<"1";
	    return 0;
	}
	int count[m-1];
	for(int i=0;i<m-1;i++)
	    count[i]=0;
	for(int i=0;i<n;i++)
	{
	    count[min[i]-1]++;
	    if(cond[i]==1)
	    {
	        cout<<"-1";
	        return 0;
	    }
	}
	/*for(int i=0;i<m;i++)
	    cout<<count[i];
	cout<<endl;*/
	//sort(count,count+m-1);
	for(int i=0;i<m-1;i++)
	{
	    if(count[i]!=1 && count[i]!=0)
	    {
	        if(count[i]%2==0)
	            ans=i+1;
	    }
	}
    cout<<ans;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n],cond[n],posG,posS,count[m-1]={0};
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	        cout<<"-1";
	        return 0;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        count[min[i]-1]=1;
	        cond[i]=0;
	    }
	}
	int ans=0;
	if(n==1)
	{
	    if(cond[0]==1)
	        cout<<"-1";
	    else
	        cout<<"1";
	    return 0;
	}
	for(int i=0;i<m-1;i++)
	{
	    if(count[i]==1)
	        ans++;
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	int n,m;
	string str;
	cin>>n>>m;
	int min[n],cond[n],posG,posS;
	for(int i=0;i<n;i++)
	{
	    cin>>str;
	    for(int j=0;j<m;j++)
	    {
	        if(str[j]=='G')
	        {
	            posG=j;
	        }
	        if(str[j]=='S')
	        {
	            posS=j;
	        }
	    }
	    if(posG>posS)
	    {
	        min[i]=n-1-posG;
	        cond[i]=1;
	    }
	    else
	    {
	        min[i]=posS-posG;
	        cond[i]=0;
	    }
	}
	int ans=0;
	//sort(min,min+n);
	if(n==1)
	{
	    if(cond[0]==1)
	        cout<<"-1";
	    else
	        cout<<"1";
	    return 0;
	}
	int count[m-1]={0},temp[m-1]={0};
	//for(int i=0;i<m-1;i++)
	  //  count[i]=0;
	for(int i=0;i<n;i++)
	{
	    count[min[i]-1]++;
	    temp[min[i]-1]++;
	    if(cond[i]==1)
	    {
	        cout<<"-1";
	        return 0;
	    }
	}
	/*for(int i=0;i<m;i++)
	    cout<<count[i];
	cout<<endl;*/
	sort(count,count+m-1);
	int zero=0;
	for(int i=0;i<m-1;i++)
	{
	    if(count[i]==0)
	        zero++;
	    if(count[i]==count[m-2])
	    {
	        cout<<i+1-zero;
	        return 0;
	    }
	}
    //cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MIN_V -1e9-9
#define MAX_V  1e9+9
#define all(c) c.begin() , c.end()
#define vi vector<int>
#define write_to_file() freopen("output.txt","w",stdout);
#define read_from_file() freopen("input.txt","r",stdin);
const int AKA = 1e6+6;
typedef  long long lld;
int arr[AKA];
int arr1[AKA];
int SD(){int i ;scanf("%d", &i);return i;}
lld SLL(){lld i ;scanf("%lld", &i);return i;}
#define watchSTL(container) {\
                                for(auto _E:container)cout<<_E<<" ";\
                                printf("\n");\
                            }
void watchset_e(set<int>SET, int N)
{
    set<int>::iterator ITERATOR=SET.begin();
    advance(ITERATOR,N);
    printf("Element in %dth = %d \n",N,*ITERATOR);
}

set<int>::iterator it ;
int main()
{
    int x = SD() , y = SD() , a , b , sum;
    set<int> s ;
    string c ;
    for(int i=0;i<x;i++)
    {
      b = y , a= 0;
       cin >> c ;
       for(int j=0;j<y;j++)
       {
           if(c[j] == 'G')
               a = j ;
           else if(c[j] == 'S')
             b = j ;
       }
       sum = b - a ;
       if(sum >= 0)
        s.insert(sum);
    }
    if(s.size() > 0)
      cout << s.size() ;
      else
        cout << "-1" ;

     return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define MIN_V -1e9-9
#define MAX_V  1e9+9
#define all(c) c.begin() , c.end()
#define vi vector<int>
#define write_to_file() freopen("output.txt","w",stdout);
#define read_from_file() freopen("input.txt","r",stdin);
const int AKA = 1e6+6;
typedef  long long lld;
int arr[AKA];
int arr1[AKA];
int SD(){int i ;scanf("%d", &i);return i;}
lld SLL(){lld i ;scanf("%lld", &i);return i;}
#define watchSTL(container) {\
                                for(auto _E:container)cout<<_E<<" ";\
                                printf("\n");\
                            }
void watchset_e(set<int>SET, int N)
{
    set<int>::iterator ITERATOR=SET.begin();
    advance(ITERATOR,N);
    printf("Element in %dth = %d \n",N,*ITERATOR);
}

set<int>::iterator it ;
int main()
{
    int x = SD() , y = SD() , a, b , sum , m =0;
    set<int>s ;
    string c ;
    for(int i=0;i<x;i++)
    {
       cin >> c ;
       for(int j=0;j<y;j++)
       {
           if(c[j] == 'G')
               a = j ;
           else if(c[j] == 'S')
               b = j ;
       }
       sum = b - a ;
       if(sum >= 0)
        s.insert(sum);
        else
            m = 1;
    }
    if(m == 0)
      cout << s.size() ;
      else
        cout << "-1" ;

     return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define MIN_V -1e9-9
#define MAX_V  1e9+9
#define all(c) c.begin() , c.end()
#define vi vector<int>
#define write_to_file() freopen("output.txt","w",stdout);
#define read_from_file() freopen("input.txt","r",stdin);
const int AKA = 1e6+6;
typedef  long long lld;
int arr[AKA];
int arr1[AKA];
int SD(){int i ;scanf("%d", &i);return i;}
lld SLL(){lld i ;scanf("%lld", &i);return i;}
#define watchSTL(container) {\
                                for(auto _E:container)cout<<_E<<" ";\
                                printf("\n");\
                            }
void watchset_e(set<int>SET, int N)
{
    set<int>::iterator ITERATOR=SET.begin();
    advance(ITERATOR,N);
    printf("Element in %dth = %d \n",N,*ITERATOR);
}

set<int>::iterator it ;
int main()
{
    int x = SD() , y = SD() , a , b , sum;
    set<int> s ;
    string c ;
    for(int i=0;i<x;i++)
    {
       cin >> c ;
       for(int j=0;j<y;j++)
       {
           if(c[j] == 'G')
               a = j ;
           else if(c[j] == 'S')
             b = j ;
       }
       sum = b - a ;
       if(sum >= 0)
        s.insert(sum);
    }
    if(s.size() > 0)
      cout << s.size() ;
      else
        cout << "-1" ;

     return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define MIN_V -1e9-9
#define MAX_V  1e9+9
#define all(c) c.begin() , c.end()
#define vi vector<int>
#define write_to_file() freopen("output.txt","w",stdout);
#define read_from_file() freopen("input.txt","r",stdin);
const int AKA = 1e6+6;
typedef  long long lld;
int arr[AKA];
int arr1[AKA];
int SD(){int i ;scanf("%d", &i);return i;}
lld SLL(){lld i ;scanf("%lld", &i);return i;}
#define watchSTL(container) {\
                                for(auto _E:container)cout<<_E<<" ";\
                                printf("\n");\
                            }
void watchset_e(set<int>SET, int N)
{
    set<int>::iterator ITERATOR=SET.begin();
    advance(ITERATOR,N);
    printf("Element in %dth = %d \n",N,*ITERATOR);
}

set<int>::iterator it ;
int main()
{
    int x = SD() , y = SD() , a , b , h=-1 , h1 =0, sum , MAx = MAX_V;
    string s ;
    for(int i=0;i<x;i++)
    {
        cin >> s ;
        for(int j=0;j<y;j++)
        {
            if(s[j] == 'G')
                a = j ;
            else if(s[j] == 'S')
                b = j ;
        }
        sum = b - a ;
        if(sum >= 0)
        {
            MAx = min(MAx , sum);
        }else if (sum < 0)
        {
            h1 = -1 ;
        }
    }
    if(h1 == -1 && MAx == MAX_V)
        cout << "-1" ;
    else
    {
        cout << MAx ;
    }

     return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m ;
    cin>>n>>m;
    string temp;
    set <int >res ;
    for (int i = 0 ;i <n ;i++)
    {
        int start =0,end=0 ;
        cin>>temp;
        for (int  j =0 ;j <m ;j++)
        {
            if (temp[j]=='G' ) start=j;
            else if (temp[j]=='S') end =j;
        }
        if ( end >start ) res.insert(end-start);
        else if (end<start && (m-1 !=start)) res.insert(m-1-start );
    }

    (res.size()==0)? cout<<-1: cout<<res.size() ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m ;
    cin>>n>>m;
    char s[n][m];
    for (int i = 0; i <n ;i++)
        for (int j =0 ;j<m;  j++)
            cin>>s[i][j];

    set <int >res ;
    for (int i = 0 ;i <n ;i++)
    {
        int start =0,end=0 ;
        for (int  j =0 ;j <m ;j++)
        {
            if (s[i][j]=='G' ) start=j;
            else if (s[i][j]=='S') end =j;
        }
        if ( end >start && !(res.count(end-start))) res.insert(end-start);
        else if (end<start) res.insert(m-start && !(res.count(m-start)));
    }

    (res.size()==0)? cout<<-1: cout<<res.size() ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m ;
    cin>>n>>m;
    char s[n][m];
    for (int i = 0; i <n ;i++)
        for (int j =0 ;j<m;  j++)
            cin>>s[i][j];

    set <int >res;int start =0 ,end=0;
    for (int i = 0 ;i <n ;i++)
    {
        for (int  j =0 ;j <m ;j++)
        {
            if (s[i][j]=='G') start = j;
            else if (s[i][j]=='S') end =j;
        }
        if (end -start  >0) res.insert(end-start);
    }

    (res.size()==0)? cout<<-1: cout<<res.size() ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m ;
    cin>>n>>m;
    char s[n][m];
    for (int i = 0; i <n ;i++)
        for (int j =0 ;j<m;  j++)
            cin>>s[i][j];

    set <int >res ;
    for (int i = 0 ;i <n ;i++)
    {
        int start =0,end=0 ;
        for (int  j =0 ;j <m ;j++)
        {
            if (s[i][j]=='G' ) start=j;
            else if (s[i][j]=='S') end =j;
        }
        if ( end >start && !(res.count(end-start))) res.insert(end-start);
        else if (end<start && m-1 !=start) res.insert(m-1-start );
    }

    (res.size()==0)? cout<<-1: cout<<res.size() ;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m ;
    cin>>n>>m;
    string temp;
    set <int >res ;
    for (int i = 0 ;i <n ;i++)
    {
        int start =0,end=0 ;
        cin>>temp;
        for (int  j =0 ;j <m ;j++)
        {
            if (temp[j]=='G' ) start=j;
            else if (temp[j]=='S') end =j;
        }

         if (end>start ) res.insert(end-start);
         else {
             cout<<-1<<endl;
             return 0;
         }
    }

    (res.size()==0)? cout<<-1: cout<<res.size() ;

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include <iostream>
using namespace std;
typedef long long ll;
char a[1003][1003];
ll d[1003], b[1003], kl = 0;
int cmp(const void* a, const void* b)
{
	return (ll*)b - (ll*)a;
}
char sd[1003];
int as(char p)
{
	if (p == 'S')
	{
		if (sd[kl] == 'G')
		{
			kl--;
		}
	}
	else
	{
		sd[++kl] = p;
	}
	return kl;
}
int main()
{
	ll n, m, s = 0;
	char p;
	scanf("%lld%lld\n", &n, &m);
	for (ll i = 0; i < n; i++)
	{
		for (ll j = 0; j < m; j++)
		{
			scanf("%c", &a[i][j]);
		}
		scanf_s("%c", &p);
	}
	for (ll i = 0; i < n; i++)
	{
		ll x = -1, y = -1, max = 0, g = 0, s6 = 0;
		for (ll j = 0; j < m; j++)
		{
			if (a[i][j] == 'G' && x == -1)
			{
				x = j;
			}
			if (a[i][j] == 'G')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				y = j;
				if (x != -1 && y > x)
				{
					d[s] = j - x;
				}
			}
			if (j == m - 1 && g != 0)
			{
				printf("-1");
				return 0;
			}
		}
		s++;
	}
	qsort(d, s, sizeof(d[0]), cmp);
	if (d[n - 1] != 0)
	{
		ll s1 = 0;
		for (ll i = 0; i < n; i++)
		{
			b[d[i]]++;
		}
		for (ll i = 1; i < 1001; i++)
		{
			if (b[i] > 0)
			{
				s1++;
			}
		}
		printf("%lld", s1);
	}
	else
	{
		printf("-1");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5 + 7;

int n, m;
char s[1006][1006];

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n;i++)
    {

        scanf("%s", s[i]);
    }
    int f = 1;
    map<int, int> mp;
    for (int i = 0; i < n;i++)
    {
        int g = -1, ss = -1;
        for (int j = 0; j < m;j++)
        {
            if(s[i][j] == 'G') g = j;
            if(s[i][j]  == 'S') ss = j;
        }
        if(g > ss)
        {
            f = 0;
            break;
        }
        else
        {
            mp[ss - g] = 1;
        }

    }
    if(f == 0) puts("-1");
    else printf("%d\n", mp.size());
    return 0;
}


#include<windows.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
//#include<bits/stdc++.h>

using namespace std;

long long i, j, k, m, M, n, s, co, ans, f, len, T, r, t;
long long ff[1001], fff = 1;
char a[1001];

int main()
{
	scanf("%lld %lld", &n, &m);
	for (i = 1;i <= n;i++)
	{
		f = 0;
		scanf("%s", a + 1);
		for (j = 1;j <= m;j++)
		{
			if (a[j] == 'G')
				f = j;
			else if (a[j] == 'S')
			{
				if (f == 0)
				{
					fff = 0;
					break;
				}
				else
				{
					ff[j - f] = 1;
					break;
				}
			}
		}
	}

	if (fff == 0)
	{
		printf("-1");
		return 0;
	}

	for (i = 1;i <= m;i++)
		if (ff[i] == 1)
			ans++;

	printf("%lld", ans);

	return 0;
}


//██████╗ ███████╗ ██╗  ██╗
//██╔══██╗██╔════╝██╔██╗██║
//██████╔╝█████╗  ██║██║██║
//██╔══██╗██╔══╝  ██║██║██║
//███████║███████╗██║══██╔╝
//╚══════╝╚══════╝╚═╝  ╚═╝

#include<bits/stdc++.h>
#define ll long long
#define DEBUG
using namespace std;
const int maxn=300010;
const int mod=100007;
const int N = 500 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);

int n,m,cnt,ans,flag;

inline int read()
{
    register int c = getchar(), fg = 1, sum = 0;
    while(c > '9' || c < '0' )
    {
        if(c == '-')fg = -1;
        c = getchar();
    }
    while(c <= '9' && c >= '0')
    {
        sum = sum *10 + c - '0';
        c = getchar();
    }
    return fg * sum;
}

void init()
{
    n=read();
    m=read();
}

void go()
{
    ans=0;
    set<int>s;
    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;
        if(ans<0)continue;
        int a,b;
        for(int j=0;j<m;j++)
        {
            if(str[j]=='S')a=j;
            if(str[j]=='G')b=j;
        }
        if(a<b)ans=-1;
        else s.insert(a-b-1);
    }
    printf("%d\n",s.empty()?-1:s.size());
}
int main()
{
    init();
    go();
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include <iostream>
using namespace std;
typedef long long ll;
char a[1003][1003];
ll d[1003], b[1003], kl = 0;
int cmp(const void* a, const void* b)
{
	return (ll*)b - (ll*)a;
}
char sd[1003];
int as(char p)
{
	if (p == 'S')
	{
		if (sd[kl] == 'G')
		{
			kl--;
		}
	}
	else
	{
		sd[++kl] = p;
	}
	return kl;
}
int main()
{
	ll n, m, s = 0;
	char p;
	scanf("%lld%lld\n", &n, &m);
	for (ll i = 0; i < n; i++)
	{
		for (ll j = 0; j < m; j++)
		{
			scanf("%c", &a[i][j]);
		}
		scanf("%c", &p);
	}
	for (ll i = 0; i < n; i++)
	{
		ll x = -1, y = -1, max = 0, g = 0, s6 = 0;
		for (ll j = 0; j < m; j++)
		{
			if (a[i][j] == 'G' && x == -1)
			{
				x = j;
			}
			if (a[i][j] == 'G')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				g = as(a[i][j]);
			}
			if (a[i][j] == 'S')
			{
				y = j;
				if (x != -1 && y > x)
				{
					d[s] = j - x;
				}
			}
			if (j == m - 1 && g != 0)
			{
				printf("-1");
				return 0;
			}
		}
		s++;
	}
	qsort(d, s, sizeof(d[0]), cmp);
	if (d[n - 1] != 0)
	{
		ll s1 = 0;
		for (ll i = 0; i < n; i++)
		{
			b[d[i]]++;
		}
		for (ll i = 1; i < 1001; i++)
		{
			if (b[i] > 0)
			{
				s1++;
			}
		}
		printf("%lld", s1);
	}
	else
	{
		printf("-1");
	}
	return 0;
}


#include<windows.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
//#include<bits/stdc++.h>

using namespace std;

long long i, j, k, m, M, n, s, co, ans, f, len, T, r, t;
long long ff[1001];
char a[1001];

int main()
{
	scanf("%lld%lld", &n, &m);
	for (i = 1;i <= n;i++)
	{
		scanf("%s", a + 1);
		for (j = 1;j <= m;j++)
		{
			if (a[j] == 'G')
				f = j;
			else if (a[j] == 'S')
			{
				if (f == 0)
				{
					printf("-1");
					return 0;
				}
				else
				{
					ff[j - f] = 1;
					break;
				}
			}
		}
	}
	for (i = 1;i <= m;i++)
		if (ff[i] == 1)
			ans++;

	printf("%lld", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef vector<int> vi;
typedef vector<ll> vll;
#define between(x, a, b) (a<=x && x<=b)
const int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
//bool vis[N];
//vector<int>g[N];
//int a[N];
int n, m, K;
string mp[N];
int res[N];

int main() {
    ios::sync_with_stdio(0);

    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> mp[i];
    }

    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        int f = 0;
        for (int j = m - 1; j >= 0; --j) {
            if (mp[i][j] == 'S') {
                f = j;
            } else if (mp[i][j] == 'G') {
                if (f) {
                    res[f - j]++;
                } else return cout << -1 << endl, 0;
            }
        }
    }

    int ans = 0;

    for (int i = 0; i < m; ++i) {
        if (res[i]) ans++;
    }
    cout << ans << endl;

    return 0;
}