Getting tle in Labyrinth CSES problemset

problem link : here
code : https://cses.fi/paste/63c6d1d0c49ca7bf284dd5/