import sys input = sys.stdin.readline import random import time N, T = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] best_val = -1 for i in range(N): for j in range(N): if A[i][j] > best_val: best_val = A[i][j] Sx, Sy = i, j START = time.time() LIMIT = 1.99 best_score = 0 best_path = [] while time.time() - START < LIMIT: visited = [[False]*N for _ in range(N)] x, y = Sx, Sy visited[x][y] = True path = [(x, y)] score = A[x][y] for k in range(T-1): cand = [] for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]: nx, ny = x+dx, y+dy if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]: cand.append((A[nx][ny], nx, ny)) if not cand: break cand.sort(reverse=True) p =1-(T-k/ T) if random.random() < p: _, nx, ny = cand[0] else: _, nx, ny = random.choice(cand[:min(3,len(cand))]) visited[nx][ny] = True path.append((nx, ny)) score += A[nx][ny] x, y = nx, ny if score > best_score: best_score = score best_path = path print(len(best_path)) for x, y in best_path: print(x, y)