#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;




bool check(string iStr,string jStr){

   bool retval = false;
   for(int i=0;i<iStr.length();i++){
           //std::cout << iStr[i] << "  " << jStr[i] << endl;

       if(iStr[i] != jStr[i])
         retval = ! retval;
   }

   return retval;
}




int main(void)
{
    int64_t N;
    cin >> N;
    int64_t M;
    cin >> M;
    vector<string> S(N);
    for(int i = 0; i < N; i++){
         cin >> S[i];
    }

    vector<bool> checkVal(N);
    int64_t checkCount = 0;
    for(int i=1;i<N;i++){
        if(check(S[0],S[i])){
            checkVal[i] = true;
            checkCount++;
        }
        else{
            checkVal[i] = false;
        }
    }

    // for(int i=1;i<N;i++){
    // std::cout << "check :" << checkVal[i] << endl;
    // }


    int64_t ans = 0;
    ans += checkCount;
    for(int i=1;i<N;i++){
        if(checkVal[i]== false){
           ans += checkCount;
        }else{
            checkCount--;
            int tmp = N-i-1-checkCount;
            ans += tmp;
    //std::cout << " tmp :" << tmp << " N :" << N << " i :" << i << " checkCount :" << checkCount<< endl;
        }
    //std::cout << ans << endl;
    }


    std::cout << ans << endl;

    return 0;
}




#include <bits/stdc++.h>
#define N 2010
#define pp pair<int,int>
#define ppp pair<pp,int>
#define M 26
#define fi first
#define si second
using namespace std;
int n, m, x, y, u, v, l;
char dp[N][N];
bool vis[N][N];
queue<ppp>q;
vector<pp>g[M];
ppp tmp;
int s[4][2] = {{ -1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool isValid(int x, int y) {
	return (x >= 0 && y >= 0 && x < n && y < m && dp[x][y] != '#');
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout.precision(10);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> dp[i][j];
			if (dp[i][j] == 'S') {
				vis[i][j] = true;
				q.push(make_pair(make_pair(i, j), 0));
			}
			if (dp[i][j] >= 'a' && dp[i][j] <= 'z') {
				g[dp[i][j] - 'a'].push_back(make_pair(i, j));
			}
		}
	while (!q.empty()) {
		tmp = q.front();
		q.pop();
		l = tmp.si;
		u = tmp.fi.fi;
		v = tmp.fi.si;
		if (dp[u][v] == 'G') {
			cout << l << endl;
			return 0;
		}
		if (dp[u][v] >= 'a' && dp[u][v] <= 'z') {
			for (int i = 0; i < g[dp[u][v] - 'a'].size(); i++) {
				x = g[dp[u][v] - 'a'][i].fi;
				y = g[dp[u][v] - 'a'][i].si;
				if (!vis[x][y]) {
					if (dp[x][y] == 'G') {
						cout << l + 1 << endl;
						return 0;
					}
					q.push(make_pair(make_pair(x, y), l + 1));
					vis[x][y] = true;
				}
			}
		}
		for (int i = 0; i < 4; i++) {
			x = s[i][0] + u;
			y = s[i][1] + v;
			if (isValid(x, y) && !vis[x][y]) {
				if (dp[x][y] == 'G') {
					cout << l + 1 << endl;
					return 0;
				}
				q.push(make_pair(make_pair(x, y), l + 1));
				vis[x][y] = true;
			}
		}
	}
	cout << -1 << endl;
	return 0;
}