TRAVERSE - Traverse through the board


An n x n game board is filled with integers, one positive integer per square. The objective is to travel along any legitimate path from the upper left corner to the lower right corner of the board.  
 
Rules
 
1. The number in any one square describes how far a step away from that location must be.  
2. If the step size moves out of the game board, then that step is not allowed.
3. All steps must be either to the right or towards the bottom.  
 
Note that a 0 is a dead end which prevents any further progress.  
 
Consider the 4 x 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three paths from the start to the target, with the irrelevant numbers in each removed.

fig

Input

The first line contains the value of n followed by a nxn matrix depicting the board configuration.

Output

The output consists of  a single integer, which is the number of paths from the upper left corner to the lower right corner.

Example 

Input:


2331
1213
1231
3110

Output:
3

Input:


3332
1213
1232
2120

Output:

0

Input:

5
11101
01111
11111
11101
11101
      
Output:
7


hide comments
sandeepd: 2019-12-03 19:55:43

Image isn't needed, problem description describes it well enough. Just note that newline is not needed in output.

ankitraj7217: 2018-07-15 10:00:03

Don't print new line after output..Costed me 3 WA.What a waste of time..

anuj_18: 2018-06-30 09:36:00

Be carefull with input there are no spaces provided between elements of a row.

amulyagaur: 2017-12-12 08:35:57

only recursion passes in 0.00s

PRIBAN91: 2015-08-25 12:54:04

Terrible output format! Don't print a newline character in output. Should have read the comments first!

mr_lazy: 2015-07-22 11:24:20

Make sure,, you don't add a newline after printing the answer.. It costed me 2 WA :(

Piyush Kumar: 2015-06-14 11:46:18

The image is missing .

vivek singh rana: 2015-06-04 12:10:08

either we can move only right,or only down,,only two choices,,,thats why answer of test case 1 is 3 (would have been 6 otherwise).

Jugal kishor sahu: 2014-12-23 10:12:22

AC: easy recursion
2 wrong answer due to '\n'........
don't print newline after output......

NOVICE: 2014-09-30 10:38:20

Cakewalk DP>>>!!


Added by:Infinity
Date:2011-03-21
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ADA95 ASM32 ASM64 BASH CLPS CLOJURE LISP sbcl LISP clisp D ERL FSHARP FORTRAN GO ICON ICK LUA NEM NICE OCAML PIKE PRLG-swi SCM guile SCM qobi ST TCL WHITESPACE