---
title: "ByteByteGo: Matrix Pathways"
url: https://perrotta.dev/2025/11/bytebytego-matrix-pathways/
last_updated: 2026-01-03
---


[ByteByteGo: Matrix Pathways](https://bytebytego.com/exercises/coding-patterns/dynamic-programming/matrix-pathways):

```python
def matrix_pathways(m: int, n: int) -> int:
    from functools import lru_cache

    @lru_cache(maxsize=None)
    def solve(m, n):
        # from 1 to m, from 1 to n
        assert (m, n) >= (0, 0)

        if m == 0 or n == 0:
            return 0

        if m == 1 and n == 1:
            return 1

        return solve(m - 1, n) + solve(m, n - 1)

    return solve(m, n)
```

