1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <list>
using namespace std;


std::list<unsigned char> sasiedzi[200];

bool* lastwynik;
bool* wynik;

unsigned char* startowy;

int main()
{
	std::ios_base::sync_with_stdio(false);
	int n,b,r;
	int m=0;

	cin>>n>>b>>r;
  
	char a;
	for(unsigned char k=0; k<n;k++)
	{
		for(unsigned char l=0;l<n;l++)
			{
				cin>>a;
				if(a=='1') 
				{
					sasiedzi[k].push_back(l);
					m++;
				}
			}
	}
	
	short c;
	startowy = new unsigned char[r];
	for (int k = 0; k < r; k++)
	{
		cin>>c;
		startowy[k] = (unsigned char) c;
		startowy[k]--;
	}

	lastwynik = new bool[n];

	bool isOdRazuOk = true;
	for (short k=0; k<n; k++)
	{
		if (k < b)
			lastwynik[k] = true;
		else
		{
			lastwynik[k] = false;
			isOdRazuOk = false;
		}
	}

	if (isOdRazuOk)
	{
		cout<<0<<endl;
		return 0;
	}

	long long stomil= 50000000LL;
	long long counter = 1;
	while (counter < stomil/m)
	{
		wynik = new bool[n];

		bool allZero = true;

		for(unsigned char k=0; k<n;k++)
		{
			bool isOk = true;
			for (auto it = sasiedzi[k].begin(); it != sasiedzi[k].end(); ++it)
			{
				if (!lastwynik[(*it)])
				{
					isOk = false;
					break;
				}
			}

			if (isOk)
			{
				wynik[k] = true;
				allZero = false;
			}
			else
				wynik[k] = false;

			//cout<<wynik[k]<<" ";
		}
		//cout<<endl;

	if (allZero)
	{
		cout<<-1<<endl;
		return 0;
	}


	bool isOk = true;
	for (int k = 0; k< r ; k++)
	{
		if (!wynik[startowy[k]])
		{
			isOk = false;
			break;
		}
	}
	if (isOk)
	{
		cout<<counter<<endl;
		return 0;
	}

	delete lastwynik;
	lastwynik = wynik;
	counter++;
	}

	cout<<-1<<endl;

}