Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
375 B
8
Indexable
Never
is_valid = lambda x,y: x >=0 and x < len(g) and y >=0 and y < len(g[0])

dx = [-1,1,0,0]
dy = [0,0,-1,1]

res = 0
for i in range(len(g)):
    for j in range(len(g[i])):
        e = 0
        if g[i][j] == 1:
            for k in range(4):
                if is_valid(i+dx[k], j+dy[k]) and g[i+dx[k]][j+dy[k]] == 1:
                    e += 1
            res += 4-e
return res