// C# code to implement the approach
using System;
public class GFG {
// Input value of N
const int N = 3;
// Input matrix
static char[, ] ch = { { '0', '1', '1' },
{ '0', '0', '0' },
{ '1', '1', '1' } };
// Declaration of Matrices for storing
// length of the path in each direction
// for each index of the input matrix
static int[, ] H = new int[N, N];
static int[, ] V = new int[N, N];
static int[, ] DB = new int[N, N];
static int[, ] DF = new int[N, N];
static int[, ] sol = new int[N, N];
// Method for traversing in
// horizontal direction
static void horizontal()
{
// Loop for traversing
// horizontally
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// Condition when '1'
// is found
if (ch[i, j] == '1') {
// Initialized variables
// to count the length
int k = j, count = 0;
// While continuous '1'
// is found
while (k < N) {
if (ch[i, k] == '1') {
count++;
k++;
}
else {
break;
}
}
k--;
for (int l = j; l <= k; l++)
H[i, l] = count;
j = k;
}
else {
H[i, j] = 0;
}
}
}
}
// Method for vertical traversal
static void vertical()
{
// Loop for traversing vertically
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// Condition when '1'
// is found
if (ch[j, i] == '1') {
// Initialized variables
// for count length of
// continuous '1'
int k = j, count = 0;
// Loop while continuous
// '1' is found
while (k < N) {
if (ch[k, i] == '1') {
count++;
k++;
}
else {
break;
}
}
k--;
// Initializing length
// in matrix
for (int l = j; l <= k; l++)
V[l, i] = count;
j = k;
}
else {
V[j, i] = 0;
}
}
}
}
static void diagonalBackward()
{
// Loop for traversing in backward
// direction diagonally
for (int i = 0; i < N; i++) {
int l = -1;
for (int j = i; j < N; j++) {
l++;
// Condition when continuous
// '1' is found
if (ch[l, j] == '1') {
// Initialized variables
// to count length of
// continuous '1'
int m = l, n = j, count = 0;
// While loop till
// continuous '1' is found
while (n < N) {
if (ch[m, n] == '1') {
count++;
m++;
n++;
}
else {
break;
}
}
m--;
n--;
// Initializing
// values in matrix
for (int p = j; p <= n; p++) {
DB[l, p] = count;
l++;
}
l--;
j = n;
}
else {
DB[l, j] = 0;
}
}
}
// Loop for traversing
for (int i = 1; i < N; i++) {
int l = i - 1;
for (int j = 0; j < (N - i); j++) {
l++;
// Condition when continuous
// '1' is found
if (ch[l, j] == '1') {
// Variables for counting length of
// continuous '1'
int m = l, n = j, count = 0;
// While loop for finding length of
// continuous '1'
while (n < (N - i)) {
if (ch[m, n] == '1') {
count++;
m++;
n++;
}
else {
break;
}
}
m--;
n--;
// Initializing length
// in matrix
for (int p = j; p <= n; p++) {
DB[l, p] = count;
l++;
}
l--;
j = n;
}
else {
DB[l, j] = 0;
}
}
}
}
// Loop for traversing diagonally in
// forward direction
static void diagonalForward()
{
// Loop for traversal
for (int i = 0; i < N; i++) {
int l = i;
for (int j = 0; j <= i; j++) {
// Condition when '1'
// is found
if (ch[j, l] == '1') {
// Variables initialized
// to count length of
// continuous '1'
int m = j, n = l, count = 0;
// While loop for
// finding the length of
// continuous '1'
while (m <= i) {
if (ch[m, n] == '1') {
count++;
m++;
n--;
}
else {
break;
}
}
m--;
n++;
// initializing values
// in matrix
for (int p = j; p <= m; p++) {
DF[p, l] = count;
l--;
}
j = m;
l = n;
l--;
}
else {
DF[j, l] = 0;
l--;
}
}
}
// Loop for traversal
for (int i = 1; i < N; i++) {
int l = i;
for (int j = N - 1; j >= i; j--) {
// Condition when
// continuous '1' is found
if (ch[l, j] == '1') {
// Variables initialized
int m = l, n = j, count = 0;
// While loop for
// counting length of
// continuous '1'
while (n >= i) {
if (ch[m, n] == '1') {
count++;
m++;
n--;
}
else {
break;
}
}
n++;
m--;
// Initializing values
// in matrix
for (int p = l; p <= m; p++) {
DF[p, j] = count;
j--;
}
l = m + 1;
j = n;
}
else {
DF[l, j] = 0;
l++;
}
}
}
}
// Method for creating a matrix of
// maximum length for each index
static void maxSol()
{
// Solution matrix
Array.Clear(sol, 0, sol.Length);
int max_ = int.MinValue;
// loop for traversing
for (int i = 0; i < N; i++) {
// Loop for initializing
// solution matrix by finding
// maximum length for each index
// in each direction
for (int j = 0; j < N; j++) {
if (H[i, j] >= V[i, j]
&& H[i, j] >= DB[i, j]
&& H[i, j] >= DF[i, j])
sol[i, j] = H[i, j];
else if (V[i, j] >= H[i, j]
&& V[i, j] >= DB[i, j]
&& V[i, j] >= DF[i, j])
sol[i, j] = V[i, j];
else if (DB[i, j] >= H[i, j]
&& DB[i, j] >= V[i, j]
&& DB[i, j] >= DF[i, j])
sol[i, j] = DB[i, j];
else
sol[i, j] = DF[i, j];
max_ = Math.Max(max_, sol[i, j]);
}
}
// Printing maximum length path
Console.WriteLine(max_);
}
// Function which is called in the
// driver function
static void solution()
{
// Initialization of matrices for
// storing the longest path in
// each index
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
H[i, j] = 0;
V[i, j] = 0;
DB[i, j] = 0;
DF[i, j] = 0;
}
}
// Function call for horizontal
// traversals
horizontal();
// Function call for vertical
// traversals
vertical();
// Function call for diagonal
// traversals
diagonalBackward();
diagonalForward();
// Function for printing matrix
// of the longest path for each index
// of the input matrix
maxSol();
}
// Driver Code
static public void Main(string[] args)
{
// Function call
solution();
}
}
// This Code is Contributed by Prasad Kandekar(prasad264)