Find perimeter of shapes formed with 1s in binary matrix
Last Updated : 23 Jul, 2025
Given a matrix of N rows and M columns, consisting of 0's and 1's. The task is to find the perimeter of the subfigure consisting of only 1s in the matrix. The perimeter of a figure is given by the total number of horizontal and vertical linings formed by the boundary 1s of the subfigure. Each line is considered to have a length of 1 unit. Note: There exists only one subfigure consisting of 1s
The idea is to traverse the matrix, find all ones and find their contribution in perimeter as per the below cases:
Case 1 (4 adjacent 1s) : contribution = 0 Case 2 (3 adjacent 1s) : contribution = 1 Case 3 (2 adjacent 1s) : contribution = 2 Case 4 (1 adjacent 1s) : contribution = 3 Case 5 (0 adjacent 1s) : contribution = 4
Finally return the sum of contribution of each 1 in the matrix as the answer.
Traverse the whole matrix and find the cell having value equal to 1.
Calculate the number of adjacent 1s for that cell and add, 4 - number of adjacent 1s to the total perimeter.
Below is the implementation of this approach:
C++
// C++ program to find perimeter of area covered by// 1 in 2D matrix consists of 0's and 1's.#include<bits/stdc++.h>usingnamespacestd;#define R 3#define C 5// Find the number of covered side for mat[i][j].intnumofneighbour(intmat[][C],inti,intj){intcount=0;// UPif(i>0&&mat[i-1][j])count++;// LEFTif(j>0&&mat[i][j-1])count++;// DOWNif(i<R-1&&mat[i+1][j])count++;// RIGHTif(j<C-1&&mat[i][j+1])count++;returncount;}// Returns sum of perimeter of shapes formed with 1sintfindperimeter(intmat[R][C]){intperimeter=0;// Traversing the matrix and finding ones to// calculate their contribution.for(inti=0;i<R;i++)for(intj=0;j<C;j++)if(mat[i][j])perimeter+=(4-numofneighbour(mat,i,j));returnperimeter;}// Driven Programintmain(){intmat[R][C]={0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,};cout<<findperimeter(mat)<<endl;return0;}
Java
// Java program to find perimeter of area// covered by 1 in 2D matrix consists // of 0's and 1'simportjava.io.*;classGFG{staticfinalintR=3;staticfinalintC=5;// Find the number of covered side // for mat[i][j].staticintnumofneighbour(intmat[][],inti,intj){intcount=0;// UPif(i>0&&mat[i-1][j]==1)count++;// LEFTif(j>0&&mat[i][j-1]==1)count++;// DOWNif(i<R-1&&mat[i+1][j]==1)count++;// RIGHTif(j<C-1&&mat[i][j+1]==1)count++;returncount;}// Returns sum of perimeter of shapes// formed with 1sstaticintfindperimeter(intmat[][]){intperimeter=0;// Traversing the matrix and // finding ones to calculate // their contribution.for(inti=0;i<R;i++)for(intj=0;j<C;j++)if(mat[i][j]==1)perimeter+=(4-numofneighbour(mat,i,j));returnperimeter;}// Driver codepublicstaticvoidmain(String[]args){intmat[][]={{0,1,0,0,0},{1,1,1,0,0},{1,0,0,0,0}};System.out.println(findperimeter(mat));}}// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find perimeter of area # covered by 1 in 2D matrix consists of 0's and 1's.R=3C=5# Find the number of covered side for mat[i][j].defnumofneighbour(mat,i,j):count=0;# UPif(i>0andmat[i-1][j]):count+=1;# LEFTif(j>0andmat[i][j-1]):count+=1;# DOWNif(i<R-1andmat[i+1][j]):count+=1# RIGHTif(j<C-1andmat[i][j+1]):count+=1;returncount;# Returns sum of perimeter of shapes formed with 1sdeffindperimeter(mat):perimeter=0;# Traversing the matrix and finding ones to# calculate their contribution.foriinrange(0,R):forjinrange(0,C):if(mat[i][j]):perimeter+=(4-numofneighbour(mat,i,j));returnperimeter;# Driver Codemat=[[0,1,0,0,0],[1,1,1,0,0],[1,0,0,0,0]]print(findperimeter(mat),end="\n");# This code is contributed by Akanksha Rai
C#
usingSystem;// C# program to find perimeter of area // covered by 1 in 2D matrix consists // of 0's and 1's publicclassGFG{publicconstintR=3;publicconstintC=5;// Find the number of covered side // for mat[i][j]. publicstaticintnumofneighbour(int[][]mat,inti,intj){intcount=0;// UP if(i>0&&mat[i-1][j]==1){count++;}// LEFT if(j>0&&mat[i][j-1]==1){count++;}// DOWN if(i<R-1&&mat[i+1][j]==1){count++;}// RIGHT if(j<C-1&&mat[i][j+1]==1){count++;}returncount;}// Returns sum of perimeter of shapes // formed with 1s publicstaticintfindperimeter(int[][]mat){intperimeter=0;// Traversing the matrix and // finding ones to calculate // their contribution. for(inti=0;i<R;i++){for(intj=0;j<C;j++){if(mat[i][j]==1){perimeter+=(4-numofneighbour(mat,i,j));}}}returnperimeter;}// Driver code publicstaticvoidMain(string[]args){int[][]mat=newint[][]{newint[]{0,1,0,0,0},newint[]{1,1,1,0,0},newint[]{1,0,0,0,0}};Console.WriteLine(findperimeter(mat));}}// This code is contributed by Shrikant13
JavaScript
<script>// JavaScript program to find perimeter of area// covered by 1 in 2D matrix consists // of 0's and 1'sletR=3;letC=5;// Find the number of covered side // for mat[i][j].functionnumofneighbour(mat,i,j){letcount=0;// UPif(i>0&&mat[i-1][j]==1)count++;// LEFTif(j>0&&mat[i][j-1]==1)count++;// DOWNif(i<R-1&&mat[i+1][j]==1)count++;// RIGHTif(j<C-1&&mat[i][j+1]==1)count++;returncount;}// Returns sum of perimeter of shapes// formed with 1sfunctionfindperimeter(mat){letperimeter=0;// Traversing the matrix and // finding ones to calculate // their contribution.for(leti=0;i<R;i++)for(letj=0;j<C;j++)if(mat[i][j]==1)perimeter+=(4-numofneighbour(mat,i,j));returnperimeter;}// Driver Codeletmat=[[0,1,0,0,0],[1,1,1,0,0],[1,0,0,0,0]];document.write(findperimeter(mat));// This code is contributed by souravghosh0416</script>
PHP
<?php// PHP program to find perimeter of area // covered by 1 in 2D matrix consists// of 0's and 1's. $R=3;$C=5;// Find the number of covered side// for mat[i][j]. functionnumofneighbour($mat,$i,$j){global$R;global$C;$count=0;// UP if($i>0&&($mat[$i-1][$j]))$count++;// LEFT if($j>0&&($mat[$i][$j-1]))$count++;// DOWN if(($i<$R-1)&&($mat[$i+1][$j]))$count++;// RIGHT if(($j<$C-1)&&($mat[$i][$j+1]))$count++;return$count;}// Returns sum of perimeter of shapes// formed with 1s functionfindperimeter($mat){global$R;global$C;$perimeter=0;// Traversing the matrix and finding ones // to calculate their contribution. for($i=0;$i<$R;$i++)for($j=0;$j<$C;$j++)if($mat[$i][$j])$perimeter+=(4-numofneighbour($mat,$i,$j));return$perimeter;}// Driver Code$mat=array(array(0,1,0,0,0),array(1,1,1,0,0),array(1,0,0,0,0));echofindperimeter($mat),"\n";// This code is contributed by Sach_Code?>
Output
12
Time Complexity: O(R x C). Auxiliary Space: O(1), since no extra space has been taken.