You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2.
A V-shaped diagonal segment is defined as:

The segment starts with 1.
The subsequent elements follow this infinite sequence: 2, 0, 2, 0, ....
The segment:

Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).
Continues the sequence in the same diagonal direction.
Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.

Return the length of the longest V-shaped diagonal segment. If no valid segment exists, return 0.

Example 1:

Input: grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 5
Explanation:

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) → (1,3) → (2,4), takes a 90-degree clockwise turn at (2,4), and continues as (3,3) → (4,2).

Example 2:

Input: grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 4
Explanation:

The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) → (3,2), takes a 90-degree clockwise turn at (3,2), and continues as (2,1) → (1,0).

Example 3:

Input: grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]
Output: 5
Explanation:

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) → (1,1) → (2,2) → (3,3) → (4,4).

Example 4:

Input: grid = [[1]]
Output: 1
Explanation:
The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0).

Constraints:

n == grid.length
m == grid[i].length
1 <= n, m <= 500
grid[i][j] is either 0, 1 or 2.

Your solution to the problem should be a method of the class Solution called lenOfVDiagonal and should pass all tests. Use the following starter code:
```python
class Solution:
    def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2.
A V-shaped diagonal segment is defined as:

The segment starts with 1.
The subsequent elements follow this infinite sequence: 2, 0, 2, 0, ....
The segment:

Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).
Continues the sequence in the same diagonal direction.
Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.

Return the length of the longest V-shaped diagonal segment. If no valid segment exists, return 0.

Example 1:

Input: grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 5
Explanation:

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) → (1,3) → (2,4), takes a 90-degree clockwise turn at (2,4), and continues as (3,3) → (4,2).

Example 2:

Input: grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 4
Explanation:

The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) → (3,2), takes a 90-degree clockwise turn at (3,2), and continues as (2,1) → (1,0).

Example 3:

Input: grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]
Output: 5
Explanation:

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) → (1,1) → (2,2) → (3,3) → (4,4).

Example 4:

Input: grid = [[1]]
Output: 1
Explanation:
The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0).

Constraints:

n == grid.length
m == grid[i].length
1 <= n, m <= 500
grid[i][j] is either 0, 1 or 2.

Your solution to the problem should be a method of the class Solution called lenOfVDiagonal and should pass all tests. Use the following starter code:
```python
class Solution:
    def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().lenOfVDiagonal(grid = [[1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[1,1,1],[1,1,1],[1,1,1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,1,2],[0,2,1,2,1,2,2],[0,0,2,1,2,1,2],[2,0,2,1,2,1,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0],[1,2,2,2],[0,2,1,2],[0,0,2,1]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[0,0,0],[0,1,0],[0,0,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,1,2],[0,2,0],[1,0,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,2,0,2],[2,2,0,2,0],[0,1,0,2,0],[2,0,2,0,2]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,2,0,0],[0,0,0,0,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,1,2,2,1],[2,0,2,2,0,2],[2,0,1,1,0,0],[1,0,2,2,2,0],[2,0,0,2,2,2]]) == 5

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,2,0,1],[0,2,0,2,0],[2,0,1,0,2],[0,2,0,2,0],[1,0,2,0,1]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]) == 5

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,2,0,2],[0,2,0,2,0],[2,0,1,0,2],[0,2,0,2,0],[2,0,2,0,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]) == 5

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0],[2,0,1,2,0],[0,2,0,1,2],[2,1,2,0,1],[0,2,0,2,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,1,2,2,2,2,2],[2,2,2,2,2,0,2,2,2,2],[2,2,2,2,2,2,0,2,2,2],[2,2,2,2,2,2,2,0,2,2],[2,2,2,2,2,2,2,2,0,2],[2,2,2,2,2,2,2,2,2,0],[2,2,2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,1,2,0,2,0,2,0],[0,0,0,0,1,2,0,2,0,2],[0,0,0,0,0,1,2,0,2,0],[0,0,0,0,0,0,1,2,0,2],[0,0,0,0,0,0,0,1,2,0],[0,0,0,0,0,0,0,0,1,2],[0,0,0,0,0,0,0,0,0,1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[0,0,0,0,0,0],[0,1,2,0,2,0],[0,2,0,2,0,1],[0,0,2,0,2,0],[0,0,0,1,2,2],[0,0,0,0,2,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,1,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2],[2,0,1,2,0,1,2],[2,0,2,0,2,0,2],[1,0,2,1,0,2,1],[2,0,2,0,2,0,2],[2,1,2,0,2,0,1]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,1,2,0,1,2,0,1,2],[2,0,2,0,2,0,2,0,2,0],[1,0,2,1,0,2,1,0,2,1],[2,0,2,0,2,0,2,0,2,0],[2,1,2,0,2,0,1,0,1,0],[2,0,2,0,2,0,2,0,2,0],[2,2,0,2,0,2,2,0,2,2],[1,0,1,0,1,0,1,0,1,0],[2,0,2,0,2,0,2,0,2,0]]) == 7

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,0,0,0,0,0,0,0,2],[2,0,0,0,0,0,0,0,0,2],[2,0,0,0,0,0,0,0,0,2],[2,0,0,0,0,0,0,0,0,2],[2,0,0,0,1,2,2,2,0,2],[2,0,0,0,0,2,0,0,0,2],[2,0,0,0,0,0,2,0,0,2],[2,0,0,0,0,0,0,2,0,2],[2,0,0,0,0,0,0,0,1,2]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,1,2,0,2],[0,1,2,2,0,2,1],[2,0,2,0,2,0,2],[1,2,0,1,0,2,0],[0,2,0,2,2,0,1],[2,1,2,0,2,0,2]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2,2],[2,0,2,0,2,0,2,0,2,0,2],[2,0,1,0,1,0,1,0,1,0,2],[2,0,2,0,2,0,2,0,2,0,2],[2,0,1,0,1,0,1,0,1,0,2],[2,0,2,0,2,0,2,0,2,0,2]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0,0,0,2],[2,2,0,0,0,2,2],[2,2,2,0,2,2,2],[2,2,0,1,0,2,2],[2,0,0,0,0,0,2],[2,2,2,2,2,2,2]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,1,0,0,0],[0,0,0,2,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,2,0,2,0,2,0,2,0,2,0],[0,1,0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0,2,0],[0,2,0,1,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,1,0,2,0,2,0,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,1,2],[2,0,1,0,2],[2,0,2,0,2],[2,1,2,0,2],[2,0,2,1,2],[2,0,2,0,2],[2,2,2,2,2]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,2,1,2,2,2,1,2],[2,0,2,2,2,0,2,2,2,0],[2,0,1,2,0,1,2,0,1,2],[2,1,0,1,0,1,0,1,0,1],[2,0,2,0,2,0,2,0,2,0],[2,1,2,0,2,0,1,0,1,0],[2,0,2,0,2,0,2,0,2,0],[2,2,0,2,0,2,2,0,2,2],[1,0,1,0,1,0,1,0,1,0],[2,0,2,0,2,0,2,0,2,0]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0,0,0,0,0,0,2],[0,1,2,0,2,0,2,0,1,0],[0,2,0,1,0,1,0,1,0,2],[0,0,2,0,2,0,2,0,2,0],[0,0,0,2,0,2,0,2,0,0],[0,0,0,0,1,2,0,2,0,0],[0,0,0,0,2,0,1,0,2,0],[0,0,0,0,0,2,0,2,0,2],[0,0,0,0,0,0,2,0,2,0],[2,0,0,0,0,0,0,2,0,2]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,2,0,2,0,2,0,2,0,2],[0,1,2,0,2,0,1,2,0,2,0],[2,0,2,1,2,0,2,0,2,1,2],[0,2,0,2,0,2,0,2,0,2,0],[1,2,2,0,2,0,1,2,0,2,1],[2,0,2,0,2,0,2,0,2,0,2],[0,1,2,0,2,0,1,2,0,2,0],[2,0,2,1,2,0,2,0,2,1,2],[0,2,0,2,0,2,0,2,0,2,0],[1,2,2,0,2,0,1,2,0,2,1]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,1,2,2,2,2,2],[2,2,2,2,2,0,2,2,2,2],[2,2,2,2,2,2,0,2,2,2],[2,2,2,2,2,2,2,0,2,2],[2,2,2,2,2,2,2,2,0,2],[2,2,2,2,2,2,2,2,2,0],[2,2,2,2,2,2,2,2,2,1]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,1,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2],[2,2,2,2,2,2,2],[2,2,2,1,2,2,2],[2,2,2,2,0,2,2],[2,2,2,2,2,2,2],[2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2]]) == 0

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2,0,2,0,1],[2,0,1,2,0,2,0,2,0,2],[0,1,2,0,1,2,0,2,0,2],[2,0,2,0,2,0,1,2,0,2],[2,0,2,0,2,0,2,0,1,2],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0]]) == 8

Answer: assert Solution().lenOfVDiagonal(grid = [[0,0,0,0,0,0,0],[0,1,2,0,2,0,0],[0,2,0,2,0,2,0],[0,2,1,2,1,2,0],[0,0,2,0,2,0,1],[0,2,0,2,0,2,0],[0,0,0,0,0,0,0]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,2,0,0,0,0,0,0],[2,0,2,0,2,0,2,0,2,0],[2,0,1,2,2,2,0,2,0,2],[0,2,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,1,2,2],[2,0,2,2,0,2,0,2],[2,0,1,1,0,0,2,0],[1,0,2,2,2,0,2,2],[2,0,0,2,2,2,2,0],[0,1,2,0,1,2,0,1]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,1,2,0,2,0,1,2,0,2],[2,2,0,2,0,2,2,0,2,0],[2,0,2,1,2,0,2,0,2,1],[2,0,0,2,0,2,0,2,0,2],[2,1,2,0,2,0,1,2,0,2],[2,2,0,2,0,2,2,0,2,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,0,0,0,0,0,0,0,2],[2,0,1,2,0,2,0,2,0,2],[2,0,0,0,1,0,0,0,0,2],[2,0,2,0,2,0,2,0,2,2],[2,0,0,0,0,0,0,0,0,2],[2,0,0,0,0,1,0,0,0,2],[2,0,0,0,0,0,1,0,0,2],[2,0,0,0,0,0,0,1,0,2],[2,0,0,0,0,0,0,0,1,2]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2,0,2,0,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,0,0,0,0,0,0,0,2],[2,0,1,2,0,2,0,2,0,2],[2,0,0,0,1,0,0,0,0,2],[2,0,2,0,2,0,2,0,2,2],[2,0,0,0,0,0,0,0,0,2]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,1,2],[2,2,2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2],[2,2,0,2,0,2],[2,0,1,2,0,2],[2,0,2,0,1,2],[2,0,2,0,2,1],[2,0,2,0,2,0]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,0,0,0,0,0,0,0,0],[0,2,0,0,0,0,0,0,0,0],[0,0,2,0,0,0,0,0,0,0],[0,0,0,2,0,0,0,0,0,0],[0,0,0,0,2,0,0,0,0,0],[0,0,0,0,0,2,0,0,0,0],[0,0,0,0,0,0,2,0,0,0],[0,0,0,0,0,0,0,2,0,0],[0,0,0,0,0,0,0,0,2,0],[0,0,0,0,0,0,0,0,0,1]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,1,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,2,1,2,2,2,1],[2,0,2,0,2,0,2,0,2],[1,2,1,2,0,2,1,2,1],[2,0,2,0,2,0,2,0,2],[1,2,1,2,1,2,1,2,1],[2,0,2,0,2,0,2,0,2],[1,2,1,2,1,2,1,2,1]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2,2,2],[2,1,2,0,2,0,2,0,2,0,2,0],[2,0,2,2,2,2,2,2,2,2,2,2],[2,0,2,0,2,0,2,0,2,0,2,0],[2,2,2,2,2,2,2,2,2,2,2,2],[2,0,2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0,2,1]]) == 8

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,2,0,2,0,1,2,0,2],[2,2,0,2,0,2,2,0,2,0],[0,2,1,2,1,2,0,2,0,2],[2,0,2,1,2,0,2,0,2,1],[0,2,0,2,0,2,0,2,0,2],[1,2,2,0,2,0,1,2,0,2],[2,2,0,2,0,2,2,0,2,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2],[2,2,2,1,2,2,2,2,2],[2,2,2,2,0,2,2,2,2],[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,1,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[0,1,2,0,2,0,2,0,2,0],[2,0,2,0,1,2,0,2,0,1],[0,2,0,2,0,2,0,2,0,2],[1,2,0,2,0,1,2,0,2,0]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,1,2,0,2,1],[2,0,2,0,2,0,2,0,2],[0,2,1,2,0,1,2,0,1],[2,0,2,0,2,0,2,0,2],[1,2,0,2,1,2,0,2,1]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,2,0,2,0,1,2,0,2],[0,2,0,2,0,2,2,0,2,0],[2,0,2,0,2,0,0,2,0,2],[0,2,0,2,0,2,0,2,0,1],[2,0,2,0,2,0,2,0,2,2],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,1,2,0,2]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2],[2,1,2,1,2],[2,2,2,2,2],[1,2,1,2,1],[2,2,2,2,2],[2,1,2,1,2],[2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,1,2,0,2,0,1],[2,2,0,2,0,2,2],[0,2,1,2,1,2,0],[2,0,2,1,2,0,2],[0,2,0,2,0,2,0],[1,2,2,0,2,0,2]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2],[2,0,2,0,2,0,2,0,2],[2,0,1,0,1,0,1,0,2],[2,0,2,0,2,0,2,0,2],[2,0,1,0,1,0,1,0,2],[2,0,2,0,2,0,2,0,2]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[1,0,2,0,2,0,1,2,0,2],[0,2,0,2,0,2,2,0,2,0],[2,0,1,1,0,0,0,2,0,2],[0,2,0,2,0,2,0,2,0,1],[2,0,2,0,2,0,2,0,2,2],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,1,2,0,2]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2,2,2],[2,0,0,0,0,0,0,0,0,2],[2,0,1,2,0,2,0,2,0,2],[2,0,2,0,1,0,1,0,1,0],[2,0,2,0,2,1,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0],[2,0,2,0,2,0,2,0,2,0]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0,0,0,0,0,0,0,0],[0,1,2,2,2,2,2,2,2,2,0],[0,2,0,2,2,2,2,2,2,0,2],[0,0,2,0,2,2,2,2,0,2,0],[0,0,0,2,0,1,2,0,2,0,0],[0,0,0,0,2,0,0,2,0,0,0],[0,0,0,0,0,2,2,2,2,2,2],[0,0,0,0,0,2,0,2,0,2,0],[0,0,0,0,0,0,2,0,2,0,0],[0,0,0,0,0,0,0,2,0,0,0]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,1,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2],[2,2,2,1,2,2,2,2],[2,2,2,2,0,2,2,2],[2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,1,2,0,2,0,1,2],[0,2,0,2,0,2,2,0],[1,0,2,0,2,0,1,2],[2,0,2,0,2,2,0,2],[0,2,0,1,2,0,2,0],[1,2,2,0,2,0,1,2]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2,1],[2,2,0,2,0,2,2],[2,0,2,0,2,0,2],[1,0,1,0,1,0,1],[2,0,2,0,2,0,2],[2,2,0,2,0,2,2],[1,2,0,2,0,2,1]]) == 3

Answer: assert Solution().lenOfVDiagonal(grid = [[2,2,2,1,0,2,2],[1,0,2,0,2,0,1],[2,0,0,1,2,0,2],[0,2,2,0,2,2,0],[1,0,2,0,2,0,1],[2,0,2,2,0,2,2]]) == 6

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,1,2],[2,0,2,0,2,0,2],[0,2,0,2,0,2,0],[2,0,2,0,2,0,2],[1,2,0,2,0,1,2]]) == 2

Answer: assert Solution().lenOfVDiagonal(grid = [[2,0,0,0,1,2,0,2,0,2],[0,0,2,2,2,0,2,0,2,0],[0,1,2,0,0,2,0,2,0,2],[0,0,0,1,2,0,2,0,2,0],[2,0,2,0,1,2,0,2,0,2],[0,2,0,2,0,1,2,0,2,0],[2,0,2,0,2,0,1,2,0,2],[0,2,0,2,0,2,0,1,2,0],[2,0,2,0,2,0,2,0,1,2],[0,2,0,2,0,2,0,2,0,1]]) == 4

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0],[0,2,0,2,0,2,0,2,0,2],[2,0,2,0,2,0,2,0,2,0]]) == 1

Answer: assert Solution().lenOfVDiagonal(grid = [[1,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2,2]]) == 2

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
    def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        next_digit = {1: 2, 2: 0, 0: 2}

        def within_bounds(i, j):
            return 0 <= i < m and 0 <= j < n

        @cache
        def f(i, j, di, dj, turned):
            result = 1
            successor = next_digit[grid[i][j]]

            if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
                result = 1 + f(i + di, j + dj, di, dj, turned)

            if not turned:
                di, dj = dj, -di
                if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
                    result = max(result, 1 + f(i + di, j + dj, di, dj, True))

            return result

        directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
        result = 0

        for i in range(m):
            for j in range(n):
                if grid[i][j] != 1:
                    continue
                for di, dj in directions:
                    result = max(result, f(i, j, di, dj, False))

        return result


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given two strings s and t.
Return the length of the longest common prefix between s and t after removing at most one character from s.
Note: s can be left without any removal.

Example 1:

Input: s = "madxa", t = "madam"
Output: 4
Explanation:
Removing s[3] from s results in "mada", which has a longest common prefix of length 4 with t.

Example 2:

Input: s = "leetcode", t = "eetcode"
Output: 7
Explanation:
Removing s[0] from s results in "eetcode", which matches t.

Example 3:

Input: s = "one", t = "one"
Output: 3
Explanation:
No removal is needed.

Example 4:

Input: s = "a", t = "b"
Output: 0
Explanation:
s and t cannot have a common prefix.

Constraints:

1 <= s.length <= 105
1 <= t.length <= 105
s and t contain only lowercase English letters.

Your solution to the problem should be a method of the class Solution called longestCommonPrefix and should pass all tests. Use the following starter code:
```python
class Solution:
    def longestCommonPrefix(self, s: str, t: str) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given two strings s and t.
Return the length of the longest common prefix between s and t after removing at most one character from s.
Note: s can be left without any removal.

Example 1:

Input: s = "madxa", t = "madam"
Output: 4
Explanation:
Removing s[3] from s results in "mada", which has a longest common prefix of length 4 with t.

Example 2:

Input: s = "leetcode", t = "eetcode"
Output: 7
Explanation:
Removing s[0] from s results in "eetcode", which matches t.

Example 3:

Input: s = "one", t = "one"
Output: 3
Explanation:
No removal is needed.

Example 4:

Input: s = "a", t = "b"
Output: 0
Explanation:
s and t cannot have a common prefix.

Constraints:

1 <= s.length <= 105
1 <= t.length <= 105
s and t contain only lowercase English letters.

Your solution to the problem should be a method of the class Solution called longestCommonPrefix and should pass all tests. Use the following starter code:
```python
class Solution:
    def longestCommonPrefix(self, s: str, t: str) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().longestCommonPrefix(s = "one", t = "one") == 3

assert Solution().longestCommonPrefix(s = "abcde", t = "bcdef") == 4

assert Solution().longestCommonPrefix(s = "abcde", t = "fbcde") == 0

assert Solution().longestCommonPrefix(s = "xyz", t = "xya") == 2

assert Solution().longestCommonPrefix(s = "ababc", t = "abcab") == 2

assert Solution().longestCommonPrefix(s = "abcdef", t = "abcdfg") == 5

assert Solution().longestCommonPrefix(s = "pqr", t = "pqs") == 2

assert Solution().longestCommonPrefix(s = "abcdef", t = "fedcba") == 0

assert Solution().longestCommonPrefix(s = "abcdefg", t = "xyzabcdefg") == 0

assert Solution().longestCommonPrefix(s = "abcd", t = "abcf") == 3

assert Solution().longestCommonPrefix(s = "abcdefg", t = "gfedcba") == 0

assert Solution().longestCommonPrefix(s = "ababab", t = "bababa") == 5

assert Solution().longestCommonPrefix(s = "abcde", t = "abcdf") == 4

assert Solution().longestCommonPrefix(s = "xyz", t = "xyw") == 2

assert Solution().longestCommonPrefix(s = "madxa", t = "madam") == 4

assert Solution().longestCommonPrefix(s = "aaaa", t = "aaaa") == 4

assert Solution().longestCommonPrefix(s = "abc", t = "def") == 0

assert Solution().longestCommonPrefix(s = "a", t = "b") == 0

assert Solution().longestCommonPrefix(s = "leetcode", t = "eetcode") == 7

assert Solution().longestCommonPrefix(s = "xyza", t = "xyz") == 3

assert Solution().longestCommonPrefix(s = "xyzabcdefg", t = "abcdefg") == 0

assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcdefyz") == 5

assert Solution().longestCommonPrefix(s = "abcde", t = "abfde") == 2

assert Solution().longestCommonPrefix(s = "aaaaa", t = "aaaaa") == 5

assert Solution().longestCommonPrefix(s = "aabbcc", t = "aabbc") == 5

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghio") == 9

assert Solution().longestCommonPrefix(s = "ababababab", t = "bababababa") == 9

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghijxyz") == 10

assert Solution().longestCommonPrefix(s = "one", t = "two") == 0

assert Solution().longestCommonPrefix(s = "aaaaabbbbbcccccddddd", t = "aaaaabbbbbcccccdddd") == 19

assert Solution().longestCommonPrefix(s = "abcdefgh", t = "fghijklm") == 0

assert Solution().longestCommonPrefix(s = "palindrome", t = "palindromex") == 10

assert Solution().longestCommonPrefix(s = "abcdabcabc", t = "abcdabcdabc") == 7

assert Solution().longestCommonPrefix(s = "qwertyuiopasdfghjklzxcvbnm", t = "qwertyuiopasdfghjklzxcvbn") == 25

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghijz") == 10

assert Solution().longestCommonPrefix(s = "thisisaverylongstring", t = "thisisaverylongstrig") == 20

assert Solution().longestCommonPrefix(s = "mississippi", t = "mississippix") == 11

assert Solution().longestCommonPrefix(s = "longestprefix", t = "longestpref") == 11

assert Solution().longestCommonPrefix(s = "abcabcabcabcabcabcabc", t = "abcabcabcabcabcabc") == 18

assert Solution().longestCommonPrefix(s = "zxyxzyzyzx", t = "zxyxzyzyzy") == 9

assert Solution().longestCommonPrefix(s = "pneumonoultramicroscopicsilicovolcanoconiosis", t = "pneumonoultramicroscopicsilicovolcanoconioz") == 42

assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabax") == 10

assert Solution().longestCommonPrefix(s = "longstringwithprefix", t = "longstringwithprifix") == 16

assert Solution().longestCommonPrefix(s = "repeatedcharacters", t = "repeatecharacters") == 17

assert Solution().longestCommonPrefix(s = "racecar", t = "racecars") == 7

assert Solution().longestCommonPrefix(s = "mississippi", t = "misissippix") == 10

assert Solution().longestCommonPrefix(s = "sameprefix", t = "sameprefixsameprefix") == 10

assert Solution().longestCommonPrefix(s = "aaaaaabb", t = "aaaaabbb") == 7

assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabcabcd") == 15

assert Solution().longestCommonPrefix(s = "abcabcabcabcabcabcabcabcabcabc", t = "abcabcabcabcabcabcabcabcabcab") == 29

assert Solution().longestCommonPrefix(s = "a", t = "a") == 1

assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeffggg") == 14

assert Solution().longestCommonPrefix(s = "prefixsuffix", t = "prefixsufiix") == 10

assert Solution().longestCommonPrefix(s = "abcde", t = "edcba") == 0

assert Solution().longestCommonPrefix(s = "xyzabcxyzabcxyz", t = "xyzabcxyzabcyzz") == 14

assert Solution().longestCommonPrefix(s = "prefixsuffix", t = "prefixsuffixx") == 12

assert Solution().longestCommonPrefix(s = "removeonechar", t = "removeonecha") == 12

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghii") == 9

assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabrab") == 11

assert Solution().longestCommonPrefix(s = "hello", t = "hell") == 4

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghix") == 9

assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonprex") == 16

assert Solution().longestCommonPrefix(s = "abcdefgh", t = "abababab") == 2

assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabcabaa") == 14

assert Solution().longestCommonPrefix(s = "racecar", t = "racecarx") == 7

assert Solution().longestCommonPrefix(s = "xyxyxyxyxyxy", t = "yxxyxyxyxyxy") == 2

assert Solution().longestCommonPrefix(s = "zyxwvutsrqponmlkjihgfedcba", t = "zyxwvutsrqponmlkjihgfedcba") == 26

assert Solution().longestCommonPrefix(s = "abcabcabcabc", t = "abcabcabcab") == 11

assert Solution().longestCommonPrefix(s = "abc", t = "abcde") == 3

assert Solution().longestCommonPrefix(s = "mississippi", t = "missisippi") == 10

assert Solution().longestCommonPrefix(s = "", t = "") == 0

assert Solution().longestCommonPrefix(s = "abcd", t = "dabc") == 0

assert Solution().longestCommonPrefix(s = "sameprefixsame", t = "sameprefixsam") == 13

assert Solution().longestCommonPrefix(s = "aaaaaa", t = "bbbbbb") == 0

assert Solution().longestCommonPrefix(s = "abcdefgabcdefg", t = "abcdefgabcdxfg") == 11

assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxyza") == 26

assert Solution().longestCommonPrefix(s = "abcdabcdabcd", t = "abcdabcdabce") == 11

assert Solution().longestCommonPrefix(s = "mississippi", t = "mississipi") == 10

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghia") == 9

assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaxa") == 7

assert Solution().longestCommonPrefix(s = "mississippi", t = "missisipi") == 8

assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaba") == 5

assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonpreffix") == 17

assert Solution().longestCommonPrefix(s = "abcd", t = "dcba") == 0

assert Solution().longestCommonPrefix(s = "aaaaaaaaaab", t = "aaaaaaaaaac") == 10

assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaxaca") == 7

assert Solution().longestCommonPrefix(s = "thisisaverylongstring", t = "thisisaverylongstr") == 18

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghik") == 9

assert Solution().longestCommonPrefix(s = "aabbccddeeffgghh", t = "aabbccddeeffggi") == 14

assert Solution().longestCommonPrefix(s = "longestprefix", t = "longestprexif") == 10

assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabcab") == 13

assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxy") == 25

assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabcabd") == 14

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefgxyz") == 7

assert Solution().longestCommonPrefix(s = "aaaaaabbbbb", t = "aaaaabbbbba") == 10

assert Solution().longestCommonPrefix(s = "abcdefabcdef", t = "abcdefabcde") == 11

assert Solution().longestCommonPrefix(s = "xylophone", t = "xylopho") == 7

assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabc") == 12

assert Solution().longestCommonPrefix(s = "aaaaab", t = "aaaabb") == 5

assert Solution().longestCommonPrefix(s = "prefixprefixprefix", t = "prefixprefixpre") == 15

assert Solution().longestCommonPrefix(s = "banana", t = "bananas") == 6

assert Solution().longestCommonPrefix(s = "oneoneoneone", t = "oneoneone") == 9

assert Solution().longestCommonPrefix(s = "aaaaabbbbb", t = "aaaaacbbbb") == 5

assert Solution().longestCommonPrefix(s = "aaaaabbbbbccccc", t = "aaaaabbbbbccc") == 13

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghijk") == 10

assert Solution().longestCommonPrefix(s = "aaaaaaabc", t = "aaaaaaaab") == 7

assert Solution().longestCommonPrefix(s = "xyxxyxyxyx", t = "xyxyxyxyyx") == 8

assert Solution().longestCommonPrefix(s = "aabbccddeeff", t = "aabbccddeegg") == 10

assert Solution().longestCommonPrefix(s = "abcabcabc", t = "abcabcabcd") == 9

assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcdexy") == 7

assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdeabcdf") == 9

assert Solution().longestCommonPrefix(s = "almostthesame", t = "almostthesam") == 12

assert Solution().longestCommonPrefix(s = "nocommonprefix", t = "different") == 0

assert Solution().longestCommonPrefix(s = "xyxyxyxyxy", t = "xyxyxyxyxz") == 9

assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabarb") == 10

assert Solution().longestCommonPrefix(s = "xyzzxyzz", t = "xyzzxyz") == 7

assert Solution().longestCommonPrefix(s = "qwertyuiop", t = "qwertyuio") == 9

assert Solution().longestCommonPrefix(s = "programming", t = "programmin") == 10

assert Solution().longestCommonPrefix(s = "abcdabcabc", t = "abcdabcdabcd") == 7

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghij") == 10

assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabacab") == 14

assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeff") == 12

assert Solution().longestCommonPrefix(s = "abcdefghij", t = "fedcba") == 0

assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcd", t = "abcdabcdabcdabcdabcd") == 20

assert Solution().longestCommonPrefix(s = "banana", t = "bananna") == 5

assert Solution().longestCommonPrefix(s = "hello", t = "hallo") == 1

assert Solution().longestCommonPrefix(s = "thisisatest", t = "thisisates") == 10

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdeghij") == 9

assert Solution().longestCommonPrefix(s = "abcdefgxyz", t = "xyzabcdefg") == 0

assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcdabcd", t = "abcdabcdabcdabcdabcdab") == 22

assert Solution().longestCommonPrefix(s = "aaaaaa", t = "aaaaa") == 5

assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcde") == 5

assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwx") == 24

assert Solution().longestCommonPrefix(s = "abcdabcdabcd", t = "abdcabcdabcd") == 3

assert Solution().longestCommonPrefix(s = "mississippi", t = "mississipp") == 10

assert Solution().longestCommonPrefix(s = "zzzzzzzzzz", t = "zzzzzzzzzz") == 10

assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdeabcdx") == 9

assert Solution().longestCommonPrefix(s = "xyzxyzxyz", t = "xyzxyzxyz") == 9

assert Solution().longestCommonPrefix(s = "aabbccddeeff", t = "aabbccddeeffgg") == 12

assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longstcommonprefix") == 18

assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonpre") == 16

assert Solution().longestCommonPrefix(s = "pneumonoultramicroscopicsilicovolcanoconiosis", t = "pneumonoultramicroscopicsilicovolcanoconio") == 42

assert Solution().longestCommonPrefix(s = "xylophone", t = "xylophones") == 9

assert Solution().longestCommonPrefix(s = "aaaaaaab", t = "aaaaaaabc") == 8

assert Solution().longestCommonPrefix(s = "racecar", t = "racecer") == 5

assert Solution().longestCommonPrefix(s = "abcde", t = "abc") == 3

assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdeghijklmnopqrstuvwxyz") == 25

assert Solution().longestCommonPrefix(s = "abcde", t = "fghij") == 0

assert Solution().longestCommonPrefix(s = "xyzabcd", t = "abcd") == 0

assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdebcde") == 9

assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcd", t = "abcdabcdabcdabcd") == 16

assert Solution().longestCommonPrefix(s = "hellohello", t = "hellohell") == 9

assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeffg") == 13

assert Solution().longestCommonPrefix(s = "hellohellohello", t = "hellohello") == 10

assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "fedcbaghi") == 0

assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabacabax") == 15

assert Solution().longestCommonPrefix(s = "xyzzzzzzzz", t = "xyzzzzzzzzz") == 10

assert Solution().longestCommonPrefix(s = "aaaaaaaaaabbbbbbbbbbcccccccccdddddddddeeeeeeeefffffffff", t = "aaaaaaaaaabbbbbbbbbbccccccccdddddddddeeeeeeeeffffffff") == 53

assert Solution().longestCommonPrefix(s = "aaaaaaa", t = "bbbbbbb") == 0

assert Solution().longestCommonPrefix(s = "xxxxxxxxxxxxxxy", t = "xxxxxxxxxxxxxyx") == 14

assert Solution().longestCommonPrefix(s = "mississippi", t = "mississippai") == 10

assert Solution().longestCommonPrefix(s = "abababababab", t = "babababababa") == 11

assert Solution().longestCommonPrefix(s = "xyxxyxyxyx", t = "xyxxyxyxy") == 9

assert Solution().longestCommonPrefix(s = "aabaa", t = "aaaaa") == 4

assert Solution().longestCommonPrefix(s = "abababab", t = "babababa") == 7

assert Solution().longestCommonPrefix(s = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", t = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyza") == 51

Answer: assert Solution().longestCommonPrefix(s = "one", t = "one") == 3

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "bcdef") == 4

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "fbcde") == 0

Answer: assert Solution().longestCommonPrefix(s = "xyz", t = "xya") == 2

Answer: assert Solution().longestCommonPrefix(s = "ababc", t = "abcab") == 2

Answer: assert Solution().longestCommonPrefix(s = "abcdef", t = "abcdfg") == 5

Answer: assert Solution().longestCommonPrefix(s = "pqr", t = "pqs") == 2

Answer: assert Solution().longestCommonPrefix(s = "abcdef", t = "fedcba") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdefg", t = "xyzabcdefg") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcd", t = "abcf") == 3

Answer: assert Solution().longestCommonPrefix(s = "abcdefg", t = "gfedcba") == 0

Answer: assert Solution().longestCommonPrefix(s = "ababab", t = "bababa") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "abcdf") == 4

Answer: assert Solution().longestCommonPrefix(s = "xyz", t = "xyw") == 2

Answer: assert Solution().longestCommonPrefix(s = "madxa", t = "madam") == 4

Answer: assert Solution().longestCommonPrefix(s = "aaaa", t = "aaaa") == 4

Answer: assert Solution().longestCommonPrefix(s = "abc", t = "def") == 0

Answer: assert Solution().longestCommonPrefix(s = "a", t = "b") == 0

Answer: assert Solution().longestCommonPrefix(s = "leetcode", t = "eetcode") == 7

Answer: assert Solution().longestCommonPrefix(s = "xyza", t = "xyz") == 3

Answer: assert Solution().longestCommonPrefix(s = "xyzabcdefg", t = "abcdefg") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcdefyz") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "abfde") == 2

Answer: assert Solution().longestCommonPrefix(s = "aaaaa", t = "aaaaa") == 5

Answer: assert Solution().longestCommonPrefix(s = "aabbcc", t = "aabbc") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghio") == 9

Answer: assert Solution().longestCommonPrefix(s = "ababababab", t = "bababababa") == 9

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghijxyz") == 10

Answer: assert Solution().longestCommonPrefix(s = "one", t = "two") == 0

Answer: assert Solution().longestCommonPrefix(s = "aaaaabbbbbcccccddddd", t = "aaaaabbbbbcccccdddd") == 19

Answer: assert Solution().longestCommonPrefix(s = "abcdefgh", t = "fghijklm") == 0

Answer: assert Solution().longestCommonPrefix(s = "palindrome", t = "palindromex") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdabcabc", t = "abcdabcdabc") == 7

Answer: assert Solution().longestCommonPrefix(s = "qwertyuiopasdfghjklzxcvbnm", t = "qwertyuiopasdfghjklzxcvbn") == 25

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghijz") == 10

Answer: assert Solution().longestCommonPrefix(s = "thisisaverylongstring", t = "thisisaverylongstrig") == 20

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "mississippix") == 11

Answer: assert Solution().longestCommonPrefix(s = "longestprefix", t = "longestpref") == 11

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabcabcabcabc", t = "abcabcabcabcabcabc") == 18

Answer: assert Solution().longestCommonPrefix(s = "zxyxzyzyzx", t = "zxyxzyzyzy") == 9

Answer: assert Solution().longestCommonPrefix(s = "pneumonoultramicroscopicsilicovolcanoconiosis", t = "pneumonoultramicroscopicsilicovolcanoconioz") == 42

Answer: assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabax") == 10

Answer: assert Solution().longestCommonPrefix(s = "longstringwithprefix", t = "longstringwithprifix") == 16

Answer: assert Solution().longestCommonPrefix(s = "repeatedcharacters", t = "repeatecharacters") == 17

Answer: assert Solution().longestCommonPrefix(s = "racecar", t = "racecars") == 7

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "misissippix") == 10

Answer: assert Solution().longestCommonPrefix(s = "sameprefix", t = "sameprefixsameprefix") == 10

Answer: assert Solution().longestCommonPrefix(s = "aaaaaabb", t = "aaaaabbb") == 7

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabcabcd") == 15

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabcabcabcabcabcabcabc", t = "abcabcabcabcabcabcabcabcabcab") == 29

Answer: assert Solution().longestCommonPrefix(s = "a", t = "a") == 1

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeffggg") == 14

Answer: assert Solution().longestCommonPrefix(s = "prefixsuffix", t = "prefixsufiix") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "edcba") == 0

Answer: assert Solution().longestCommonPrefix(s = "xyzabcxyzabcxyz", t = "xyzabcxyzabcyzz") == 14

Answer: assert Solution().longestCommonPrefix(s = "prefixsuffix", t = "prefixsuffixx") == 12

Answer: assert Solution().longestCommonPrefix(s = "removeonechar", t = "removeonecha") == 12

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghii") == 9

Answer: assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabrab") == 11

Answer: assert Solution().longestCommonPrefix(s = "hello", t = "hell") == 4

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghix") == 9

Answer: assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonprex") == 16

Answer: assert Solution().longestCommonPrefix(s = "abcdefgh", t = "abababab") == 2

Answer: assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabcabaa") == 14

Answer: assert Solution().longestCommonPrefix(s = "racecar", t = "racecarx") == 7

Answer: assert Solution().longestCommonPrefix(s = "xyxyxyxyxyxy", t = "yxxyxyxyxyxy") == 2

Answer: assert Solution().longestCommonPrefix(s = "zyxwvutsrqponmlkjihgfedcba", t = "zyxwvutsrqponmlkjihgfedcba") == 26

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabc", t = "abcabcabcab") == 11

Answer: assert Solution().longestCommonPrefix(s = "abc", t = "abcde") == 3

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "missisippi") == 10

Answer: assert Solution().longestCommonPrefix(s = "", t = "") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcd", t = "dabc") == 0

Answer: assert Solution().longestCommonPrefix(s = "sameprefixsame", t = "sameprefixsam") == 13

Answer: assert Solution().longestCommonPrefix(s = "aaaaaa", t = "bbbbbb") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdefgabcdefg", t = "abcdefgabcdxfg") == 11

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxyza") == 26

Answer: assert Solution().longestCommonPrefix(s = "abcdabcdabcd", t = "abcdabcdabce") == 11

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "mississipi") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghia") == 9

Answer: assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaxa") == 7

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "missisipi") == 8

Answer: assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaba") == 5

Answer: assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonpreffix") == 17

Answer: assert Solution().longestCommonPrefix(s = "abcd", t = "dcba") == 0

Answer: assert Solution().longestCommonPrefix(s = "aaaaaaaaaab", t = "aaaaaaaaaac") == 10

Answer: assert Solution().longestCommonPrefix(s = "abacaxaba", t = "abacaxaca") == 7

Answer: assert Solution().longestCommonPrefix(s = "thisisaverylongstring", t = "thisisaverylongstr") == 18

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghik") == 9

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeffgghh", t = "aabbccddeeffggi") == 14

Answer: assert Solution().longestCommonPrefix(s = "longestprefix", t = "longestprexif") == 10

Answer: assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabcab") == 13

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxy") == 25

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabcabd") == 14

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefgxyz") == 7

Answer: assert Solution().longestCommonPrefix(s = "aaaaaabbbbb", t = "aaaaabbbbba") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdefabcdef", t = "abcdefabcde") == 11

Answer: assert Solution().longestCommonPrefix(s = "xylophone", t = "xylopho") == 7

Answer: assert Solution().longestCommonPrefix(s = "abcabcabcabcabc", t = "abcabcabcabc") == 12

Answer: assert Solution().longestCommonPrefix(s = "aaaaab", t = "aaaabb") == 5

Answer: assert Solution().longestCommonPrefix(s = "prefixprefixprefix", t = "prefixprefixpre") == 15

Answer: assert Solution().longestCommonPrefix(s = "banana", t = "bananas") == 6

Answer: assert Solution().longestCommonPrefix(s = "oneoneoneone", t = "oneoneone") == 9

Answer: assert Solution().longestCommonPrefix(s = "aaaaabbbbb", t = "aaaaacbbbb") == 5

Answer: assert Solution().longestCommonPrefix(s = "aaaaabbbbbccccc", t = "aaaaabbbbbccc") == 13

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "abcdefghijk") == 10

Answer: assert Solution().longestCommonPrefix(s = "aaaaaaabc", t = "aaaaaaaab") == 7

Answer: assert Solution().longestCommonPrefix(s = "xyxxyxyxyx", t = "xyxyxyxyyx") == 8

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeff", t = "aabbccddeegg") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcabcabc", t = "abcabcabcd") == 9

Answer: assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcdexy") == 7

Answer: assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdeabcdf") == 9

Answer: assert Solution().longestCommonPrefix(s = "almostthesame", t = "almostthesam") == 12

Answer: assert Solution().longestCommonPrefix(s = "nocommonprefix", t = "different") == 0

Answer: assert Solution().longestCommonPrefix(s = "xyxyxyxyxy", t = "xyxyxyxyxz") == 9

Answer: assert Solution().longestCommonPrefix(s = "abracadabra", t = "abracadabarb") == 10

Answer: assert Solution().longestCommonPrefix(s = "xyzzxyzz", t = "xyzzxyz") == 7

Answer: assert Solution().longestCommonPrefix(s = "qwertyuiop", t = "qwertyuio") == 9

Answer: assert Solution().longestCommonPrefix(s = "programming", t = "programmin") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdabcabc", t = "abcdabcdabcd") == 7

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdefghij") == 10

Answer: assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabacab") == 14

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeff") == 12

Answer: assert Solution().longestCommonPrefix(s = "abcdefghij", t = "fedcba") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcd", t = "abcdabcdabcdabcdabcd") == 20

Answer: assert Solution().longestCommonPrefix(s = "banana", t = "bananna") == 5

Answer: assert Solution().longestCommonPrefix(s = "hello", t = "hallo") == 1

Answer: assert Solution().longestCommonPrefix(s = "thisisatest", t = "thisisates") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "abcdeghij") == 9

Answer: assert Solution().longestCommonPrefix(s = "abcdefgxyz", t = "xyzabcdefg") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcdabcd", t = "abcdabcdabcdabcdabcdab") == 22

Answer: assert Solution().longestCommonPrefix(s = "aaaaaa", t = "aaaaa") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcdexyz", t = "abcde") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwx") == 24

Answer: assert Solution().longestCommonPrefix(s = "abcdabcdabcd", t = "abdcabcdabcd") == 3

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "mississipp") == 10

Answer: assert Solution().longestCommonPrefix(s = "zzzzzzzzzz", t = "zzzzzzzzzz") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdeabcdx") == 9

Answer: assert Solution().longestCommonPrefix(s = "xyzxyzxyz", t = "xyzxyzxyz") == 9

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeff", t = "aabbccddeeffgg") == 12

Answer: assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longstcommonprefix") == 18

Answer: assert Solution().longestCommonPrefix(s = "longestcommonprefix", t = "longestcommonpre") == 16

Answer: assert Solution().longestCommonPrefix(s = "pneumonoultramicroscopicsilicovolcanoconiosis", t = "pneumonoultramicroscopicsilicovolcanoconio") == 42

Answer: assert Solution().longestCommonPrefix(s = "xylophone", t = "xylophones") == 9

Answer: assert Solution().longestCommonPrefix(s = "aaaaaaab", t = "aaaaaaabc") == 8

Answer: assert Solution().longestCommonPrefix(s = "racecar", t = "racecer") == 5

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "abc") == 3

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdeghijklmnopqrstuvwxyz") == 25

Answer: assert Solution().longestCommonPrefix(s = "abcde", t = "fghij") == 0

Answer: assert Solution().longestCommonPrefix(s = "xyzabcd", t = "abcd") == 0

Answer: assert Solution().longestCommonPrefix(s = "abcdeabcde", t = "abcdebcde") == 9

Answer: assert Solution().longestCommonPrefix(s = "abcdabcdabcdabcdabcd", t = "abcdabcdabcdabcd") == 16

Answer: assert Solution().longestCommonPrefix(s = "hellohello", t = "hellohell") == 9

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeffgg", t = "aabbccddeeffg") == 13

Answer: assert Solution().longestCommonPrefix(s = "hellohellohello", t = "hellohello") == 10

Answer: assert Solution().longestCommonPrefix(s = "abcdefghijk", t = "fedcbaghi") == 0

Answer: assert Solution().longestCommonPrefix(s = "abacabadabacaba", t = "abacabadabacabax") == 15

Answer: assert Solution().longestCommonPrefix(s = "xyzzzzzzzz", t = "xyzzzzzzzzz") == 10

Answer: assert Solution().longestCommonPrefix(s = "aaaaaaaaaabbbbbbbbbbcccccccccdddddddddeeeeeeeefffffffff", t = "aaaaaaaaaabbbbbbbbbbccccccccdddddddddeeeeeeeeffffffff") == 53

Answer: assert Solution().longestCommonPrefix(s = "aaaaaaa", t = "bbbbbbb") == 0

Answer: assert Solution().longestCommonPrefix(s = "xxxxxxxxxxxxxxy", t = "xxxxxxxxxxxxxyx") == 14

Answer: assert Solution().longestCommonPrefix(s = "mississippi", t = "mississippai") == 10

Answer: assert Solution().longestCommonPrefix(s = "abababababab", t = "babababababa") == 11

Answer: assert Solution().longestCommonPrefix(s = "xyxxyxyxyx", t = "xyxxyxyxy") == 9

Answer: assert Solution().longestCommonPrefix(s = "aabaa", t = "aaaaa") == 4

Answer: assert Solution().longestCommonPrefix(s = "abababab", t = "babababa") == 7

Answer: assert Solution().longestCommonPrefix(s = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", t = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyza") == 51

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
    def longestCommonPrefix(self, s: str, t: str) -> int:
        n, m = len(s), len(t)
        i = j = 0
        rem = False
        while i < n and j < m:
            if s[i] != t[j]:
                if rem:
                    break
                rem = True
            else:
                j += 1
            i += 1
        return j

class Solution:
    def longestCommonPrefix(self, s: str, t: str) -> int:


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:

For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.
Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.

Return true if the final two digits in s are the same; otherwise, return false.

Example 1:

Input: s = "3902"
Output: true
Explanation:

Initially, s = "3902"
First operation:

(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
s becomes "292"

Second operation:

(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
s becomes "11"

Since the digits in "11" are the same, the output is true.

Example 2:

Input: s = "34789"
Output: false
Explanation:

Initially, s = "34789".
After the first operation, s = "7157".
After the second operation, s = "862".
After the third operation, s = "48".
Since '4' != '8', the output is false.

Constraints:

3 <= s.length <= 105
s consists of only digits.

Your solution to the problem should be a method of the class Solution called hasSameDigits and should pass all tests. Use the following starter code:
```python
class Solution:
    def hasSameDigits(self, s: str) -> bool:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:

For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.
Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.

Return true if the final two digits in s are the same; otherwise, return false.

Example 1:

Input: s = "3902"
Output: true
Explanation:

Initially, s = "3902"
First operation:

(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
s becomes "292"

Second operation:

(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
s becomes "11"

Since the digits in "11" are the same, the output is true.

Example 2:

Input: s = "34789"
Output: false
Explanation:

Initially, s = "34789".
After the first operation, s = "7157".
After the second operation, s = "862".
After the third operation, s = "48".
Since '4' != '8', the output is false.

Constraints:

3 <= s.length <= 105
s consists of only digits.

Your solution to the problem should be a method of the class Solution called hasSameDigits and should pass all tests. Use the following starter code:
```python
class Solution:
    def hasSameDigits(self, s: str) -> bool:
        # Add your code here
```

SOLUTION:

assert Solution().hasSameDigits(s = "9876543210") == False

assert Solution().hasSameDigits(s = "111") == True

assert Solution().hasSameDigits(s = "1234567890") == False

assert Solution().hasSameDigits(s = "898989898") == True

assert Solution().hasSameDigits(s = "1111111111") == True

assert Solution().hasSameDigits(s = "99999") == True

assert Solution().hasSameDigits(s = "999999999") == True

assert Solution().hasSameDigits(s = "3902") == True

assert Solution().hasSameDigits(s = "000000000") == True

assert Solution().hasSameDigits(s = "121212121") == True

assert Solution().hasSameDigits(s = "11111") == True

assert Solution().hasSameDigits(s = "34789") == False

assert Solution().hasSameDigits(s = "5555555555") == True

assert Solution().hasSameDigits(s = "555555") == True

assert Solution().hasSameDigits(s = "1357913579") == False

assert Solution().hasSameDigits(s = "1010101010") == True

assert Solution().hasSameDigits(s = "0000000000") == True

assert Solution().hasSameDigits(s = "909090") == True

assert Solution().hasSameDigits(s = "98765") == False

assert Solution().hasSameDigits(s = "9999999999") == True

assert Solution().hasSameDigits(s = "1212121212") == True

assert Solution().hasSameDigits(s = "00000") == True

assert Solution().hasSameDigits(s = "2468024680") == False

assert Solution().hasSameDigits(s = "555555555") == True

assert Solution().hasSameDigits(s = "121212") == True

assert Solution().hasSameDigits(s = "123456789") == False

assert Solution().hasSameDigits(s = "12121212121212121212") == True

assert Solution().hasSameDigits(s = "8642086420") == False

assert Solution().hasSameDigits(s = "13579135791357913579") == False

assert Solution().hasSameDigits(s = "98765432") == False

assert Solution().hasSameDigits(s = "44444444444444444444") == True

assert Solution().hasSameDigits(s = "1919191919") == True

assert Solution().hasSameDigits(s = "1357924680135792468") == False

assert Solution().hasSameDigits(s = "99887766554433221100") == False

assert Solution().hasSameDigits(s = "3333333333333") == True

assert Solution().hasSameDigits(s = "54321098765432109876") == False

assert Solution().hasSameDigits(s = "86420864208642086420") == False

assert Solution().hasSameDigits(s = "123321123321") == True

assert Solution().hasSameDigits(s = "555555555555555555") == True

assert Solution().hasSameDigits(s = "97531975319753197531") == False

assert Solution().hasSameDigits(s = "9080706050") == False

assert Solution().hasSameDigits(s = "1122334455") == False

assert Solution().hasSameDigits(s = "111222333444555") == True

assert Solution().hasSameDigits(s = "82828282828282828282") == True

assert Solution().hasSameDigits(s = "88888888888888888888") == True

assert Solution().hasSameDigits(s = "01234567890123456789") == False

assert Solution().hasSameDigits(s = "11223344556677889900") == False

assert Solution().hasSameDigits(s = "1020304050") == False

assert Solution().hasSameDigits(s = "1357924680") == False

assert Solution().hasSameDigits(s = "1234567890123") == False

assert Solution().hasSameDigits(s = "12345678901234567890") == False

assert Solution().hasSameDigits(s = "0246813579") == False

assert Solution().hasSameDigits(s = "98765432109876543210") == False

assert Solution().hasSameDigits(s = "987654321") == False

assert Solution().hasSameDigits(s = "36363636363636363636") == True

assert Solution().hasSameDigits(s = "12345678") == False

assert Solution().hasSameDigits(s = "59595959595959595959") == True

assert Solution().hasSameDigits(s = "13579246801") == False

assert Solution().hasSameDigits(s = "9182736450") == False

assert Solution().hasSameDigits(s = "24680246802468024680") == False

assert Solution().hasSameDigits(s = "0246802468024680246") == False

assert Solution().hasSameDigits(s = "594872631478523690") == False

assert Solution().hasSameDigits(s = "19191919191919191919") == True

assert Solution().hasSameDigits(s = "9876543210987654321") == False

assert Solution().hasSameDigits(s = "9876543210987") == False

assert Solution().hasSameDigits(s = "9182736451") == False

assert Solution().hasSameDigits(s = "98765432109876") == False

assert Solution().hasSameDigits(s = "1111111111111") == True

assert Solution().hasSameDigits(s = "90909090909090909090") == True

assert Solution().hasSameDigits(s = "0101010101") == True

assert Solution().hasSameDigits(s = "111222333444") == False

assert Solution().hasSameDigits(s = "121212121212121212") == True

assert Solution().hasSameDigits(s = "55555555555555555555") == True

assert Solution().hasSameDigits(s = "2222222222222") == True

assert Solution().hasSameDigits(s = "2468013579") == False

assert Solution().hasSameDigits(s = "8080808080") == True

assert Solution().hasSameDigits(s = "77777777777777777777") == True

assert Solution().hasSameDigits(s = "30303030303030303030") == True

assert Solution().hasSameDigits(s = "99009900990099009900") == False

assert Solution().hasSameDigits(s = "09876543210987654321") == False

assert Solution().hasSameDigits(s = "10101010101010101010") == True

Answer: assert Solution().hasSameDigits(s = "9876543210") == False

Answer: assert Solution().hasSameDigits(s = "111") == True

Answer: assert Solution().hasSameDigits(s = "1234567890") == False

Answer: assert Solution().hasSameDigits(s = "898989898") == True

Answer: assert Solution().hasSameDigits(s = "1111111111") == True

Answer: assert Solution().hasSameDigits(s = "99999") == True

Answer: assert Solution().hasSameDigits(s = "999999999") == True

Answer: assert Solution().hasSameDigits(s = "3902") == True

Answer: assert Solution().hasSameDigits(s = "000000000") == True

Answer: assert Solution().hasSameDigits(s = "121212121") == True

Answer: assert Solution().hasSameDigits(s = "11111") == True

Answer: assert Solution().hasSameDigits(s = "34789") == False

Answer: assert Solution().hasSameDigits(s = "5555555555") == True

Answer: assert Solution().hasSameDigits(s = "555555") == True

Answer: assert Solution().hasSameDigits(s = "1357913579") == False

Answer: assert Solution().hasSameDigits(s = "1010101010") == True

Answer: assert Solution().hasSameDigits(s = "0000000000") == True

Answer: assert Solution().hasSameDigits(s = "909090") == True

Answer: assert Solution().hasSameDigits(s = "98765") == False

Answer: assert Solution().hasSameDigits(s = "9999999999") == True

Answer: assert Solution().hasSameDigits(s = "1212121212") == True

Answer: assert Solution().hasSameDigits(s = "00000") == True

Answer: assert Solution().hasSameDigits(s = "2468024680") == False

Answer: assert Solution().hasSameDigits(s = "555555555") == True

Answer: assert Solution().hasSameDigits(s = "121212") == True

Answer: assert Solution().hasSameDigits(s = "123456789") == False

Answer: assert Solution().hasSameDigits(s = "12121212121212121212") == True

Answer: assert Solution().hasSameDigits(s = "8642086420") == False

Answer: assert Solution().hasSameDigits(s = "13579135791357913579") == False

Answer: assert Solution().hasSameDigits(s = "123123123123123123123123123") == False

Answer: assert Solution().hasSameDigits(s = "98765432") == False

Answer: assert Solution().hasSameDigits(s = "44444444444444444444") == True

Answer: assert Solution().hasSameDigits(s = "1919191919") == True

Answer: assert Solution().hasSameDigits(s = "54321098765432109876543210") == False

Answer: assert Solution().hasSameDigits(s = "10101010101010101010101010") == True

Answer: assert Solution().hasSameDigits(s = "1357924680135792468") == False

Answer: assert Solution().hasSameDigits(s = "99887766554433221100") == False

Answer: assert Solution().hasSameDigits(s = "5959595959595959595959595959") == True

Answer: assert Solution().hasSameDigits(s = "3333333333333") == True

Answer: assert Solution().hasSameDigits(s = "9988776655443322110099887766") == False

Answer: assert Solution().hasSameDigits(s = "54321098765432109876") == False

Answer: assert Solution().hasSameDigits(s = "999000999000999000999000") == False

Answer: assert Solution().hasSameDigits(s = "101010101010101010101010101010") == True

Answer: assert Solution().hasSameDigits(s = "86420864208642086420") == False

Answer: assert Solution().hasSameDigits(s = "123321123321") == True

Answer: assert Solution().hasSameDigits(s = "555555555555555555") == True

Answer: assert Solution().hasSameDigits(s = "97531975319753197531") == False

Answer: assert Solution().hasSameDigits(s = "9080706050") == False

Answer: assert Solution().hasSameDigits(s = "999888777666555444333222111000") == False

Answer: assert Solution().hasSameDigits(s = "1234321123432112343211234321") == True

Answer: assert Solution().hasSameDigits(s = "1122334455") == False

Answer: assert Solution().hasSameDigits(s = "111222333444555") == True

Answer: assert Solution().hasSameDigits(s = "82828282828282828282") == True

Answer: assert Solution().hasSameDigits(s = "88888888888888888888") == True

Answer: assert Solution().hasSameDigits(s = "00112233445566778899001122") == False

Answer: assert Solution().hasSameDigits(s = "01234567890123456789") == False

Answer: assert Solution().hasSameDigits(s = "505050505050505050505050505050") == True

Answer: assert Solution().hasSameDigits(s = "012345678901234567890123456789") == False

Answer: assert Solution().hasSameDigits(s = "56789012345678901234567890") == False

Answer: assert Solution().hasSameDigits(s = "11223344556677889900") == False

Answer: assert Solution().hasSameDigits(s = "1020304050") == False

Answer: assert Solution().hasSameDigits(s = "0909090909090909090909090909") == True

Answer: assert Solution().hasSameDigits(s = "1357924680") == False

Answer: assert Solution().hasSameDigits(s = "1234567890123") == False

Answer: assert Solution().hasSameDigits(s = "12345678901234567890") == False

Answer: assert Solution().hasSameDigits(s = "0246813579") == False

Answer: assert Solution().hasSameDigits(s = "98765432109876543210") == False

Answer: assert Solution().hasSameDigits(s = "987654321") == False

Answer: assert Solution().hasSameDigits(s = "36363636363636363636") == True

Answer: assert Solution().hasSameDigits(s = "12345678") == False

Answer: assert Solution().hasSameDigits(s = "59595959595959595959") == True

Answer: assert Solution().hasSameDigits(s = "111222333444555666777888999") == True

Answer: assert Solution().hasSameDigits(s = "1010101010101010101010101010") == True

Answer: assert Solution().hasSameDigits(s = "987987987987987987987987987") == False

Answer: assert Solution().hasSameDigits(s = "13579246801") == False

Answer: assert Solution().hasSameDigits(s = "9012345678901234567890123456") == False

Answer: assert Solution().hasSameDigits(s = "9182736450") == False

Answer: assert Solution().hasSameDigits(s = "24680246802468024680") == False

Answer: assert Solution().hasSameDigits(s = "0246802468024680246") == False

Answer: assert Solution().hasSameDigits(s = "594872631478523690") == False

Answer: assert Solution().hasSameDigits(s = "19191919191919191919") == True

Answer: assert Solution().hasSameDigits(s = "9876543210987654321") == False

Answer: assert Solution().hasSameDigits(s = "9876543210987") == False

Answer: assert Solution().hasSameDigits(s = "9182736451") == False

Answer: assert Solution().hasSameDigits(s = "98765432109876") == False

Answer: assert Solution().hasSameDigits(s = "111222333444555666777888999000") == False

Answer: assert Solution().hasSameDigits(s = "1111111111111") == True

Answer: assert Solution().hasSameDigits(s = "90909090909090909090") == True

Answer: assert Solution().hasSameDigits(s = "0101010101") == True

Answer: assert Solution().hasSameDigits(s = "1928374655647382910918273645") == False

Answer: assert Solution().hasSameDigits(s = "111222333444") == False

Answer: assert Solution().hasSameDigits(s = "5555555555555555555555555555") == True

Answer: assert Solution().hasSameDigits(s = "121212121212121212") == True

Answer: assert Solution().hasSameDigits(s = "55555555555555555555") == True

Answer: assert Solution().hasSameDigits(s = "123321123321123321123321123321") == True

Answer: assert Solution().hasSameDigits(s = "1212121212121212121212121212") == True

Answer: assert Solution().hasSameDigits(s = "2222222222222") == True

Answer: assert Solution().hasSameDigits(s = "50505050505050505050505050") == True

Answer: assert Solution().hasSameDigits(s = "87654321098765432109876543210") == False

Answer: assert Solution().hasSameDigits(s = "2468013579") == False

Answer: assert Solution().hasSameDigits(s = "8080808080") == True

Answer: assert Solution().hasSameDigits(s = "918273645191827364519182736451") == False

Answer: assert Solution().hasSameDigits(s = "77777777777777777777") == True

Answer: assert Solution().hasSameDigits(s = "111222333444555666777888999000111") == False

Answer: assert Solution().hasSameDigits(s = "30303030303030303030") == True

Answer: assert Solution().hasSameDigits(s = "99009900990099009900") == False

Answer: assert Solution().hasSameDigits(s = "09876543210987654321") == False

Answer: assert Solution().hasSameDigits(s = "10101010101010101010") == True

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
  # Same as 3461. Check If Digits Are Equal in String After Operations I
  def hasSameDigits(self, s: str) -> bool:
    n = len(s)
    num1 = 0
    num2 = 0

    for i in range(n - 1):
      coefficient = self._nCMOD10(n - 2, i)
      num1 += (coefficient * (int(s[i]) - 0)) % 10
      num1 %= 10
      num2 += (coefficient * (int(s[i + 1]) - 0)) % 10
      num2 %= 10

    return num1 == num2

  def _nCMOD10(self, n: int, k: int) -> int:
    """Returns (n, k) % 10."""
    mod2 = self._lucasTheorem(n, k, 2)
    mod5 = self._lucasTheorem(n, k, 5)
    lookup = [
        [0, 6, 2, 8, 4],  # mod2 == 0
        [5, 1, 7, 3, 9]   # mod2 == 1
    ]
    return lookup[mod2][mod5]

  def _lucasTheorem(self, n: int, k: int, prime: int) -> int:
    """Returns (n, k) % prime."""
    res = 1
    while n > 0 or k > 0:
      nMod = n % prime
      MOD = k % prime
      res *= math.comb(nMod, MOD)
      res %= prime
      n //= prime
      k //= prime
    return res

class Solution:
    def hasSameDigits(self, s: str) -> bool:


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given an integer side, representing the edge length of a square with corners at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
You are also given a positive integer k and a 2D integer array points, where points[i] = [xi, yi] represents the coordinate of a point lying on the boundary of the square.
You need to select k elements among points such that the minimum Manhattan distance between any two points is maximized.
Return the maximum possible minimum Manhattan distance between the selected k points.
The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

Example 1:

Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
Output: 2
Explanation:

Select all four points.

Example 2:

Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
Output: 1
Explanation:

Select the points (0, 0), (2, 0), (2, 2), and (2, 1).

Example 3:

Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
Output: 1
Explanation:

Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).

Constraints:

1 <= side <= 109
4 <= points.length <= min(4 * side, 15 * 103)
points[i] == [xi, yi]
The input is generated such that:

points[i] lies on the boundary of the square.
All points[i] are unique.

4 <= k <= min(25, points.length)

Your solution to the problem should be a method of the class Solution called maxDistance and should pass all tests. Use the following starter code:
```python
class Solution:
    def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given an integer side, representing the edge length of a square with corners at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
You are also given a positive integer k and a 2D integer array points, where points[i] = [xi, yi] represents the coordinate of a point lying on the boundary of the square.
You need to select k elements among points such that the minimum Manhattan distance between any two points is maximized.
Return the maximum possible minimum Manhattan distance between the selected k points.
The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.

Example 1:

Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
Output: 2
Explanation:

Select all four points.

Example 2:

Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
Output: 1
Explanation:

Select the points (0, 0), (2, 0), (2, 2), and (2, 1).

Example 3:

Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
Output: 1
Explanation:

Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).

Constraints:

1 <= side <= 109
4 <= points.length <= min(4 * side, 15 * 103)
points[i] == [xi, yi]
The input is generated such that:

points[i] lies on the boundary of the square.
All points[i] are unique.

4 <= k <= min(25, points.length)

Your solution to the problem should be a method of the class Solution called maxDistance and should pass all tests. Use the following starter code:
```python
class Solution:
    def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

Answer: assert Solution().maxDistance(side = 4, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,4],[2,4],[3,4],[4,4],[4,3],[4,2],[4,1],[4,0],[3,0],[2,0],[1,0]], k = 8) == 2

Answer: assert Solution().maxDistance(side = 3, points = [[0,0],[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[2,2]], k = 4) == 1

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,4],[2,4],[3,4],[4,4],[4,3],[4,2],[4,1],[4,0],[3,0],[2,0],[1,0]], k = 8) == 1

Answer: assert Solution().maxDistance(side = 3, points = [[0,0],[0,1],[0,2],[0,3],[3,0],[3,1],[3,2],[3,3],[1,3],[2,3]], k = 6) == 1

Answer: assert Solution().maxDistance(side = 3, points = [[0,0],[0,1],[0,2],[0,3],[1,3],[2,3],[3,3],[3,2],[3,1],[3,0],[2,0],[1,0]], k = 6) == 2

Answer: assert Solution().maxDistance(side = 3, points = [[0,0],[0,1],[0,2],[0,3],[1,0],[1,3],[2,0],[2,3],[3,0],[3,1],[3,2],[3,3]], k = 6) == 2

Answer: assert Solution().maxDistance(side = 4, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[4,0],[4,1],[4,2],[4,3],[4,4],[1,4],[2,4],[3,4]], k = 7) == 2

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,5],[0,10],[5,0],[5,10],[10,0],[10,5],[10,10],[2,3],[7,8],[3,7],[8,2]], k = 6) == 5

Answer: assert Solution().maxDistance(side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4) == 1

Answer: assert Solution().maxDistance(side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5) == 1

Answer: assert Solution().maxDistance(side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4) == 2

Answer: assert Solution().maxDistance(side = 4, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[4,0],[4,1],[4,2],[4,3],[4,4],[1,4],[2,4],[3,4],[1,0],[2,0],[3,0]], k = 8) == 2

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9],[9,9],[9,8],[9,7],[9,6],[9,5],[9,4],[9,3],[9,2],[9,1],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 24) == 1

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[12,0],[12,1],[12,2],[12,3],[12,4],[12,5],[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[1,12],[2,12],[3,12],[4,12],[5,12],[6,12],[7,12],[8,12],[9,12],[10,12],[11,12],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0]], k = 20) == 2

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,4],[2,4],[3,4],[4,4],[4,3],[4,2],[4,1],[4,0],[3,0],[2,0],[1,0],[0,3],[1,3],[2,3],[3,3],[0,2],[1,2],[3,2],[4,2],[2,1]], k = 15) == 0

Answer: assert Solution().maxDistance(side = 20, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[20,0],[20,1],[20,2],[20,3],[20,4],[20,5],[20,6],[20,7],[20,8],[20,9],[20,10],[20,11],[20,12],[20,13],[20,14],[20,15],[20,16],[20,17],[20,18],[20,19],[20,20],[1,20],[2,20],[3,20],[4,20],[5,20],[6,20],[7,20],[8,20],[9,20],[10,20],[11,20],[12,20],[13,20],[14,20],[15,20],[16,20],[17,20],[18,20],[19,20],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0],[13,0],[14,0],[15,0],[16,0],[17,0],[18,0],[19,0]], k = 20) == 4

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[10,10],[10,9],[10,8],[10,7],[10,6],[10,5],[10,4],[10,3],[10,2],[10,1],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 10) == 4

Answer: assert Solution().maxDistance(side = 7, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[1,7],[2,7],[3,7],[4,7],[5,7],[6,7],[7,7],[7,6],[7,5],[7,4],[7,3],[7,2],[7,1],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 15) == 1

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,4],[2,0],[2,4],[3,0],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4],[1,1],[1,3],[3,1],[3,3]], k = 10) == 0

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[9,0],[9,1],[9,2],[9,3],[9,4],[9,5],[9,6],[9,7],[9,8],[9,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[8,1],[8,2],[8,3],[8,4],[8,5],[8,6],[8,7],[8,8]], k = 15) == 2

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9],[9,9],[9,8],[9,7],[9,6],[9,5],[9,4],[9,3],[9,2],[9,1],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 18) == 2

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8],[8,7],[8,6],[8,5],[8,4],[8,3],[8,2],[8,1],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7],[3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],[4,1],[4,2],[4,3],[4,4],[4,5],[4,6],[4,7],[5,1],[5,2],[5,3],[5,4],[5,5],[5,6],[5,7],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6],[6,7],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6],[7,7]], k = 20) == 1

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9],[9,9],[9,8],[9,7],[9,6],[9,5],[9,4],[9,3],[9,2],[9,1],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 20) == 1

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,4],[2,4],[3,4],[4,4],[4,3],[4,2],[4,1],[4,0],[3,0],[2,0],[1,0],[2,1],[2,2],[2,3],[3,2]], k = 15) == 0

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,12],[12,0],[12,12],[0,6],[6,0],[6,12],[12,6],[3,0],[3,12],[9,0],[9,12],[0,3],[0,9],[12,3],[12,9],[6,3],[6,9],[3,3],[3,9],[9,3],[9,9]], k = 10) == 3

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[4,0],[4,1],[4,2],[4,3],[4,4],[1,4],[2,4],[3,4],[1,0],[2,0],[3,0],[1,2],[2,2],[3,2],[1,1],[2,1],[3,1],[1,3],[2,3],[3,3]], k = 10) == 0

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,5],[0,10],[1,0],[1,10],[2,0],[2,10],[3,0],[3,10],[4,0],[4,10],[5,0],[5,2],[5,4],[5,6],[5,8],[5,10],[6,0],[6,10],[7,0],[7,10],[8,0],[8,10],[9,0],[9,10],[10,0],[10,1],[10,2],[10,3],[10,4],[10,5],[10,6],[10,7],[10,8],[10,9],[10,10]], k = 10) == 3

Answer: assert Solution().maxDistance(side = 15, points = [[0,0],[0,15],[15,0],[15,15],[7,0],[7,15],[0,7],[15,7],[5,5],[5,10],[10,5],[10,10],[3,3],[3,12],[12,3],[12,12],[6,0],[6,15],[9,0],[9,15],[0,6],[0,9],[15,6],[15,9],[8,8],[7,7],[7,8],[8,7]], k = 15) == 1

Answer: assert Solution().maxDistance(side = 7, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[6,0],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6],[6,7],[1,7],[2,7],[3,7],[4,7],[5,7],[1,0],[2,0],[3,0],[4,0],[5,0],[1,1],[2,1],[3,1],[4,1],[5,1],[1,2],[2,2],[3,2],[4,2],[5,2],[1,3],[2,3],[3,3],[4,3],[5,3],[1,4],[2,4],[3,4],[4,4],[5,4],[1,5],[2,5],[3,5],[4,5],[5,5],[1,6],[2,6],[3,6],[4,6],[5,6]], k = 20) == 1

Answer: assert Solution().maxDistance(side = 15, points = [[0,0],[0,15],[15,0],[15,15],[0,7],[7,0],[7,15],[15,7]], k = 4) == 15

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,10],[10,0],[10,10],[0,5],[5,0],[5,10],[10,5],[2,0],[3,0],[4,0],[6,0],[7,0],[8,0],[9,0],[2,10],[3,10],[4,10],[6,10],[7,10],[8,10],[9,10],[0,2],[0,3],[0,4],[0,6],[0,7],[0,8],[0,9],[10,2],[10,3],[10,4],[10,6],[10,7],[10,8],[10,9],[2,2],[2,3],[2,4],[2,6],[2,7],[2,8],[3,2],[3,3],[3,4],[3,6],[3,7],[3,8],[4,2],[4,3],[4,4],[4,6],[4,7],[4,8],[6,2],[6,3],[6,4],[6,6],[6,7],[6,8],[7,2],[7,3],[7,4],[7,6],[7,7],[7,8],[8,2],[8,3],[8,4],[8,6],[8,7],[8,8]], k = 15) == 2

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8],[8,7],[8,6],[8,5],[8,4],[8,3],[8,2],[8,1],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0],[0,7],[1,7],[2,7],[3,7],[4,7],[5,7],[6,7],[7,7],[8,7],[0,6],[1,6],[2,6],[3,6],[4,6],[5,6],[6,6],[7,6],[8,6]], k = 18) == 0

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[1,12],[2,12],[3,12],[4,12],[5,12],[6,12],[7,12],[8,12],[9,12],[10,12],[11,12],[12,12],[12,11],[12,10],[12,9],[12,8],[12,7],[12,6],[12,5],[12,4],[12,3],[12,2],[12,1],[12,0],[11,0],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 20) == 2

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,6],[2,6],[3,6],[4,6],[5,6],[6,6],[6,5],[6,4],[6,3],[6,2],[6,1],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[10,10],[10,9],[10,8],[10,7],[10,6],[10,5],[10,4],[10,3],[10,2],[10,1],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 15) == 2

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[1,5],[2,5],[3,5],[4,5],[5,5],[5,4],[5,3],[5,2],[5,1],[5,0],[4,0],[3,0],[2,0],[1,0],[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,1],[4,2],[4,3],[4,4]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8],[8,7],[8,6],[8,5],[8,4],[8,3],[8,2],[8,1],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 18) == 1

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[10,0],[10,1],[10,2],[10,3],[10,4],[10,5],[10,6],[10,7],[10,8],[10,9],[10,10],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10]], k = 5) == 8

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[6,0],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6],[1,6],[2,6],[3,6],[4,6],[5,6],[1,0],[2,0],[3,0],[4,0],[5,0],[1,1],[1,2],[1,3],[1,4],[1,5],[5,1],[5,2],[5,3],[5,4],[5,5]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8],[8,7],[8,6],[8,5],[8,4],[8,3],[8,2],[8,1],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 16) == 2

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[12,0],[12,1],[12,2],[12,3],[12,4],[12,5],[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[1,12],[2,12],[3,12],[4,12],[5,12],[6,12],[7,12],[8,12],[9,12],[10,12],[11,12]], k = 25) == 1

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[1,12],[2,12],[3,12],[4,12],[5,12],[6,12],[7,12],[8,12],[9,12],[10,12],[11,12],[12,12],[12,11],[12,10],[12,9],[12,8],[12,7],[12,6],[12,5],[12,4],[12,3],[12,2],[12,1],[12,0],[11,0],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 24) == 2

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[8,0],[8,1],[8,2],[8,3],[8,4],[8,5],[8,6],[8,7],[8,8],[8,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[1,2],[2,2],[3,2],[4,2],[5,2],[6,2],[7,2],[1,3],[2,3],[3,3],[4,3],[5,3],[6,3],[7,3],[1,4],[2,4],[3,4],[4,4],[5,4],[6,4],[7,4],[1,5],[2,5],[3,5],[4,5],[5,5],[6,5],[7,5],[1,6],[2,6],[3,6],[4,6],[5,6],[6,6],[7,6],[1,7],[2,7],[3,7],[4,7],[5,7],[6,7],[7,7],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8]], k = 24) == 1

Answer: assert Solution().maxDistance(side = 12, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[1,0],[1,12],[2,0],[2,12],[3,0],[3,12],[4,0],[4,12],[5,0],[5,12],[6,0],[6,12],[7,0],[7,12],[8,0],[8,12],[9,0],[9,12],[10,0],[10,12],[11,0],[11,12],[12,0],[12,1],[12,2],[12,3],[12,4],[12,5],[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[1,1],[1,11],[11,1],[11,11],[2,2],[2,10],[10,2],[10,10],[3,3],[3,9],[9,3],[9,9],[4,4],[4,8],[8,4],[8,8],[5,5],[5,7],[7,5],[7,7],[6,6]], k = 30) == 1

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[1,5],[2,5],[3,5],[4,5],[5,5],[5,4],[5,3],[5,2],[5,1],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[5,0],[5,1],[5,2],[5,3],[5,4],[5,5],[1,5],[2,5],[3,5],[4,5],[1,0],[2,0],[3,0],[4,0],[1,2],[2,2],[3,2],[4,2],[1,3],[2,3],[3,3],[4,3],[1,4],[2,4],[3,4],[4,4]], k = 15) == 0

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[8,8],[8,7],[8,6],[8,5],[8,4],[8,3],[8,2],[8,1],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 12) == 2

Answer: assert Solution().maxDistance(side = 20, points = [[0,0],[0,20],[20,0],[20,20],[0,10],[10,0],[10,20],[20,10],[5,0],[5,20],[15,0],[15,20],[10,5],[10,15],[5,5],[5,15],[15,5],[15,15]], k = 6) == 10

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,9],[9,0],[9,9],[0,3],[0,6],[3,0],[6,0],[3,9],[6,9],[9,3],[9,6],[3,3],[3,6],[6,3],[6,6]], k = 8) == 3

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[1,0],[1,5],[2,0],[2,5],[3,0],[3,5],[4,0],[4,5],[5,0],[5,1],[5,2],[5,3],[5,4],[5,5],[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,1],[4,2],[4,3],[4,4]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[10,0],[10,1],[10,2],[10,3],[10,4],[10,5],[10,6],[10,7],[10,8],[10,9],[10,10],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0]], k = 16) == 2

Answer: assert Solution().maxDistance(side = 15, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[1,15],[2,15],[3,15],[4,15],[5,15],[6,15],[7,15],[8,15],[9,15],[10,15],[11,15],[12,15],[13,15],[14,15],[15,15],[15,14],[15,13],[15,12],[15,11],[15,10],[15,9],[15,8],[15,7],[15,6],[15,5],[15,4],[15,3],[15,2],[15,1],[15,0],[14,0],[13,0],[12,0],[11,0],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 25) == 2

Answer: assert Solution().maxDistance(side = 20, points = [[0,0],[0,20],[20,0],[20,20],[5,0],[5,20],[15,0],[15,20],[10,0],[10,20],[0,5],[20,5],[0,15],[20,15],[5,5],[15,5],[5,15],[15,15],[10,5],[10,15],[5,10],[15,10],[10,10]], k = 12) == 5

Answer: assert Solution().maxDistance(side = 7, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6],[7,7],[1,7],[2,7],[3,7],[4,7],[5,7],[6,7]], k = 10) == 2

Answer: assert Solution().maxDistance(side = 5, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[1,0],[1,1],[1,2],[1,3],[1,4],[2,0],[2,1],[2,2],[2,3],[2,4],[3,0],[3,1],[3,2],[3,3],[3,4],[4,0],[4,1],[4,2],[4,3],[4,4]], k = 9) == 1

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,0],[1,6],[2,0],[2,6],[3,0],[3,6],[4,0],[4,6],[5,0],[5,6],[6,0],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6],[1,1],[1,5],[5,1],[5,5],[2,2],[2,4],[4,2],[4,4],[3,3]], k = 15) == 1

Answer: assert Solution().maxDistance(side = 20, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[0,11],[0,12],[0,13],[0,14],[0,15],[0,16],[0,17],[0,18],[0,19],[0,20],[20,0],[20,1],[20,2],[20,3],[20,4],[20,5],[20,6],[20,7],[20,8],[20,9],[20,10],[20,11],[20,12],[20,13],[20,14],[20,15],[20,16],[20,17],[20,18],[20,19],[20,20],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0],[13,0],[14,0],[15,0],[16,0],[17,0],[18,0],[19,0],[1,20],[2,20],[3,20],[4,20],[5,20],[6,20],[7,20],[8,20],[9,20],[10,20],[11,20],[12,20],[13,20],[14,20],[15,20],[16,20],[17,20],[18,20],[19,20]], k = 30) == 2

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[7,0],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6],[7,7],[1,7],[2,7],[3,7],[4,7],[5,7],[6,7],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0]], k = 12) == 1

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[10,10],[10,9],[10,8],[10,7],[10,6],[10,5],[10,4],[10,3],[10,2],[10,1],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 20) == 2

Answer: assert Solution().maxDistance(side = 10, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[0,10],[1,10],[2,10],[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[10,10],[10,9],[10,8],[10,7],[10,6],[10,5],[10,4],[10,3],[10,2],[10,1],[10,0],[9,0],[8,0],[7,0],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 25) == 1

Answer: assert Solution().maxDistance(side = 8, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[8,0],[8,1],[8,2],[8,3],[8,4],[8,5],[8,6],[8,7],[8,8],[1,8],[2,8],[3,8],[4,8],[5,8],[6,8],[7,8],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0]], k = 12) == 2

Answer: assert Solution().maxDistance(side = 7, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[1,0],[1,7],[2,0],[2,7],[3,0],[3,7],[4,0],[4,7],[5,0],[5,7],[6,0],[6,7],[7,0],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6],[7,7],[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[4,1],[4,2],[4,3],[4,4],[4,5],[4,6],[5,1],[5,2],[5,3],[5,4],[5,5],[5,6],[6,1],[6,2],[6,3],[6,4],[6,5],[6,6]], k = 15) == 1

Answer: assert Solution().maxDistance(side = 9, points = [[0,0],[0,9],[9,0],[9,9],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[1,8],[2,7],[3,6],[4,5],[5,4],[6,3],[7,2],[8,1]], k = 10) == 0

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,6],[2,6],[3,6],[4,6],[5,6],[6,6],[6,5],[6,4],[6,3],[6,2],[6,1],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0]], k = 12) == 2

Answer: assert Solution().maxDistance(side = 6, points = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,6],[2,6],[3,6],[4,6],[5,6],[6,6],[6,5],[6,4],[6,3],[6,2],[6,1],[6,0],[5,0],[4,0],[3,0],[2,0],[1,0],[1,1],[1,2],[1,3],[1,4],[1,5],[2,1],[2,2],[2,3],[2,4],[2,5],[3,1],[3,2],[3,3],[3,4],[3,5],[4,1],[4,2],[4,3],[4,4],[4,5],[5,1],[5,2],[5,3],[5,4],[5,5]], k = 12) == 2

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

from dataclasses import dataclass

@dataclass(frozen=True)
class Sequence:
  startX: int
  startY: int
  endX: int
  endY: int
  length: int

  def __iter__(self):
    yield self.startX
    yield self.startY
    yield self.endX
    yield self.endY
    yield self.length

class Solution:
  def maxDistance(self, side: int, points: list[list[int]], k: int) -> int:
    ordered = self._getOrderedPoints(side, points)

    def isValidDistance(m: int) -> bool:
      """
      Returns True if we can select `k` points such that the minimum Manhattan
      distance between any two consecutive chosen points is at least `m`.
      """
      dq = collections.deque([Sequence(*ordered[0], *ordered[0], 1)])
      maxLength = 1

      for i in range(1, len(ordered)):
        x, y = ordered[i]
        startX, startY = ordered[i]
        length = 1
        while dq and abs(x - dq[0].endX) + abs(y - dq[0].endY) >= m:
          if (abs(x - dq[0].startX) + abs(y - dq[0].startY) >= m
                  and dq[0].length + 1 >= length):
            startX = dq[0].startX
            startY = dq[0].startY
            length = dq[0].length + 1
            maxLength = max(maxLength, length)
          dq.popleft()
        dq.append(Sequence(startX, startY, x, y, length))

      return maxLength >= k

    l = 0
    r = side

    while l < r:
      m = (l + r + 1) // 2
      if isValidDistance(m):
        l = m
      else:
        r = m - 1

    return l

  def _getOrderedPoints(self, side: int, points: list[list[int]]) -> list[list[int]]:
    """
    Returns the ordered points on the perimeter of a square of side length
    `side`, starting from left, top, right, and bottom boundaries.
    """
    left = sorted([(x, y) for x, y in points if x == 0 and y > 0])
    top = sorted([(x, y) for x, y in points if x > 0 and y == side])
    right = sorted([(x, y) for x, y in points if x == side and y < side],
                   reverse=True)
    bottom = sorted([(x, y) for x, y in points if y == 0], reverse=True)
    return left + top + right + bottom