---
# System prepended metadata

title: maintaning visited

---

def list_neighbor(x,y):
	adjacent_nodes = []

	if x!=n-1:
		adjacent_nodes.append(x+1,y)
	if y!=n-1:
		adjacent_nodes.append(x,y+1)
	if x!=0:
		adjacent_nodes.append(x-1,y)
	if y!=0:
		adjacent_nodes.append(x,y-1)

	return adjacent_nodes

def dijkstra():
    
    q = heapq()
    
    s = (0,0)
    dis = 0
    q.push((s,dis))
    
    while(!q.empty()):
        element = q.pop()
        if val[element[0]]==9:
            return element[1]
        l = list_neighbor(element[0])
        for (i in l):
            if val[i]==0:
                continue
            elif val[i]==1:
                q.push(i,element[1]+1)
                
            
# maintaning visited