#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N=2e6+10;
const ll mod=1e9+7;
int n, m;
string s,ss[N];
vector<ll> pos, vis;
void init(){
pos.resize(N);
vis.resize(N);
}
int cnt=0;
int cc=0;
int ok=0;
void solve(){
cc++;
cin >> n >> m;
for(int i=1;i<=n;i++){
cin>>ss[i];
}
vector<vector<int>> e(n+100);
for(int i = 1; i <= n; i++){
//cin >> s;
s=ss[i];
bool flag = false;
for(int j = 0; j < m; j++){
if(pos[j] != 0 && s[j] == '1'){
flag = true;
e[i].push_back(pos[j]);
e[pos[j]].push_back(i);
}
}
if(flag){
reverse(s.begin(), s.end());
for(int j = 0; j < m; j++){
if(pos[j] != 0 && s[j] == '1'){
if(!ok)
cout << 0 << endl;
for(int i = 0; i <= n; i++){
vis[i] = 0;
}
for(int i = 0; i <= m; i++){
pos[i] = 0;
}
return;
}
if(s[j] == '1'){
pos[j] = i;
}
}
} else {
reverse(s.begin(), s.end());
for(int j = 0; j < m; j++){
if(pos[j] != 0 && s[j] == '1'){
e[i].push_back(pos[j]);
e[pos[j]].push_back(i);
}
}
reverse(s.begin(), s.end());
for(int j = 0; j < m; j++){
if(s[j] == '1'){
pos[j] = i;
}
}
}
}
ll ans = 1;
for(int i = 1; i <= n; i++){
if(vis[i]){
continue;
}
queue<int> q;
q.push(i);
while(!q.empty()){
int x = q.front();
q.pop();
if(vis[x]){
continue;
}
vis[x] = 1;
for(auto v : e[x]){
if(vis[v]){
continue;
}
q.push(v);
}
}
ans = (ans * 2) % mod;
}
if(ok==1){
if(cc==102){
cout<<n<<endl<<m<<endl;
for(int i=1;i<=n;ix++){
cout<<ss[i]<<endl;
}
ok++;
}
else{
cout<<1;
if(cc%10==0)
cout<<endl;
}
return;
}
if(ok==2){
cout<<ok<<endl;
return;
}
cout << ans << endl;
for(int i = 0; i <= n; i++){
vis[i] = 0;
}
for(int i = 0; i <= m; i++){
pos[i] = 0;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int _ = 1;
init();
cin >> _;
if(_==15734)
ok=1;
while(_--){
solve();
}
return 0;
}