giulio98/xlcost-single-prompt huggingface dataset:
# print unique rows in a given boolean matrix | given a binary matrix of m x n of integers , you need to return only unique rows of binary array ; the main function that prints all unique rows in a given matrix . ; traverse through the matrix ; check if there is similar column is already printed , i . e if i and jth column match . ; if no row is similar ; print the row ; 

ROW = 4
COL = 5


def findUniqueRows(M):
    for i in range(ROW):
        flag = 0
        for j in range(i):
            flag = 1
            for k in range(COL):
                if (M[i][k] != M[j][k]):
                    flag = 0
            if (flag == 1):
                break
        if (flag == 0):
            for j in range(COL):
                print(M[i][j], end=" ")
            print()


if __name__ == '__main__':
    M = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 0, 1, 0, 0]]
    findUniqueRows(M)

# Expected output:
0 1 0 0 1 
1 0 1 1 0 
1 0 1 0 0
# print unique rows in a given boolean matrix | python3 code to print unique row in a given binary matrix ; 
def printArray(matrix):
    rowCount = len(matrix)
    if rowCount == 0:
        return
    columnCount = len(matrix[0])
    if columnCount == 0:
        return
    row_output_format = " ".join(["%s"] * columnCount)
    printed = {}
    for row in matrix:
        routput = row_output_format % tuple(row)
        if routput not in printed:
            printed[routput] = True
            print(routput)


mat = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 1, 1, 0, 0]]
printArray(mat)

# Expected output:
0 1 0 0 1
1 0 1 1 0
1 1 1 0 0
# find the largest rectangle of 1 's with swapping of columns allowed | python 3 program to find the largest rectangle of 1 's with swapping of columns allowed. ; returns area of the largest rectangle of 1 's ; an auxiliary array to store count of consecutive 1 's in every column. ; step 1 : fill the auxiliary array hist [ ] [ ] ; first row in hist [ ] [ ] is copy of first row in mat [ ] [ ] ; fill remaining rows of hist [ ] [ ] ; step 2 : sort rows of hist [ ] [ ] in non - increasing order ; counting occurrence ; traverse the count array from right side ; step 3 : traverse the sorted hist [ ] [ ] to find maximum area ; since values are in decreasing order , the area ending with cell ( i , j ) can be obtained by multiplying column number with value of hist [ i ] [ j ] ; 

R = 3
C = 5


def maxArea(mat):
    hist = [[0 for i in range(C + 1)]for i in range(R + 1)]
    for i in range(0, C, 1):
        hist[0][i] = mat[0][i]
        for j in range(1, R, 1):
            if ((mat[j][i] == 0)):
                hist[j][i] = 0
            else:
                hist[j][i] = hist[j - 1][i] + 1
    for i in range(0, R, 1):
        count = [0 for i in range(R + 1)]
        for j in range(0, C, 1):
            count[hist[i][j]] += 1
        col_no = 0
        j = R
        while (j >= 0):
            if (count[j] > 0):
                for k in range(0, count[j], 1):
                    hist[i][col_no] = j
                    col_no += 1
            j -= 1
    max_area = 0
    for i in range(0, R, 1):
        for j in range(0, C, 1):
            curr_area = (j + 1) * hist[i][j]
            if (curr_area > max_area):
                max_area = curr_area
    return max_area


if __name__ == '__main__':
    mat = [[0, 1, 0, 1, 0], [0, 1, 0, 1, 1], [1, 1, 0, 1, 0]]
    print("Area of the largest rectangle is", maxArea(mat))

# Expected output:
Area of the largest rectangle is 6
# find sum of all elements in a matrix except the elements in row and / or column of given cell ? | a structure to represent a cell index ; r is row , varies from 0 to r - 1 ; c is column , varies from 0 to c - 1 ; a simple solution to find sums for a given array of cell indexes ; iterate through all cell indexes ; compute sum for current cell index ; 

class Cell:
    def __init__(self, r, c):
        self .r = r
        self .c = c


def printSums(mat, arr, n):
    for i in range(0, n):
        Sum = 0
        r = arr[i].r
        c = arr[i].c
        for j in range(0, R):
            for k in range(0, C):
                if j != r and k != c:
                    Sum += mat[j][k]
        print(Sum)


if __name__ == "__main__":
    mat = [[1, 1, 2], [3, 4, 6], [5, 3, 2]]
    R = C = 3
    arr = [Cell(0, 0), Cell(1, 1), Cell(0, 1)]
    n = len(arr)
    printSums(mat, arr, n)

# Expected output:
15
10
16
# find sum of all elements in a matrix except the elements in row and / or column of given cell ? | python3 implementation of the approach a structure to represent a cell index ; r is row , varies from 0 to r - 1 ; c is column , varies from 0 to c - 1 ; compute sum of all elements , sum of every row and sum every column ; compute the desired sum for all given cell indexes ; 

class Cell:
    def __init__(self, r, c):
        self .r = r
        self .c = c


def printSums(mat, arr, n):
    Sum = 0
    row, col = [0] * R, [0] * C
    for i in range(0, R):
        for j in range(0, C):
            Sum += mat[i][j]
            row[i] += mat[i][j]
            col[j] += mat[i][j]
    for i in range(0, n):
        r0, c0 = arr[i].r, arr[i].c
        print(Sum - row[r0] - col[c0] + mat[r0][c0])


if __name__ == "__main__":
    mat = [[1, 1, 2], [3, 4, 6], [5, 3, 2]]
    R = C = 3
    arr = [Cell(0, 0), Cell(1, 1), Cell(0, 1)]
    n = len(arr)
    printSums(mat, arr, n)

# Expected output:
15
10
16
# count number of islands where every island is row | this function takes a matrix of ' x ' and ' o ' and returns the number of rectangular islands of ' x ' where no two islands are row - wise or column - wise adjacent , the islands may be diagonaly adjacent ; initialize result ; traverse the input matrix ; if current cell is ' x ' , then check whether this is top - leftmost of a rectangle . if yes , then increment count ; 
def countIslands(mat):
    count = 0
    for i in range(0, M):
        for j in range(0, N):
            if (mat[i][j] == 'X'):
                if ((i == 0 or mat[i - 1][j] == 'O')
                        and (j == 0 or mat[i][j - 1] == 'O')):
                    count = count + 1
    return count


M = 6
N = 3
mat = [['O', 'O', 'O'], ['X', 'X', 'O'], ['X', 'X', 'O'],
       ['O', 'O', 'X'], ['O', 'O', 'X'], ['X', 'X', 'O']]
print("Number of rectangular islands is", countIslands(mat))

# Expected output:
Number of rectangular islands is 3
# find a common element in all rows of a given row | specify number of rows and columns ; returns common element in all rows of mat [ m ] [ n ] . if there is no common element , then - 1 is returned ; an array to store indexes of current last column ; initialize min_row as first row ; keep finding min_row in current last column , till either all elements of last column become same or we hit first column . ; find minimum in current last column ; eq_count is count of elements equal to minimum in current last column . ; traverse current last column elements again to update it ; decrease last column index of a row whose value is more than minimum . ; reduce last column index by 1 ; if equal count becomes m , return the value ; 

M = 4
N = 5


def findCommon(mat):
    column = [N - 1] * M
    min_row = 0
    while (column[min_row] >= 0):
        for i in range(M):
            if (mat[i][column[i]] < mat[min_row][column[min_row]]):
                min_row = i
        eq_count = 0
        for i in range(M):
            if (mat[i][column[i]] > mat[min_row][column[min_row]]):
                if (column[i] == 0):
                    return -1
                column[i] -= 1
            else:
                eq_count += 1
        if (eq_count == M):
            return mat[min_row][column[min_row]]
    return -1


if __name__ == "__main__":
    mat = [[1, 2, 3, 4, 5], [2, 4, 5, 8, 10],
           [3, 5, 7, 9, 11], [1, 3, 5, 7, 9]]
    result = findCommon(mat)
    if (result == -1):
        print("No common element")
    else:
        print("Common element is", result)

# Expected output:
Common element is 5
# find a common element in all rows of a given row | python3 implementation of the approach ; specify number of rows and columns ; returns common element in all rows of mat [ m ] [ n ] . if there is no common element , then - 1 is returned ; a hash map to store count of elements ; increment the count of first element of the row ; starting from the second element of the current row ; if current element is different from the previous element i . e . it is appearing for the first time in the current row ; find element having count equal to number of rows ; no such element found ; 

from collections import defaultdict
M = 4
N = 5


def findCommon(mat):
    global M
    global N
    cnt = dict()
    cnt = defaultdict(lambda: 0, cnt)
    i = 0
    j = 0
    while (i < M):
        cnt[mat[i][0]] = cnt[mat[i][0]] + 1
        j = 1
        while (j < N):
            if (mat[i][j] != mat[i][j - 1]):
                cnt[mat[i][j]] = cnt[mat[i][j]] + 1
            j = j + 1
        i = i + 1
    for ele in cnt:
        if (cnt[ele] == M):
            return ele
    return -1


mat = [[1, 2, 3, 4, 5], [2, 4, 5, 8, 10], [3, 5, 7, 9, 11], [1, 3, 5, 7, 9], ]
result = findCommon(mat)
if (result == -1):
    print("No common element")
else:
    print("Common element is ", result)

# Expected output:
Common element is  5
# given a matrix of ‘ o ’ and ‘ x ’ , replace ' o ' with ' x ' if surrounded by ' x ' | size of given matrix is m x n ; a recursive function to replace previous value ' prevv ' at ' ( x , ▁ y ) ' and all surrounding values of ( x , y ) with new value ' newv ' . ; base cases ; replace the color at ( x , y ) ; recur for north , east , south and west ; returns size of maximum size subsquare matrix surrounded by 'x   ; step 1 : replace all ' o ' s with '-   ; call floodfill for all ' - ' lying on edges left side ; right side ; top side ; bottom side ; step 3 : replace all ' - ' with 'x   ; 

M = 6
N = 6


def floodFillUtil(mat, x, y, prevV, newV):
    if (x < 0 or x >= M or y < 0 or y >= N):
        return
    if (mat[x][y] != prevV):
        return
    mat[x][y] = newV
    floodFillUtil(mat, x + 1, y, prevV, newV)
    floodFillUtil(mat, x - 1, y, prevV, newV)
    floodFillUtil(mat, x, y + 1, prevV, newV)
    floodFillUtil(mat, x, y - 1, prevV, newV)


def replaceSurrounded(mat):
    for i in range(M):
        for j in range(N):
            if (mat[i][j] == 'O'):
                mat[i][j] = '-'
    for i in range(M):
        if (mat[i][0] == '-'):
            floodFillUtil(mat, i, 0, '-', 'O')
    for i in range(M):
        if (mat[i][N - 1] == '-'):
            floodFillUtil(mat, i, N - 1, '-', 'O')
    for i in range(N):
        if (mat[0][i] == '-'):
            floodFillUtil(mat, 0, i, '-', 'O')
    for i in range(N):
        if (mat[M - 1][i] == '-'):
            floodFillUtil(mat, M - 1, i, '-', 'O')
    for i in range(M):
        for j in range(N):
            if (mat[i][j] == '-'):
                mat[i][j] = 'X'


if __name__ == '__main__':
    mat = [
        [
            'X', 'O', 'X', 'O', 'X', 'X'], [
            'X', 'O', 'X', 'X', 'O', 'X'], [
                'X', 'X', 'X', 'O', 'X', 'X'], [
                    'O', 'X', 'X', 'X', 'X', 'X'], [
                        'X', 'X', 'X', 'O', 'X', 'O'], [
                            'O', 'O', 'X', 'O', 'O', 'O']]
    replaceSurrounded(mat)
    for i in range(M):
        print(*mat[i])

# Expected output:
X O X O X X
X O X X X X
X X X X X X
O X X X X X
X X X O X O
O O X O O O
# given a matrix of ' o ' and ' x ' , find the largest subsquare surrounded by ' x ' | size of given matrix is n x n ; initialize maxside with 0 ; fill the dp matrix horizontally . for contiguous ' x ' increment the value of x , otherwise make it 0 ; fill the dp matrix vertically . for contiguous ' x ' increment the value of y , otherwise make it 0 ; now check , for every value of ( i , j ) if sub - square is possible , traverse back horizontally by value val , and check if vertical contiguous ' x ' enfing at ( i , j - val + 1 ) is greater than equal to val . similarly , check if traversing back vertically , the horizontal contiguous ' x ' ending at ( i - val + 1 , j ) is greater than equal to val . ; store the final answer in maxval ; return the final answe . ; 

N = 6


def maximumSubSquare(arr):
    dp = [[[-1, -1]for i in range(51)]for j in range(51)]
    maxside = [[0 for i in range(51)]for j in range(51)]
    x = 0
    y = 0
    for i in range(N):
        x = 0
        for j in range(N):
            if (arr[i][j] == 'X'):
                x += 1
            else:
                x = 0
            dp[i][j][0] = x
    for i in range(N):
        for j in range(N):
            if (arr[j][i] == 'X'):
                y += 1
            else:
                y = 0
            dp[j][i][1] = y
    maxval = 0
    val = 0
    for i in range(N):
        for j in range(N):
            val = min(dp[i][j][0], dp[i][j][1])
            if (dp[i][j - val + 1][1] >= val and dp[i - val + 1][j][0] >= val):
                maxside[i][j] = val
            else:
                maxside[i][j] = 0
            maxval = max(maxval, maxside[i][j])
    return maxval


mat = [
    [
        'X', 'O', 'X', 'X', 'X', 'X'], [
            'X', 'O', 'X', 'X', 'O', 'X'], [
                'X', 'X', 'X', 'O', 'O', 'X'], [
                    'O', 'X', 'X', 'X', 'X', 'X'], [
                        'X', 'X', 'X', 'O', 'X', 'O'], [
                            'O', 'O', 'X', 'O', 'O', 'O']]
print(maximumSubSquare(mat))

# Expected output:
4
# flood fill algorithm | dimentions of paint screen ; a recursive function to replace previous color ' prevc ' at ' ( x , ▁ y ) ' and all surrounding pixels of ( x , y ) with new color ' newc ' and ; base cases ; replace the color at ( x , y ) ; recur for north , east , south and west ; it mainly finds the previous color on ( x , y ) and calls floodfillutil ( ) ; 

M = 8
N = 8


def floodFillUtil(screen, x, y, prevC, newC):
    if (x < 0 or x >= M or y < 0 or y >=
            N or screen[x][y] != prevC or screen[x][y] == newC):
        return
    screen[x][y] = newC
    floodFillUtil(screen, x + 1, y, prevC, newC)
    floodFillUtil(screen, x - 1, y, prevC, newC)
    floodFillUtil(screen, x, y + 1, prevC, newC)
    floodFillUtil(screen, x, y - 1, prevC, newC)


def floodFill(screen, x, y, newC):
    prevC = screen[x][y]
    floodFillUtil(screen, x, y, prevC, newC)


screen = [
    [
        1, 1, 1, 1, 1, 1, 1, 1], [
            1, 1, 1, 1, 1, 1, 0, 0], [
                1, 0, 0, 1, 1, 0, 1, 1], [
                    1, 2, 2, 2, 2, 0, 1, 0], [
                        1, 1, 1, 2, 2, 0, 1, 0], [
                            1, 1, 1, 2, 2, 2, 2, 0], [
                                1, 1, 1, 1, 1, 2, 1, 1], [
                                    1, 1, 1, 1, 1, 2, 2, 1]]
x = 4
y = 4
newC = 3
floodFill(screen, x, y, newC)
print("Updated screen after call to floodFill:")
for i in range(M):
    for j in range(N):
        print(screen[i][j], end=' ')
    print()

# Expected output:
Updated screen after call to floodFill:
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 
1 0 0 1 1 0 1 1 
1 3 3 3 3 0 1 0 
1 1 1 3 3 0 1 0 
1 1 1 3 3 3 3 0 
1 1 1 1 1 3 1 1 
1 1 1 1 1 3 3 1
# collect maximum points in a grid using two traversals | a memoization based program to find maximum collection using two traversals of a grid ; checks whether a given input is valid or not ; driver function to collect max value ; if p1 or p2 is at an invalid cell ; if both traversals reach their destinations ; if both traversals are at last row but not at their destination ; if subproblem is already solved ; initialize answer for this subproblem ; this variable is used to store gain of current cell ( s ) ; recur for all possible cases , then store and return the one with max value ; this is mainly a wrapper over recursive function getmaxutil ( ) . this function creates a table for memoization and calls getmaxutil ( ) ; create a memoization table and initialize all entries as - 1 ; calculation maximum value using memoization based function getmaxutil ( ) ; 

R = 5
C = 4
intmin = -10000000
intmax = 10000000


def isValid(x, y1, y2):
    return ((x >= 0 and x < R and y1 >= 0 and y1 < C and y2 >= 0 and y2 < C))


def getMaxUtil(arr, mem, x, y1, y2):
    if isValid(x, y1, y2) == False:
        return intmin
    if x == R - 1 and y1 == 0 and y2 == C - 1:
        if y1 == y2:
            return arr[x][y1]
        else:
            return arr[x][y1] + arr[x][y2]
    if x == R - 1:
        return intmin
    if mem[x][y1][y2] != -1:
        return mem[x][y1][y2]
    ans = intmin
    temp = 0
    if y1 == y2:
        temp = arr[x][y1]
    else:
        temp = arr[x][y1] + arr[x][y2]
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1, y2 - 1))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1, y2 + 1))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1, y2))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 - 1, y2))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 - 1, y2 - 1))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 - 1, y2 + 1))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 + 1, y2))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 + 1, y2 - 1))
    ans = max(ans, temp + getMaxUtil(arr, mem, x + 1, y1 + 1, y2 + 1))
    mem[x][y1][y2] = ans
    return ans


def geMaxCollection(arr):
    mem = [[[-1 for i in range(C)]for i in range(C)]for i in range(R)]
    return getMaxUtil(arr, mem, 0, 0, C - 1)


if __name__ == '__main__':
    arr = [[3, 6, 8, 2], [5, 2, 4, 3], [1, 1, 20, 10],
           [1, 1, 20, 10], [1, 1, 20, 10], ]
    print('Maximum collection is ', geMaxCollection(arr))

# Expected output:
Maximum collection is  73
# collect maximum coins before hitting a dead end | a naive recursive python 3 program to find maximum number of coins that can be collected before hitting a dead end ; to check whether current cell is out of the grid or not ; dir = 0 for left , dir = 1 for facing right . this function returns number of maximum coins that can be collected starting from ( i , j ) . ; if this is a invalid cell or if cell is a blocking cell ; check if this cell contains the coin ' c ' or if its empty ' e ' . ; get the maximum of two cases when you are facing right in this cell ; direction is right ; direction is left get the maximum of two cases when you are facing left in this cell ; 

R = 5
C = 5


def isValid(i, j):

    return (i >= 0 and i < R and j >= 0 and j < C)


def maxCoinsRec(arr, i, j, dir):
    if (isValid(i, j) == False or arr[i][j] == '#'):
        return 0
    if (arr[i][j] == 'C'):
        result = 1
    else:
        result = 0

    if (dir == 1):

        return (result + max(maxCoinsRec(arr, i+1, j, 0),
                             maxCoinsRec(arr, i, j+1, 1)))

    return (result + max(maxCoinsRec(arr, i+1, j, 1),
                         maxCoinsRec(arr, i, j-1, 0)))


if __name__ == '__main__':
    arr = [['E', 'C', 'C', 'C', 'C'],
           ['C', '#', 'C', '#', 'E'],
           ['#', 'C', 'C', '#', 'C'],
           ['C', 'E', 'E', 'C', 'E'],
           ['C', 'E', '#', 'C', 'E']]

    print("Maximum number of collected coins is ", maxCoinsRec(arr, 0, 0, 1))

# Expected output:
Maximum number of collected coins is  8
# find length of the longest consecutive path from a given starting character | python3 program to find the longest consecutive path ; tool matrices to recur for adjacent cells . ; dp [ i ] [ j ] stores length of longest consecutive path starting at arr [ i ] [ j ] . ; check whether mat [ i ] [ j ] is a valid cell or not . ; check whether current character is adjacent to previous character ( character processed in parent call ) or not . ; i , j are the indices of the current cell and prev is the character processed in the parent call . . also mat [ i ] [ j ] is our current character . ; if this cell is not valid or current character is not adjacent to previous one ( e . g . d is not adjacent to b ) or if this cell is already included in the path than return 0. ; if this subproblem is already solved , return the answer ; initialize answer ; recur for paths with different adjacent cells and store the length of longest path . ; save the answer and return ; returns length of the longest path with all characters consecutive to each other . this function first initializes dp array that is used to store results of subproblems , then it calls recursive dfs based function getlenutil ( ) to find max length path ; check for each possible starting point ; recur for all eight adjacent cells ; 

R = 3
C = 3
x = [0, 1, 1, -1, 1, 0, -1, -1]
y = [1, 0, 1, 1, -1, -1, 0, -1]
dp = [[0 for i in range(C)]for i in range(R)]


def isvalid(i, j):
    if (i < 0 or j < 0 or i >= R or j >= C):
        return False
    return True


def isadjacent(prev, curr):
    if (ord(curr) - ord(prev)) == 1:
        return True
    return False


def getLenUtil(mat, i, j, prev):
    if (isvalid(i, j) == False or isadjacent(prev, mat[i][j]) == False):
        return 0
    if (dp[i][j] != -1):
        return dp[i][j]
    ans = 0
    for k in range(8):
        ans = max(ans, 1 + getLenUtil(mat, i + x[k], j + y[k], mat[i][j]))
    dp[i][j] = ans
    return dp[i][j]


def getLen(mat, s):
    for i in range(R):
        for j in range(C):
            dp[i][j] = -1
    ans = 0
    for i in range(R):
        for j in range(C):
            if (mat[i][j] == s):
                for k in range(8):
                    ans = max(ans, 1 + getLenUtil(mat, i + x[k], j + y[k], s))
    return ans


mat = [['a', 'c', 'd'], ['h', 'b', 'a'], ['i', 'g', 'f']]
print(getLen(mat, 'a'))
print(getLen(mat, 'e'))
print(getLen(mat, 'b'))
print(getLen(mat, 'f'))

# Expected output:
4
0
3
4
# find the longest path in a matrix with given constraints | python3 program to find the longest path in a matrix with given constraints ; returns length of the longest path beginning with mat [ i ] [ j ] . this function mainly uses lookup table dp [ n ] [ n ] ; base case ; if this subproblem is already solved ; to store the path lengths in all the four directions ; since all numbers are unique and in range from 1 to n * n , there is atmost one possible direction from any cell ; if none of the adjacent fours is one greater we will take 1 otherwise we will pick maximum from all the four directions ; returns length of the longest path beginning with any cell ; initialize result ; create a lookup table and fill all entries in it as - 1 ; compute longest path beginning from all cells ; update result if needed ; 

n = 3


def findLongestFromACell(i, j, mat, dp):
    if (i < 0 or i >= n or j < 0 or j >= n):
        return 0
    if (dp[i][j] != -1):
        return dp[i][j]
    x, y, z, w = -1, -1, -1, -1
    if (j < n - 1 and ((mat[i][j] + 1) == mat[i][j + 1])):
        x = 1 + findLongestFromACell(i, j + 1, mat, dp)
    if (j > 0 and (mat[i][j] + 1 == mat[i][j - 1])):
        y = 1 + findLongestFromACell(i, j - 1, mat, dp)
    if (i > 0 and (mat[i][j] + 1 == mat[i - 1][j])):
        z = 1 + findLongestFromACell(i - 1, j, mat, dp)
    if (i < n - 1 and (mat[i][j] + 1 == mat[i + 1][j])):
        w = 1 + findLongestFromACell(i + 1, j, mat, dp)
    dp[i][j] = max(x, max(y, max(z, max(w, 1))))
    return dp[i][j]


def finLongestOverAll(mat):
    result = 1
    dp = [[-1 for i in range(n)]for i in range(n)]
    for i in range(n):
        for j in range(n):
            if (dp[i][j] == -1):
                findLongestFromACell(i, j, mat, dp)
            result = max(result, dp[i][j])
    return result


mat = [[1, 2, 9], [5, 3, 8], [4, 6, 7]]
print("Length of the longest path is ", finLongestOverAll(mat))

# Expected output:
Length of the longest path is  4