Largest Plus Formed by All Ones in a Binary Square Matrix
Last Updated : 16 Jun, 2026
Given an n × n binary matrix mat[][] consisting of 0s and 1s, find the size of the largest ‘+’ shape that can be formed using only 1s. If no ‘+’ can be formed, return 0.
Note: A ‘+’ shape is formed by a central cell from which four arms extend in the up, down, left, and right directions, while staying within the matrix boundaries. The size of the ‘+’ shape is defined as the total number of cells that make up the structure, including the center cell and all the cells in its four arms.
Examples:
Input: mat[][] = [[0, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 1, 0]] Output: 5 Explanation: Largest ‘+’ would be formed by highlighted part of size 5.
Input: mat[][]= [[0,1], [1,0]] Output: 1 Explanation: Largest ‘+’ would be formed by highlighted part of size 5.
Input: mat = [[0]] Output: 0 Explanation: No ‘+’ sign can be formed.
[Naive Approach] Check Every Cell as Center - O(n ^ 3) Time and O(1) Space
Treat every cell containing 1 as the center of a '+' and expand simultaneously in all four directions while the cells contain 1s. The largest valid expansion determines the size of the '+' centered at that cell. Repeat this for all cells and keep track of the maximum size found.
C++
#include<iostream>#include<vector>usingnamespacestd;intfindLargestPlus(vector<vector<int>>&mat){intn=mat.size();intans=0;// Try every cell as the center of '+'.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==0)continue;intarm=1;// Expand in all four directions.while(i-arm>=0&&i+arm<n&&j-arm>=0&&j+arm<n&&mat[i-arm][j]==1&&mat[i+arm][j]==1&&mat[i][j-arm]==1&&mat[i][j+arm]==1){arm++;}ans=max(ans,4*arm-3);}}returnans;}intmain(){vector<vector<int>>mat={{0,1,1,1},{0,1,1,1},{0,0,1,1},{0,0,1,0}};cout<<findLargestPlus(mat);return0;}
Java
classGFG{staticintfindLargestPlus(int[][]mat){intn=mat.length;intans=0;// Try every cell as the center of '+'.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==0)continue;intarm=1;// Expand in all four directions.while(i-arm>=0&&i+arm<n&&j-arm>=0&&j+arm<n&&mat[i-arm][j]==1&&mat[i+arm][j]==1&&mat[i][j-arm]==1&&mat[i][j+arm]==1){arm++;}ans=Math.max(ans,4*arm-3);}}returnans;}publicstaticvoidmain(String[]args){int[][]mat={{0,1,1,1},{0,1,1,1},{0,0,1,1},{0,0,1,0}};System.out.println(findLargestPlus(mat));}}
Python
deffindLargestPlus(mat):n=len(mat)ans=0# Try every cell as the center of '+'.foriinrange(n):forjinrange(n):ifmat[i][j]==0:continuearm=1# Expand in all four directions.while(i-arm>=0andi+arm<nandj-arm>=0andj+arm<nandmat[i-arm][j]==1andmat[i+arm][j]==1andmat[i][j-arm]==1andmat[i][j+arm]==1):arm+=1ans=max(ans,4*arm-3)returnansmat=[[0,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,1,0]]print(findLargestPlus(mat))
C#
usingSystem;classGFG{staticintFindLargestPlus(int[][]mat){intn=mat.Length;intans=0;// Try every cell as the center of '+'.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==0)continue;intarm=1;// Expand in all four directions.while(i-arm>=0&&i+arm<n&&j-arm>=0&&j+arm<n&&mat[i-arm][j]==1&&mat[i+arm][j]==1&&mat[i][j-arm]==1&&mat[i][j+arm]==1){arm++;}ans=Math.Max(ans,4*arm-3);}}returnans;}staticvoidMain(){int[][]mat={newint[]{0,1,1,1},newint[]{0,1,1,1},newint[]{0,0,1,1},newint[]{0,0,1,0}};Console.WriteLine(FindLargestPlus(mat));}}
JavaScript
functionfindLargestPlus(mat){constn=mat.length;letans=0;// Try every cell as the center of '+'.for(leti=0;i<n;i++){for(letj=0;j<n;j++){if(mat[i][j]===0)continue;letarm=1;// Expand in all four directions.while(i-arm>=0&&i+arm<n&&j-arm>=0&&j+arm<n&&mat[i-arm][j]===1&&mat[i+arm][j]===1&&mat[i][j-arm]===1&&mat[i][j+arm]===1){arm++;}ans=Math.max(ans,4*arm-3);}}returnans;}// Driver Codeconstmat=[[0,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,1,0]];console.log(findLargestPlus(mat));
Output
5
[Expected Approach] Using Dynamic Programming - O(n ^ 2) Time and O(n ^ 2) Space
For each cell, precompute the number of consecutive 1s extending left, right, up, and down (including the cell itself). The maximum possible arm length of a '+' centered at a cell is the minimum of these four values. Compute this for every cell and return the largest '+' size found.
Step By Step Implementation:
We use four auxiliary matrices: left[][], right[][], top[][] and bottom[][].
left[i][j] stores the number of consecutive 1s to the left of cell (i, j), including the cell itself.
right[i][j] stores the number of consecutive 1s to the right of cell (i, j), including the cell itself.
top[i][j] stores the number of consecutive 1s above cell (i, j), including the cell itself.
bottom[i][j] stores the number of consecutive 1s below cell (i, j), including the cell itself.
For every cell, the maximum possible arm length of a '+' centered at that cell is the minimum of left[i][j], right[i][j], top[i][j], and bottom[i][j]. We compute this value for all cells and use the largest one to determine the size of the largest '+'.
C++
#include<iostream>#include<vector>usingnamespacestd;intfindLargestPlus(vector<vector<int>>&mat){intn=mat.size();vector<vector<int>>left(n,vector<int>(n,0));vector<vector<int>>right(n,vector<int>(n,0));vector<vector<int>>top(n,vector<int>(n,0));vector<vector<int>>bottom(n,vector<int>(n,0));// Compute consecutive 1s towards left and top.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==1){left[i][j]=1+(j>0?left[i][j-1]:0);top[i][j]=1+(i>0?top[i-1][j]:0);}}}// Compute consecutive 1s towards right and bottom.for(inti=n-1;i>=0;i--){for(intj=n-1;j>=0;j--){if(mat[i][j]==1){right[i][j]=1+(j+1<n?right[i][j+1]:0);bottom[i][j]=1+(i+1<n?bottom[i+1][j]:0);}}}intans=0;// Find the largest '+' centered at each cell.for(inti=0;i<n;i++){for(intj=0;j<n;j++){intarm=min({left[i][j],right[i][j],top[i][j],bottom[i][j]});ans=max(ans,4*arm-3);}}returnans;}intmain(){vector<vector<int>>mat={{0,1,1,1},{0,1,1,1},{0,0,1,1},{0,0,1,0}};cout<<findLargestPlus(mat);return0;}
Java
classGFG{staticintfindLargestPlus(int[][]mat){intn=mat.length;int[][]left=newint[n][n];int[][]right=newint[n][n];int[][]top=newint[n][n];int[][]bottom=newint[n][n];// Compute consecutive 1s towards left and top.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==1){left[i][j]=1+(j>0?left[i][j-1]:0);top[i][j]=1+(i>0?top[i-1][j]:0);}}}// Compute consecutive 1s towards right and bottom.for(inti=n-1;i>=0;i--){for(intj=n-1;j>=0;j--){if(mat[i][j]==1){right[i][j]=1+(j+1<n?right[i][j+1]:0);bottom[i][j]=1+(i+1<n?bottom[i+1][j]:0);}}}intans=0;// Find the largest '+' centered at each cell.for(inti=0;i<n;i++){for(intj=0;j<n;j++){intarm=Math.min(Math.min(left[i][j],right[i][j]),Math.min(top[i][j],bottom[i][j]));ans=Math.max(ans,4*arm-3);}}returnans;}publicstaticvoidmain(String[]args){int[][]mat={{0,1,1,1},{0,1,1,1},{0,0,1,1},{0,0,1,0}};System.out.println(findLargestPlus(mat));}}
Python
deffindLargestPlus(mat):n=len(mat)left=[[0]*nfor_inrange(n)]right=[[0]*nfor_inrange(n)]top=[[0]*nfor_inrange(n)]bottom=[[0]*nfor_inrange(n)]# Compute consecutive 1s towards left and top.foriinrange(n):forjinrange(n):ifmat[i][j]==1:left[i][j]=1+(left[i][j-1]ifj>0else0)top[i][j]=1+(top[i-1][j]ifi>0else0)# Compute consecutive 1s towards right and bottom.foriinrange(n-1,-1,-1):forjinrange(n-1,-1,-1):ifmat[i][j]==1:right[i][j]=1+(right[i][j+1]ifj+1<nelse0)bottom[i][j]=1+(bottom[i+1][j]ifi+1<nelse0)ans=0# Find the largest '+' centered at each cell.foriinrange(n):forjinrange(n):arm=min(left[i][j],right[i][j],top[i][j],bottom[i][j])ans=max(ans,4*arm-3)returnansmat=[[0,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,1,0]]print(findLargestPlus(mat))
C#
usingSystem;classGFG{staticintFindLargestPlus(int[][]mat){intn=mat.Length;int[][]left=newint[n][];int[][]right=newint[n][];int[][]top=newint[n][];int[][]bottom=newint[n][];for(inti=0;i<n;i++){left[i]=newint[n];right[i]=newint[n];top[i]=newint[n];bottom[i]=newint[n];}// Compute consecutive 1s towards left and top.for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]==1){left[i][j]=1+(j>0?left[i][j-1]:0);top[i][j]=1+(i>0?top[i-1][j]:0);}}}// Compute consecutive 1s towards right and bottom.for(inti=n-1;i>=0;i--){for(intj=n-1;j>=0;j--){if(mat[i][j]==1){right[i][j]=1+(j+1<n?right[i][j+1]:0);bottom[i][j]=1+(i+1<n?bottom[i+1][j]:0);}}}intans=0;// Find the largest '+' centered at each cell.for(inti=0;i<n;i++){for(intj=0;j<n;j++){intarm=Math.Min(Math.Min(left[i][j],right[i][j]),Math.Min(top[i][j],bottom[i][j]));ans=Math.Max(ans,4*arm-3);}}returnans;}staticvoidMain(){int[][]mat={newint[]{0,1,1,1},newint[]{0,1,1,1},newint[]{0,0,1,1},newint[]{0,0,1,0}};Console.WriteLine(FindLargestPlus(mat));}}
JavaScript
functionfindLargestPlus(mat){constn=mat.length;constleft=Array.from({length:n},()=>Array(n).fill(0));constright=Array.from({length:n},()=>Array(n).fill(0));consttop=Array.from({length:n},()=>Array(n).fill(0));constbottom=Array.from({length:n},()=>Array(n).fill(0));// Compute consecutive 1s towards left and top.for(leti=0;i<n;i++){for(letj=0;j<n;j++){if(mat[i][j]===1){left[i][j]=1+(j>0?left[i][j-1]:0);top[i][j]=1+(i>0?top[i-1][j]:0);}}}// Compute consecutive 1s towards right and bottom.for(leti=n-1;i>=0;i--){for(letj=n-1;j>=0;j--){if(mat[i][j]===1){right[i][j]=1+(j+1<n?right[i][j+1]:0);bottom[i][j]=1+(i+1<n?bottom[i+1][j]:0);}}}letans=0;// Find the largest '+' centered at each cell.for(leti=0;i<n;i++){for(letj=0;j<n;j++){constarm=Math.min(left[i][j],right[i][j],top[i][j],bottom[i][j]);ans=Math.max(ans,4*arm-3);}}returnans;}// Driver Codeconstmat=[[0,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,1,0]];console.log(findLargestPlus(mat));