You are given row x col grid representing a map where grid[i][j] = 1 represents&nbsp;land and grid[i][j] = 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

&nbsp;
Example 1:

Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.


Example 2:

Input: grid = [[1]]
Output: 4


Example 3:

Input: grid = [[1,0]]
Output: 4


&nbsp;
Constraints:


	row == grid.length
	col == grid[i].length
	1 &lt;= row, col &lt;= 100
	grid[i][j] is 0 or 1.
	There is exactly one island in grid.


class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        perimeter = 0

        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == 1:
                    perimeter += 4
                    if i != 0 and grid[i-1][j] == 1:
                        perimeter -= 2
                    if j != 0 and grid[i][j-1] == 1:
                        perimeter -= 2 
            
        return perimeter
class Solution {
    public int islandPerimeter(int[][] grid) {
        if(grid == null || grid.length == 0) return 0;

        int row=grid.length,col=grid[0].length;
        int perimeter=0;

        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){

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

                    if(i>0 && grid[i-1][j]==1){
                    perimeter-=2;
                   }

                    if(j>0 && grid[i][j-1]==1){
                    perimeter-=2;
                   }
                }

            }
        }
        return perimeter;
    }
}
////**  hindi me samjho ab// 
// agar mje left or right or bottom or top me 1 mila to me aaga badunga agar nahi
//mila mtlb ya to wo boundary  hai  uske baad 0  ara hai agar esa hai to cnt+=1 
//kardo kuki wo hi to meri boundary banegi . note : agar boundary hai mtlb 
//i <0 or j<0 or i>=n or j>=m to cnt+=1 kardo or agar box ke side walo me 0 hai to wahan bhi cnt+=1 . 
// hope it make sense 
// please upvote if you like my post 
class Solution {
public:
     int cnt =0 ;
bool vis[101][101] ;
    bool valid( int i , int j, int n , int m){
        if(i>=n or j>=m or i<0 or j<0 )
             return false;
        return true ;
    }
    int islandPerimeter(vector<vector<int>>& grid) {
        memset(vis,false , sizeof(vis)) ;
        for(int i = 0 ; i <grid.size() ; i ++){ 
              for(int  j=0 ; j<grid[0].size() ; j++) {
                   
                  
                   if(grid[i][j]==1){
                       
                     solve(grid, i,j) ;
                       return cnt ;
                   }
              }        
         }
        return 69;
    }
 void  solve(vector<vector<int>>&grid, int i , int j ){
       int n = grid.size() ; 
     int m = grid[0].size() ;
 vis[i][j]=true ;
     if(!valid(i-1,j,n,m) or (valid(i-1,j,n,m) and grid[i-1][j]==0)){
         cnt++ ;
     }
     else if(valid(i-1,j,n,m)and !vis[i-1][j])
         solve(grid, i-1,j) ;
     
     if(!valid(i+1,j,n,m) or (valid(i+1,j,n,m) and grid[i+1][j]==0)){
         cnt++ ;
     }
        else if(valid(i+1,j,n,m)and !vis[i+1][j])
         solve(grid,i+1,j) ;
     
     if(!valid(i,j-1,n,m) or (valid(i,j-1,n,m) and grid[i][j-1]==0)){
         cnt++ ;
     }
          else if(valid(i,j-1,n,m)and !vis[i][j-1])
         solve(grid,i,j-1) ;
     
     if(!valid(i,j+1,n,m) or (valid(i,j+1,n,m) and grid[i][j+1]==0)){
         cnt++ ;
     }
          else if(valid(i,j+1,n,m)and !vis[i][j+1])
         solve(grid,i,j+1) ;
     
     
     
}
};
var islandPerimeter = function(grid) {
    let perimeter = 0
    let row = grid.length
    let col = grid[0].length
    
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[i].length; j++) {
            if (grid[i][j] === 1) {
                if (i === 0 || i > 0 && grid[i-1][j] === 0) perimeter++ 
                if (i === row-1 || i < row-1 && grid[i+1][j] === 0) perimeter++              
                if (j === 0 || j > 0 && grid[i][j-1] === 0) perimeter++
                if (j === col - 1 || j < col && grid[i][j+1] === 0) perimeter++
            }
        }
    }
    
    return perimeter
};
Island Perimeter