112212024-07-17 14:34:29bzyOil Fieldcpp14Accepted 100/10087ms988 KiB
#include <bits/stdc++.h>
using namespace std;
void run_case()
{
    int n,m;
    cin>>n>>m;
    vector <string> matrix;
    bool valid = true;
    bool firstrow = false;
    bool hadone = false;
    for(int i = 0; i< n ;i++){
        string s;
        cin>>s;
        matrix.push_back(s);
        bool f1 = false;
        if(valid == true){
            if(firstrow == false){
                for(int j = 0;j<m;j++){
                    if(s[j] == '1' && f1 == false){
                        f1 = true;
                        hadone = true;
                        firstrow = true;
                    }
                    else if(s[j] == '1' && f1 == true){
                        if(s[j-1] != '1') valid = false;

                    }
                }
            }
            else{
                for(int j = 0;j<m;j++){
                    if(s[j] == '1' && f1 == false){
                        f1 = true;
                        if(matrix[i-1][j] != '1') valid = false;
                    }
                    else if(s[j] == '1' && f1 == true){
                        if(s[j-1] != '1' || matrix[i-1][j] != '1') valid = false;
                    }
                }
            }
        }
    }
    int occurance = 0;
    bool firstocc = false;
    for(int i = 0; i<n;i++){
        int freq = 0;
        if(firstocc == false){
            for(int j = 0;j<m;j++){
                if(matrix[i][j] == '1'){
                    firstocc = true;
                    freq++;
                }
            }
            occurance = freq;
        }
        else{
            for(int j = 0;j<m;j++){
                if(matrix[i][j] == '1')freq ++;
            }
            if(freq != 0 && freq != occurance) valid = false;
        }
    }
    if(valid == false || hadone == false) cout<<0<<endl;
    else cout<<1<<endl;

}

int main()
{
    int t;
    cin>>t;
    while(t-->0){
        run_case();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms356 KiB
2Accepted2ms356 KiB
subtask250/50
3Accepted8ms552 KiB
4Accepted8ms392 KiB
5Accepted10ms496 KiB
6Accepted10ms396 KiB
7Accepted4ms376 KiB
subtask350/50
8Accepted8ms500 KiB
9Accepted10ms356 KiB
10Accepted32ms632 KiB
11Accepted61ms356 KiB
12Accepted68ms492 KiB
13Accepted37ms756 KiB
14Accepted50ms812 KiB
15Accepted87ms988 KiB
16Accepted54ms932 KiB
17Accepted4ms356 KiB