#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
for (int test = 1; test <= T; ++test) {
int N, M;
cin >> N >> M;
vector<string> line(N);
for (int i = 0; i < N; ++i)
cin >> line[i];
int ans = 0, fx=-1,fy=-1,lx=-1,ly=-1, oneCount=0;
for(int i = 0;i < N;i++)
{
for(int j = 0;j < M;j++)
{
if(line[i].at(j) == '1')
{
if(fx==-1){
fx = i;
fy = j;
}
oneCount++;
}
}
}
for(int i = N-1;i >= 0;i--)
{
for(int j = M-1;j >= 0;j--)
{
if(lx==-1 && line[i].at(j) == '1')
{
lx = i;
ly = j;
}
}
}
//cout << fx << " " << fy << endl << lx << " " << ly << endl;
//cout << oneCount << endl << fx << " " << fy << endl << lx << " " << ly << endl;
if(fx > lx || fy > ly || fx == -1 || lx == -1)
{
ans = 0;
}
else
{
//cout << "aaaaaaaaaa";
int counter=0;
bool plsbreak = false;
for(int i = fx;i <= lx;i++)
{
for(int j = fy;j <= ly;j++)
{
if(line[i].at(j) == '1')
{
counter++;
}
else
{
plsbreak = true;
break;
}
}
if(plsbreak)
{
break;
}
}
//cout << counter;
if(counter != oneCount || plsbreak)
{
ans = 0;
}
else
{
ans = 1;
}
}
cout << ans << endl;
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 1ms | 508 KiB | ||||
2 | Accepted | 1ms | 320 KiB | ||||
subtask2 | 50/50 | ||||||
3 | Accepted | 4ms | 424 KiB | ||||
4 | Accepted | 4ms | 320 KiB | ||||
5 | Accepted | 7ms | 568 KiB | ||||
6 | Accepted | 7ms | 724 KiB | ||||
7 | Accepted | 2ms | 320 KiB | ||||
subtask3 | 50/50 | ||||||
8 | Accepted | 4ms | 320 KiB | ||||
9 | Accepted | 7ms | 320 KiB | ||||
10 | Accepted | 26ms | 1052 KiB | ||||
11 | Accepted | 54ms | 2248 KiB | ||||
12 | Accepted | 61ms | 2620 KiB | ||||
13 | Accepted | 32ms | 1848 KiB | ||||
14 | Accepted | 46ms | 2356 KiB | ||||
15 | Accepted | 75ms | 4124 KiB | ||||
16 | Accepted | 45ms | 2752 KiB | ||||
17 | Accepted | 2ms | 320 KiB |