#C10705. Longest Consecutive Path in a Matrix

    ID: 39940 Type: Default 1000ms 256MiB

Longest Consecutive Path in a Matrix

Longest Consecutive Path in a Matrix

Given an N x N matrix of lowercase letters, your task is to determine the length of the longest path such that each cell in the path contains the consecutive letter (in lexicographic order) from the previous cell. Movement is restricted to the four cardinal directions (up, down, left, right). For example, if a cell contains the letter 'a', its adjacent cell in the path must contain the letter 'b'.

The problem requires reading the matrix from stdin and writing the result to stdout. The input begins with a single integer N (the size of the matrix), followed by N lines each containing a string of N lowercase letters.

Note: The consecutive nature is defined as follows: if a cell contains a character x, an adjacent cell qualifies if it contains the character y such that $$ y = x + 1 $$ (where the addition is done over the Unicode code point values).

inputFormat

The first line of the input contains an integer N denoting the size of the matrix. The next N lines each contain a string of N lowercase letters representing a row of the matrix.

outputFormat

Output a single integer which is the length of the longest consecutive path in the matrix.

## sample
4
abcd
bcda
cdab
dabc
4