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

#define INF 1e+9
#define MAX_V 2505

struct edge {
  int to;
  int cost;
};

using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d+V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while ( !que.empty() ) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) { continue; }

    for ( int i = 0; i < G[v].size(); i++ ) {
      edge e = G[v][i];
      if ( d[e.to] > d[v] + e.cost ) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int h, w;
int pos(int i, int j) { return i*w + j; }

int main() {
  cin >> h >> w;
  V = h * w;
  string table[h];
  for ( int i = 0; i < h; i++ ) { cin >> table[i]; }

  int black = 0;
  for ( int i = 0; i < h; i++ ) {
    for ( int j = 0; j < w; j++ ) {
      if ( table[i][j] == '#' ) { black++; continue; }
      if ( i+1 < h )  { G[pos(i,j)].push_back({pos(i+1,j), 1}); }
      if ( i-1 >= 0 ) { G[pos(i,j)].push_back({pos(i-1,j), 1}); }
      if ( j+1 < w )  { G[pos(i,j)].push_back({pos(i,j+1), 1}); }
      if ( j-1 >= 0 ) { G[pos(i,j)].push_back({pos(i,j-1), 1}); }
    }
  }

  dijkstra(pos(0,0));
  int ans = -1;
  if ( d[pos(h-1,w-1)] != INF ) {
    ans = h*w - d[pos(h-1,w-1)] - black - 1;
  }

  cout << ans << endl;
  return 0;
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N;
char s[4002];
int r[4002],g[4002],b[4002];
long long int ans = 0;
int main(){
	scanf("%d",&N);
	scanf("%s",&s[1]);
	//printf("%d\n",N);
	//printf("%s\n",&s[1]);

	for(int i=N-1;i>0;i--){
		r[i] = r[i+1];
		g[i] = g[i+1];
		b[i] = b[i+1];

		if(s[i+1] == 'R')r[i] ++;
		else if (s[i+1] == 'G') g[i]++;
		else b[i]++;
	}
	//for(int i=1;i<=N;i++)printf("%d ",r[i]);
//		printf("\n");
	//for(int i=1;i<=N;i++)printf("%d ",g[i]);
//		printf("\n");
//	for(int i=1;i<=N;i++)printf("%d ",b[i]);
//		printf("\n");

	for(int i=1;i<=N-2;i++){
		for(int j=i+1;j<=N-1 ; j++){
			if(s[i] == s[j])continue;
			int x= j-i;
			
			

			if( (s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')){
				ans+=b[j]; 
				if(x+j <=N && s[x+j] == 'B') ans--;
			}
			else if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')){
				ans+=g[j]; 
				if(x+j <=N && s[x+j] == 'G') ans--;
			}
			else {
				ans+=r[j]; 
				if(x+j<=N && s[x+j] == 'R') ans--;
			}
		//	printf("%d %d %d\n",i,j,ans);
		}
	}
	printf("%lld\n",ans);
	return 0;
}