Not able to pass one test case in MARLA

I am not able to pass one test case from the Second subtask in MARLA. I am getting runtime error. Link to my solution: https://www.codechef.com/viewsolution/33231172

access denied?

# cook your dish here

import sys
import math
input = sys.stdin.readline

############ ---- Input Functions ---- ############
# inp — For taking integer inputs.
def inp():
    return(int(input()))
 #inlt — For taking List inputs   
def inlt():
    return(list(map(int,input().split())))
#insr — For taking string inputs. Actually it returns a List of Characters
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
#invr — For taking space seperated integer variable inputs.
def invr():
    return(map(int,input().split()))
def f(n,m,grid):
    if n == 1 and m == 1:
        return([grid[0][0],1])
    def dfs(i,j,val):
        if i<0 or j<0 or i>=n or j>=m:
           return(0)
        #print(val)
        if grid[i][j] != val:
            return(0)
        elif grid[i][j] == val:
            grid[i][j] = -1
            return(1 + dfs(i,j-1,val)+dfs(i+1,j,val)+dfs(i,j+1,val)+dfs(i-1,j,val))
    d = {}
    for r in range(n):
        for c in range(m):
            #print(r,c)
        
            if grid[r][c] != -1:
                v = grid[r][c]
                
                count = dfs(r,c,v)
                
                
                if v in d.keys():
                    d[v] = max(count,d[v])
                else:
                    d[v] = count
            else:
                pass
            
    #print(d)

    d = sorted(d.items(), key=lambda item: (-item[1],item[0]))
    key = d[0][0]
    val = d[0][1]
    return([key,val])

xk = list(map(int, input().rstrip().split()))
n = xk[0]
m = xk[1]
grid = []
for i in range(n):
    grid.append(inlt())
result = f(n,m,grid) 
print(result[0],result[1])Preformatted text

Have you tried a case with a 1000x1000 grid with only ones? That would probably exhaust the default recursion limit for python.

1 Like

Thank you so much. My answer got accepted after increasing the recursion limit.