1 #include<bits/stdc++.h>
2 #define mod 998244353
3 using namespace std;
4 const int N=110;
5 int n,m,mp[2][N][N],f[2][N][N],ans;
6 char s[N][N];
7 void upd(int&x,int y){x+=y;if(x>=mod)x-=mod;}
8 int gcd(int a,int b){return !b?a:gcd(b,a%b);}
9 int main(){
10 #ifndef ONLINE_JUDGE
11 freopen("T2.in","r",stdin);
12 freopen("T2.out","w",stdout);
13 #endif
14 int T;scanf("%d",&T);
15 while(T--){
16 ans=0;
17 scanf("%d%d",&n,&m);
18 for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
19 for(int i=1;i<=n<<1;++i)
20 for(int j=1;j<=m<<1;++j)s[i][j]=s[(i-1)%n+1][(j-1)%m+1];
21 int d=gcd(n,m),cur=0;
22 for(int dx=0,dy;dx<=d;++dx){
23 dy=d-dx;
24 if(gcd(dx,n)!=1||gcd(dy,m)!=1)continue;
25
26 for(int i=1;i<=dx+1;++i)
27 for(int j=1;j<=dy+1;++j)
28 mp[0][i][j]=mp[1][i][j]=0;
29
30 for(int l=1,ax=1,ay=1;l<=n*m/d;++l){
31
32 cur^=1;
33
34 for(int i=1;i<=dx+1;++i)
35 for(int j=1;j<=dy+1;++j)
36 mp[cur][i][j]=mp[cur^1][i][j]|(s[ax+i-1][ay+j-1]-'0');
37
38 for(int i=1;i<=dx+1;++i)
39 for(int j=1;j<=dy+1;++j)f[0][i][j]=f[1][i][j]=0;
40
41 if(!mp[cur][1][1]){
42 f[cur][1][1]=1;
43 for(int i=1;i<=dx+1;++i)
44 for(int j=1;j<=dy+1;++j){
45 if(i!=dx+1&&!mp[cur][i+1][j])upd(f[cur][i+1][j],f[cur][i][j]);
46 if(j!=dy+1&&!mp[cur][i][j+1])upd(f[cur][i][j+1],f[cur][i][j]);
47 }
48 }
49
50 if(!mp[cur^1][dx+1][dy+1]){
51 f[cur^1][dx+1][dy+1]=1;
52 for(int i=dx+1;i;--i)
53 for(int j=dy+1;j;--j){
54 if(i!=1&&!mp[cur^1][i-1][j])upd(f[cur^1][i-1][j],f[cur^1][i][j]);
55 if(j!=1&&!mp[cur^1][i][j-1])upd(f[cur^1][i][j-1],f[cur^1][i][j]);
56 }
57 }
58
59 for(int i=1;i<=dx+1;++i)
60 for(int j=1;j<=dy+1;++j)if(mp[cur][i][j]){
61 int x=0;
62 if(i!=1)upd(x,f[cur][i-1][j]);
63 if(j!=1)upd(x,f[cur][i][j-1]);
64 int y = f[cur^1][i][j];
65 upd(ans, 1ll*((l-1)*d+i+j-2)*x%mod*y%mod);
66 }
67
68 ax=(ax+dx-1)%n+1,ay=(ay+dy-1)%m+1;
69
70 }
71 }
72 printf("%d
",ans);
73 }
74 return 0;
75 }