arr1 = [['#','#','#','#','#'], ['#','*','*','#','#'], ['#','#','#','*','#'], ['#','*','*','#','#'], ['#','#','#','#','#']] arr2 = [['#','#','#','#','#','#','#','#','#','#'], ['#','-','-','-','*','-','-','*','#','#'], ['#','#','#','-','*','-','-','-','-','#'], ['#','*','*','#','#','#','#','#','#','#'], ['#','#','*','*','*','-','-','-','#','#'], ['#','#','#','#','#','#','#','#','#','#']] def fun(n,m,arr): persons = 0 rooms = 0 for i in range(n): for j in range(m): if arr[i][j] != '#': if arr[i][j] == "*": persons += 1 if arr[i-1][j] != '#' or arr[i][j-1] != '#' or arr[i+1][j] != '#' or arr[i][j+1] != '#': arr[i][j] = '#' else: rooms += 1 arr[i][j] = '#' if rooms > 0: return persons/rooms else: return False print(fun(5,5,arr1)) print(fun(6,10,arr2))