#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
//#pragma GCC optimize("O3")
using namespace std;
using namespace __gnu_pbds;
auto random_address = [] { char *p = new char; delete p; return uint64_t(p); };
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
string s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    cin>>s;
    s=" "+s;
    if(s[1]!=s[n])
    {
        cout<<1<<'\n';
        return 0;
    }
    int nrlit=0;
    for(int i=1;i<n;i++)
        if(s[i]!=s[1]&&s[i+1]!=s[1])
        {
            cout<<2;
            return 0;
        }
    cout<<-1;

    return 0;
}

/*
========= Plozia =========
	Author:Plozia
	Problem:T2
	Date:2021/3/21
========= Plozia =========
*/

#include <bits/stdc++.h>

typedef long long LL;
const int MAXN = 500 + 10;
const LL P = 998244353;
int n, m, a[MAXN][MAXN];
LL sum;

int read()
{
	int sum = 0, fh = 1; char ch = getchar();
	for (; ch < '0' || ch > '9'; ch = getchar()) fh -= (ch == '-') << 1;
	for (; ch >= '0' && ch <= '9'; ch = getchar()) sum = (sum << 3) + (sum << 1) + (ch ^ 48);
	return (fh == 1) ? sum : -sum;
}

LL ksm(LL a, LL b, LL p)
{
	LL ans = 1 % p;
	for (; b; b >>= 1, a = a * a % p)
		if (b & 1) ans = ans * a % p;
	return ans;
}

int main()
{
	n = read(), m = read();
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
		{
			char ch = getchar();
			while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
			if (ch == 'R') a[i][j] = 1;
			if (ch == 'B') a[i][j] = 2;
		}
	for (int i = 1; i <= n; ++i)
	{
		int Tag = a[i][1];
		int x = i, y = 1;
		bool flag = 0;
		for (;;)
		{
			--x; ++y;
			if (x < 1 || y > m) break ;
			if (Tag == 0 && a[x][y] != 0) {	Tag = a[x][y]; }
			else if (a[x][y] != 0)
			{
				flag = 1;
				if (a[x][y] != Tag) { printf("0\n"); return 0; }
			}
		}
		if (Tag == 0 && flag == 0) ++sum;
	}
	for (int i = 2; i <= m; ++i)
	{
		int Tag = a[n][i];
		int x = n, y = i;
		bool flag = 0;
		for (;;)
		{
			--x; ++y;
			if (x < 1 || y > m) break ;
			if (Tag == 0 && a[x][y] != 0) {	Tag = a[x][y]; }
			else if (a[x][y] != 0)
			{
				flag = 1;
				if (a[x][y] != Tag) { printf("0\n"); return 0; }
			}
		}
		if (Tag == 0 && flag == 0) ++sum;
	}
	printf("%lld\n", ksm(2, sum, P)); return 0;
}
