#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define endl '\n' 
using namespace std;
string p[520];
signed main(){	
	int mx = 0;
	int h,w;
	cin >> h >> w;
	int ans = 1;
	for(int i = 0;i < h;i++){
		cin >> p[i];
	}
	
	bool able = true;
	for(int i = 0;i < w;i++){
		int sti = i;
		int stj = 0;
		bool c = true;
		int cnt = 0;
		map<char,int> mp;
		while(sti >= 0 && stj < h){
			if(p[stj][sti] != '.'){
				if(mp[p[stj][sti]] == 0){
					cnt += 1;
					mp[p[stj][sti]] = 1;
					if(cnt == 2)able = false;
				}
				c = false;
			}
			sti--;
			stj++;
		}
		if(c) ans = (ans * 2ll)% 998244353;
	}
	for(int i = 1;i < h;i++){
		int sti = w - 1;
		int stj = i;
		bool c = true;
		int cnt = 0;
		map<char,int> mp;
		while(sti >= 0 && stj < h){
			if(p[stj][sti] != '.'){
				if(mp[p[stj][sti]] == 0){
					cnt += 1;
					mp[p[stj][sti]] = 1;
					if(cnt == 2)able = false;
				}
				c = false;
			}
			sti--;
			stj++;
		}
		if(c) ans = (ans * 2ll)% 998244353;
	}
	if(able == true)cout << ans << endl;
	else cout << 0 << endl;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define For(i,a,b) for(reg int i=a;i<=b;++i)
#define Down(i,a,b) for(reg int i=a;i>=b;--i) 
#define ull unsigned long long
#define ll long long
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int gcd(int n,int m){return m?gcd(m,n%m):n;}
inline int lcm(int x,int y){return x/gcd(x,y)*y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
namespace yspm{
	inline int read(){
	    int res=0,f=1; char k;
	    while(!isdigit(k=getchar())) if(k=='-') f=-1;
	    while(isdigit(k)) res=res*10+k-'0',k=getchar();
	    return res*f;
    }
    const int N=510;
    char s[N<<1][N<<1];
    int n,m;
	signed main(){
	    n=read(); m=read(); For(i,1,n) scanf("%s",s[i]+1);
	    for(reg int i=1;i<=m+n;++i) for(reg int j=1;j<=m+n;++j) if(s[i][j]==0) s[i][j]='.';
	    int ans=1;
	    if(s[n][m]=='.') ans*=2; 
	    for(reg int S=2;S<n+m;++S){
	        bool ad=1; char dif='.'; 
	        for(reg int j=1;j<S;++j){
	            if(s[S-j][j]!='.'){
	                ad=0;
	                if(dif=='.') dif=s[S-j][j];
	                else if(s[S-j][j]!=dif) puts("0"),exit(0); 
                }
	        } if(ad) ans=ans*2%998244353;
	    } cout<<ans<<endl; 	        
		return 0;
    }
}signed main(){return yspm::main();}
//Use The Time To Enrich This Selfclosing Youth

