#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
char ch[1005][1005] = {{}};
int bfs(int x, int y){
int rtn = 1;
queue<int> q;
q.push(x << 16 + y);
while (!q.empty()){
int a = q.front() >> 16, b = (q.front() << 16) >> 16;
q.pop();
int fnd = 0;
if (!(ch[a-1][b] == 'F'||ch[a+1][b] == 'F'||ch[a][b-1] == 'F'||ch[a][b+1] == 'F'))
fnd = -1;
if (ch[a][b] == 'F')
return -1;
ch[a][b] = 'F';
if (ch[a+1][b] == 'O')
q.push((a + 1) << 16 + b),++fnd,++rtn;
if (ch[a-1][b] == 'O')
if (fnd==1)
return -1;
else
q.push((a - 1) << 16 + b),++fnd,++rtn;
if (ch[a][b+1] == 'O')
if (fnd==1)
return -1;
else
q.push(a << 16 + b + 1),++fnd,++rtn;
if (ch[a][b-1] == 'O')
if (fnd==1)
return -1;
else
q.push(a << 16 + b - 1),++rtn;
}
return rtn;
}
int main(){
int n,m,k;
unsigned long long ans = 1;
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf(" %c",&ch[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (ch[i][j] == 'O'){
int rtn = bfs(i,j);
if (rtn == -1){
cout << 0;
return 0;
}
ans *= k;
for (int l = 1; l < rtn; ++l)
ans *= (k - 1), ans %= 998244353;
}
cout << (ans % 998244353);
return 0;
}
为什么会错