For some reason I kept getting runtime error on first case, rest of the cases worked perfectly fine and I am unable to identify what is wrong with my solution. It would be appreciative if someone could assist.
def connected_components(board, N, M):
visited = [[False for _ in range(M)] for _ in range(N)]
components = []
for i in range(N):
for j in range(M):
if(board[i][j] == '1' and visited[i][j] == False):
c = []
dfs_util(board, visited, c, i, j, N, M)
components.append(len(c))
components.sort(key=lambda x: -x)
return components
def dfs_util(board, visited, c, i, j, N, M):
if(i < 0 or j < 0 or i >= N or j >= M or board[i][j] == '0' or visited[i][j] == True):
return
visited[i][j] = True
c.append([i,j])
dfs_util(board, visited, c, i+1, j, N, M)
dfs_util(board, visited, c, i, j+1, N, M)
dfs_util(board, visited, c, i-1, j, N, M)
dfs_util(board, visited, c, i, j-1, N, M)
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
board = []
for i in range(N):
row = list(input())
board.append(row)
l = connected_components(board, N, M)
ans = 0
for i in range(len(l)):
if(i%2 == 1):
ans += l[i]
print(ans)
?