```
T = 0
N = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]==1:
T+=1
try:
if grid[i][j+1]==1:
N+=1
except:
pass
try:
if grid[i+1][j]==1:
N+=1
except:
pass
return T*4-2*N
```