Let's trace a small grid:
1 1
1 0
Cell (0,0): edges. Right neighbor is land (). Bottom neighbor is land (). Contribution: .
Cell (0,1): edges. Left neighbor is land (). Contribution: .
Cell (1,0): edges. Top neighbor is land (). Contribution: .
Total perimeter: .
You check each cell and up to neighbors per cell. That's time, space.