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
Examples:
Input : mat[][] = {{1,0},{1,1}}
Output : 8
Explanation:

Example 1
Input : mat[][] = {{0, 1, 0, 0, 0},{1, 1, 1, 0, 0},{1, 0, 0, 0, 0}}
Output : 12
Explanation:

Example 2
Approach:
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++
#include<bits/stdc++.h>
using namespace std;
#define R 3
#define C 5
int numofneighbour( int mat[][C], int i, int j)
{
int count = 0;
if (i > 0 && mat[i - 1][j])
count++;
if (j > 0 && mat[i][j - 1])
count++;
if (i < R-1 && mat[i + 1][j])
count++;
if (j < C-1 && mat[i][j + 1])
count++;
return count;
}
int findperimeter( int mat[R][C])
{
int perimeter = 0;
for ( int i = 0; i < R; i++)
for ( int j = 0; j < C; j++)
if (mat[i][j])
perimeter += (4 - numofneighbour(mat, i ,j));
return perimeter;
}
int main()
{
int mat[R][C] =
{
0, 1, 0, 0, 0,
1, 1, 1, 0, 0,
1, 0, 0, 0, 0,
};
cout << findperimeter(mat) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static final int R = 3 ;
static final int C = 5 ;
static int numofneighbour( int mat[][],
int i, int j)
{
int count = 0 ;
if (i > 0 && mat[i - 1 ][j] == 1 )
count++;
if (j > 0 && mat[i][j - 1 ] == 1 )
count++;
if (i < R - 1 && mat[i + 1 ][j] == 1 )
count++;
if (j < C - 1 && mat[i][j + 1 ] == 1 )
count++;
return count;
}
static int findperimeter( int mat[][])
{
int perimeter = 0 ;
for ( int i = 0 ; i < R; i++)
for ( int j = 0 ; j < C; j++)
if (mat[i][j] == 1 )
perimeter += ( 4 -
numofneighbour(mat, i, j));
return perimeter;
}
public static void main(String[] args)
{
int mat[][] = {{ 0 , 1 , 0 , 0 , 0 },
{ 1 , 1 , 1 , 0 , 0 },
{ 1 , 0 , 0 , 0 , 0 }};
System.out.println(findperimeter(mat));
}
}
|
Python3
R = 3
C = 5
def numofneighbour(mat, i, j):
count = 0 ;
if (i > 0 and mat[i - 1 ][j]):
count + = 1 ;
if (j > 0 and mat[i][j - 1 ]):
count + = 1 ;
if (i < R - 1 and mat[i + 1 ][j]):
count + = 1
if (j < C - 1 and mat[i][j + 1 ]):
count + = 1 ;
return count;
def findperimeter(mat):
perimeter = 0 ;
for i in range ( 0 , R):
for j in range ( 0 , C):
if (mat[i][j]):
perimeter + = ( 4 - numofneighbour(mat, i, j));
return perimeter;
mat = [ [ 0 , 1 , 0 , 0 , 0 ],
[ 1 , 1 , 1 , 0 , 0 ],
[ 1 , 0 , 0 , 0 , 0 ] ]
print (findperimeter(mat), end = "\n" );
|
C#
using System;
public class GFG
{
public const int R = 3;
public const int C = 5;
public static int numofneighbour( int [][] mat, int i, int j)
{
int count = 0;
if (i > 0 && mat[i - 1][j] == 1)
{
count++;
}
if (j > 0 && mat[i][j - 1] == 1)
{
count++;
}
if (i < R - 1 && mat[i + 1][j] == 1)
{
count++;
}
if (j < C - 1 && mat[i][j + 1] == 1)
{
count++;
}
return count;
}
public static int findperimeter( int [][] mat)
{
int perimeter = 0;
for ( int i = 0; i < R; i++)
{
for ( int j = 0; j < C; j++)
{
if (mat[i][j] == 1)
{
perimeter += (4 - numofneighbour(mat, i, j));
}
}
}
return perimeter;
}
public static void Main( string [] args)
{
int [][] mat = new int [][]
{
new int [] {0, 1, 0, 0, 0},
new int [] {1, 1, 1, 0, 0},
new int [] {1, 0, 0, 0, 0}
};
Console.WriteLine(findperimeter(mat));
}
}
|
Javascript
<script>
let R = 3;
let C = 5;
function numofneighbour(mat, i, j)
{
let count = 0;
if (i > 0 && mat[i - 1][j] == 1)
count++;
if (j > 0 && mat[i][j - 1] == 1)
count++;
if (i < R - 1 && mat[i + 1][j] == 1)
count++;
if (j < C - 1 && mat[i][j + 1] == 1)
count++;
return count;
}
function findperimeter(mat)
{
let perimeter = 0;
for (let i = 0; i < R; i++)
for (let j = 0; j < C; j++)
if (mat[i][j] == 1)
perimeter += (4 -
numofneighbour(mat, i, j));
return perimeter;
}
let mat = [ [ 0, 1, 0, 0, 0 ],
[ 1, 1, 1, 0, 0 ],
[ 1, 0, 0, 0, 0 ] ];
document.write(findperimeter(mat));
</script>
|
PHP
<?php
$R = 3;
$C = 5;
function numofneighbour( $mat , $i , $j )
{
global $R ;
global $C ;
$count = 0;
if ( $i > 0 && ( $mat [ $i - 1][ $j ]))
$count ++;
if ( $j > 0 && ( $mat [ $i ][ $j - 1]))
$count ++;
if (( $i < $R -1 )&& ( $mat [ $i + 1][ $j ]))
$count ++;
if (( $j < $C -1) && ( $mat [ $i ][ $j + 1]))
$count ++;
return $count ;
}
function findperimeter( $mat )
{
global $R ;
global $C ;
$perimeter = 0;
for ( $i = 0; $i < $R ; $i ++)
for ( $j = 0; $j < $C ; $j ++)
if ( $mat [ $i ][ $j ])
$perimeter += (4 -
numofneighbour( $mat , $i , $j ));
return $perimeter ;
}
$mat = array ( array (0, 1, 0, 0, 0),
array (1, 1, 1, 0, 0),
array (1, 0, 0, 0, 0));
echo findperimeter( $mat ), "\n" ;
?>
|
Time Complexity: O(R x C).
Auxiliary Space: O(1), since no extra space has been taken.
This article is contributed by Aarti_Rathi and Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.