#C3442. Unique Grid Path Length

    ID: 46870 Type: Default 1000ms 256MiB

Unique Grid Path Length

Unique Grid Path Length

You are given a grid of characters with R rows. Each row is represented as a string. Your task is to determine the maximum length of a contiguous path in the grid such that no letter appears more than once along the path.

You can start at any cell in the grid, and from a cell you can move to any of its adjacent cells in the four cardinal directions (up, down, left, right). The path must be contiguous and you cannot revisit a letter that has already been included in the path.

The problem requires you to explore the grid using backtracking (or depth-first search) techniques to ensure that every path with distinct characters is considered.

Note: All formulas related to path lengths are standard and do not require special LaTeX formatting beyond this explanation.

inputFormat

The first line contains an integer R, the number of rows in the grid. Each of the next R lines contains a non-empty string representing a row of the grid.

outputFormat

Output a single integer, which is the maximum length of a contiguous path in the grid where all characters are unique.## sample

4
abcd
efgh
ijkl
mnop
16

</p>