Methodical Rocket


Windra has an nn by nn grid of positive integers aa. Windra starts at the top left (which contains a[1][1]a[1][1]). Windra will then make a series of moves, he has two choices for each move, if he is at coordinate (i,j)(i, j) he can:

  • Move to coordinate (i+a[i][j],j)(i + a[i][j], j)
  • Move to coordinate (i,j+a[i][j])(i, j + a[i][j])

Note that some moves may go off the grid.

Windra's evil twin, Bindra, plots to kill Windra by setting him on a path that goes off the grid. Bindra would like to know how many paths go off the grid.

Input Format

Your first line will contain a single integer nn. Your next nn lines will contain nn integers each, representing your row of integers in aa.

Output Format

You should output a single integer representing the number of paths off the grid.

Constraints

  • 1≤n≤501 \leq n \leq 50
  • 1≤a[i][j]≤n+11 \leq a[i][j] \leq n + 1

Template

Code 1
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

# print your output

Sample Cases

Input 1
2
1 1
1 1
Output 1
6
Explanation 1

There are the following paths:

  • Down, down
  • Down, right, down
  • Down, right, right
  • Right, down, down
  • Right, down, right
  • Right, right
Input 2
5
2 1 1 1 1
1 1 2 1 3
1 1 1 1 1
1 1 1 2 1
4 1 1 1 1
Output 2
59
Input 3
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 2
1 1 1 2 1
Output 3
142

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.