#include <iostream>
using namespace std;
int a[20][20],n, b[20],c[20], st = 0, dr = 0;
int main()
{
cin >> n >> c[0]; b[c[0]] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
while (st <= dr)
{
int x = c[st];
for (int i = 1; i <= n; i++)
if (a[x][i] && !b[i]) { dr++; c[dr] = i; b[i] = 1;}
for (int i = st; i <= dr; i++) cout << c[i] <<" ";
cout << endl;
st++;
}
return 0;
}