#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int a[22][22],h,w,mi;
using namespace std;
void dfs(int y,int x,int depth){
	if(depth>=mi)return;
	r(i,4){
		int y1=y,x1=x,c=0,cc=1;
		while(1){
			if(i>1){
				if(y1<0||y1>=h){c++;goto P;}
				if(cc&&i==2&&a[y1+1][x1]==1){c++;goto P;}
				if(cc&&i==3&&a[y1-1][x1]==1){c++;goto P;}
				if(y1+1<h&&i==2&&a[y1+1][x1])goto P;
				if(y1-1>=0&&i==3&&a[y1-1][x1])goto P;
			}
			else{
				if(x1<0||x1>=w){c++;goto P;}
				if(cc&&i==0&&a[y1][x1+1]==1){c++;goto P;}
				if(cc&&i==1&&a[y1][x1-1]==1){c++;goto P;}
				if(x1+1<w&&i==0&&a[y1][x1+1])goto P;
				if(x1-1>=0&&i==1&&a[y1][x1-1])goto P;
			}y1+=dy[i];x1+=dx[i];cc=0;
		}
		P:;int y2=y1+dy[i],x2=x1+dx[i];
		if(!c){if(a[y2][x2]==3||mi<depth+1){mi=depth+1;return;}
		else{
			a[y2][x2]=0;
			dfs(y1,x1,depth+1);
			a[y2][x2]=1;
		}}
	}
}
int main(){
	int hh,ww;
	while(cin>>w>>h,w){
		r(i,h)r(j,w){
			cin>>a[i][j];
			if(a[i][j]==2){
				a[i][j]=0;
				hh=i;mi=11;
				ww=j;
			}
		}
		dfs(hh,ww,0);
		if(mi==11)cout<<-1<<endl;
		else cout<<mi<<endl;
	}
}
#define _GLIBCXX_DEBUG //エラーを出すためのおまじない
#include <bits/stdc++.h>
#include <algorithm>
//#include <cstdlib>  // abs() for integer
#include <cmath>    // abs() for float, and fabs()
#include <math.h>
using namespace std;
#define coYES cout << "YES" << endl
#define coNO cout << "NO" << endl
#define coYes cout << "Yes" << endl
#define coNo cout << "No" << endl
#define coyes cout << "yes" << endl
#define cono cout << "no" << endl
//#define cocount cout << count << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define Cinint(n) int n;\
cin >> n;
#define Cinvecint(a, n) vector<int> a(n);\
for (int i = 0; i < (int)(n); i++) {\
  int num;\
  cin >> num;\
  a.at(i) = num;\
}
#define Cinstring(s) string s;\
cin >>s;

  

int main() {
  Cinint(n);
  Cinstring(s);
  int64_t count = 0;
  int64_t Rcount = 0;
  int64_t Gcount = 0;
  int64_t Bcount = 0;
  rep(i, n) {
    if (s.at(i) == 'R') {
      Rcount++;
    }
    else if (s.at(i) == 'G') {
      Gcount++;
    }
    else {
      Bcount++;
    }
  }
  count = Rcount*Gcount*Bcount;
  rep(i, n-2) {
    for (int j = i+1; j < n-1; j++) {
      int sa = j-i;
      int k = j+sa;
      if (k <= n-1) {
        if (s.at(i) == 'R') {
          if (s.at(j) == 'G') {
            if (s.at(k) == 'B') {
              count--;
            }
          }
          else if (s.at(j) == 'B') {
            if (s.at(k) == 'G') {
              count--;
            }
          }
        }
        else if (s.at(i) == 'G') {
          if (s.at(j) == 'B') {
            if (s.at(k) == 'R') {
              count--;
            }
          }
          else if (s.at(j) == 'R') {
            if (s.at(k) == 'B') {
              count--;
            }
          }
        }
        else {
          if (s.at(j) == 'R') {
            if (s.at(k) == 'G') {
              count--;
            }
          }
          else if (s.at(j) == 'G') {
            if (s.at(k) == 'R') {
              count--;
            }
          }
        }
      }
    }
  }
  cout << count << endl;
}
