/************************************************
Algorithmen und Problemloesungen mit C++,
http://www.algorithmen-und-problemloesungen.de
Copyright @2007 by Doina Logofatu
************************************************/

#include <queue>
#include <fstream>
#include <vector>
#include <algorithm>

int main(){    
    std::ifstream in("graph.in");
    std::ofstream out("bfs.out");
    std::queue<short> qu;
    std::vector<short> v;
    short n, k, t;   
    in >> n >> k;
    qu.push(k-1);    
    std::vector<std::vector<bool>> a(n, std::vector<bool>(n, true));
    for(short i=0; i<n; i++)
      for(short j=0; j<n; j++){
        in >> t;
        a[i][j] = t ? true : false; 
      }
    while(!qu.empty()){
     short t = qu.front();     
     for(short i=0; i<n; ++i)
      if(a[t][i] && 
         find(v.begin(), v.end(), i)==v.end()
        ){
        qu.push(i); 
      }    
     if(find(v.begin(), v.end(), t)==v.end()){
       v.push_back(t);
     } else qu.pop();   
    }    
    for(short i=0; i<v.size(); i++)
     out << v[i]+1 << " ";
    return 0;
}
