122422024-12-10 13:05:42AGergoOil Fieldcpp17Accepted 100/10075ms4124 KiB
#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;
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms508 KiB
2Accepted1ms320 KiB
subtask250/50
3Accepted4ms424 KiB
4Accepted4ms320 KiB
5Accepted7ms568 KiB
6Accepted7ms724 KiB
7Accepted2ms320 KiB
subtask350/50
8Accepted4ms320 KiB
9Accepted7ms320 KiB
10Accepted26ms1052 KiB
11Accepted54ms2248 KiB
12Accepted61ms2620 KiB
13Accepted32ms1848 KiB
14Accepted46ms2356 KiB
15Accepted75ms4124 KiB
16Accepted45ms2752 KiB
17Accepted2ms320 KiB