#include using namespace std; using pi = pair; const int inf = 1e6; signed main() { int v, d; cin >> v >> d; using bs = bitset<1000>; vector G(v); for (int i = 0; i < v; ++i) { string s; cin >> s; for (int j = 0; j < v; ++j) if (s[j] == '1') G[i].set(j); } vector> D(v, vector(v, pi(inf,inf))); bool valid = true; int invind = -1; for (int src = 0; valid && src < v; ++src) { bs eprev, oprev, empbs, pos, prev, next; bool isdegz = true; for (int i = 0; isdegz&&i < v;++i) if (G[src][i]) isdegz = false; if (isdegz) { valid = false; break; } pos.set(src); prev.set(src); for (int mv = 0; mv < d && pos.count() > 0;) { for (int i = 0; i < v;++i) if (pos[i]) next |= G[i]; pos = next & ~oprev; oprev |= next; next = empbs; ++mv; if (mv >= d) break; for (int i = 0; i < v; ++i) if (pos[i]) next |= G[i]; pos = next & ~eprev; eprev |= next; next = empbs; ++mv; if (mv >= d) break; } if(d%2){ for (int i = 0;valid&&i