Problem :

https://www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid/problem


My Solution :

#!/usr/bin/env python3


def dfs(grid, x, y):
    if x not in range(len(grid)) or y not in range(len(grid[0])):
        return 0
    if not grid[x][y]:
        return 0
    count = 1
    grid[x][y] = 0
    count += dfs(grid, x+1, y+1)
    count += dfs(grid, x+1, y)
    count += dfs(grid, x+1, y-1)
    count += dfs(grid, x, y+1)
    count += dfs(grid, x, y-1)
    count += dfs(grid, x-1, y+1)
    count += dfs(grid, x-1, y)
    count += dfs(grid, x-1, y-1)
    return count


def maxRegion(grid):
    M = 0
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            M = max(M, dfs(grid, i, j))
    return M


n = int(input())
m = int(input())
grid = []
for _ in range(n):
    grid.append(list(map(int, input().rstrip().split())))
res = maxRegion(grid)
print(res)