# Find the Length of Maximum Path in Given Matrix for Each Index

In this article, we will learn how to determine the length of maximum path in given matrix for each index.

In this tutorial, a square matrix mat[][] of size m X n is provided, with each entry being either 0 or 1. If an element has the value 1, it is connected; if it has the value 0, it is not connected. In a binary matrix, our purpose is to know the longest possible path.

The maximum length path on the matrix, or all elements in the matrix, must be found in order to solve the challenge. We shall change no more than one 0 to 1 before determining the path.

Let's use an illustration to better comprehend the issue.

Input:

Output:

```3
```

Analysis

To maximize the path length, we can change 0 at index (0, 1) or (1, 0).

### Solution Strategy

Determining the length after transforming each 0 to 1 is a simple solution to the problem. We will conduct a depth first search to determine the path length and then return the maximum of all path lengths.

An efficient strategy would be to avoid performing many conversions and instead choose the one that provides the most promising answer. We'll find a group in which changing one 0 to 1 returns the longest path.

A code to demonstrate how our solution works,

Python Program:

Output:

```The length of largest path is 3
```

So that's the end of the article. I sincerely hope you find this post to be educational and useful.

### Feedback   