Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)
A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.
Example 1:
Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: 
There are three 1s that are enclosed by 0s, and one 1 that isn't enclosed because its on the boundary.
Example 2:
Input: [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: 
All 1s are either on the boundary or can reach the boundary.
Note:
1 <= A.length <= 500
1 <= A[i].length <= 500
0 <= A[i][j] <= 1
All rows have the same size.
class Solution:
    def numEnclaves(self, A: List[List[int]]) -> int:
        def dfs(i, j):
            if not (0<=i<len(A) and 0<=j<len(A[i])):
                return
            if A[i][j]==0:
                return
            A[i][j]=0
            dfs(i-1, j)
            dfs(i+1, j)
            dfs(i, j-1)
            dfs(i, j+1)
        for i in range(len(A)):
            for j in range(len(A[i])):
                if A[i][j]==0:
                    continue
                if (i==0 or j==0 or i==len(A)-1 or j==len(A[i])-1):
                    dfs(i, j)
        res = sum([sum(row) for row in A])
        return res
class Solution:
    def numEnclaves(self, A: List[List[int]]) -> int:
        def dfs(i, j):
            if not (0<=i<len(A) and 0<=j<len(A[i])):
                return
            if A[i][j]==0:
                return
            A[i][j]=0
            dfs(i-1, j)
            dfs(i+1, j)
            dfs(i, j-1)
            dfs(i, j+1)
        for i in range(len(A)):
            for j in range(len(A[i])):
                if A[i][j]==0:
                    continue
                if (i==0 or j==0 or i==len(A)-1 or j==len(A[i])-1):
                    dfs(i, j)
        res = sum([sum(row) for row in A])
        return res