LeeFuuChang the Potato once saw a way to generate password by combining numbers from a grid in a certain pattern.

So you, as a kind and generous student, wanted to help Potato finish the program that does the transformation from grid to password.
Feel free to use the following code as a starter.
def solve(n, d, grid):
"""
This is the explanation of the template code
We've read the input for you.
this function will take in 3 parameters:
- n:
a integer representing the size of the grid
- d:
a integer representing the starting direction
- grid:
a 'nxn' grid filled with integers
a.k.a, for any grid[i][j] is int
you need to return a string that satisfies the problem's requirement.
Feel free to delete this explanation after reading it.
"""
ans = ""
# your
# code
# here
return ans
_n, _d = map(int, input().split())
_grid = []
for _ in range(_n):
_row = []
for num in input().split():
_row.append(int(num))
_grid.append(_row)
print(solve(_n, _d, _grid))
Potato judges your code by the following limitations:
n <= 11, d = 0n <= 11, d = 1n <= 29, d = 0n <= 29, d = 1n <= 59, d ∈ {0, 1}
Potato's whisper: it is HIGHLY suggested to NOT use list in solving this problem, since list WON'T help you solve the actual midterm problem, but finding the pattern WILL.
First line contains 2 integers n,d representing:
nxnd direction ( refer to the image besides TA.Potato )Each of the following n lines contains n integers, forming a nxn grid.
It's guaranteed that:
1 <= n <= 59d ∈ {0, 1}0 <= grid[i][j] <= n*nOutput the password formed by the order of traversing the grid, with no space between numbers, and a newline at the end.