#include #include using namespace std; // #include // using namespace boost::multiprecision; using lnt = long long; using Real = long double; #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(lnt i=0;i<(n);++i) #define RANGE(i, a, b) for(lnt i=(a);i<(b);++i) #define RREP(i, n) for(lnt i=(n)-1;i>= 0;--i) #define ln '\n' #define pb push_back #define eb emplace_back #define pque priority_queue #define umap unordered_map #define uset unordered_set #define dcout cout< inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;} template inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;} template inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;} template inline T nHr(T a,T b){return nCr(a+b-1,b);} template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} using pint = pair; using plnt = pair; using vint = vector; using vlnt = vector; constexpr lnt MOD = 1e9+7; int main(){ summon_tourist; int N, T; cin >> N >> T; vector A(N, vint(N)); REP(i, N) REP(j, N) cin >> A[i][j]; int bs = min(3, (int)sqrt(T)); int mi, mj; int mx = -1; REP(i, N-bs+1) REP(j, N-bs+1) { int sum = 0; RANGE(k, i, i+bs) RANGE(l, j, j+bs) sum += A[k][l]; if(chmax(mx, sum)) { mi = i; mj = j; } } int si, sj; mx = -1; RANGE(i, mi, mi+bs) RANGE(j, mj, mj+bs) { if(chmax(mx, A[i][j])) { si = i; sj = j; } } vector path; path.emplace_back(si,sj); vector> vis(N, vector(N,false)); vis[si][sj] = true; const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; for(int step = 1; step < T; step++){ int ci = path.back().first, cj = path.back().second; int best_i = -1, best_j = -1, best_val = -BIG; REP(d, 4){ int ni = ci + dx[d], nj = cj + dy[d]; if(ni<0 || ni>=N || nj<0 || nj>=N) continue; int val = A[ni][nj]; if(!vis[ni][nj]){ if(chmax(best_val, val)){ best_i = ni; best_j = nj; } } } if(best_i == -1) break; path.emplace_back(best_i,best_j); vis[best_i][best_j]=true; } cout << path.size() << ln; for(auto [i,j] : path) cout << i << ' ' << j << ln; }