first,i explain my view, first step:if(this point is 1), so give sum += 4,second step:

furthermore,if(this point is 1(i,j) && around of this point have four point [(i-1,j),(i+1,j),(i,j-1),(i,j+1)] = 1) than this point will from sum -=1,so the result is output.

the fllowing is code:

'''

class Solution {

public int islandPerimeter(int[][] grid) {

int sum = 0;

for(int i = 0; i < grid.length; ++i) {

for(int j = 0; j < grid[i].length; ++j) {

if (grid[i][j] == 1) {

sum += 4;

if (i > 0) {

if (grid[i - 1][j] == 1) {

sum -= 1;

}

}

if (i < grid.length - 1) {

if (grid[i + 1][j] == 1) {

sum -= 1;

}

}

if (j > 0) {

if (grid[i][j - 1] == 1) {

sum -= 1;

}

}

if (j < grid[i].length - 1) {

if (grid[i][j + 1] == 1) {

sum -= 1;

}

}

}

}

}

return sum;

}

}

'''