package hackerrank;

import java.util.Scanner;

/**
 * https://www.hackerrank.com/challenges/connected-cell-in-a-grid
 */
public class ConnectedCellsInAGrid {
    private static int connectedCells(int[][] matrix) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = in.nextInt();
            }
        }
        System.out.println(connectedCells(matrix));
    }
}

@@ -208,44 +208,1510 @@
 trix
-) %7B%0A        // TODO%0A        return 0
+, int maxRow, int maxCol) %7B%0A        int max = 0;%0A        for (int i = 0; i %3C maxRow; i++) %7B%0A            for (int j = 0; j %3C maxCol; j++) %7B%0A                if (matrix%5Bi%5D%5Bj%5D == 1) %7B%0A                    int tmpMax = connectedCells(matrix, maxRow, maxCol, i, j);%0A                    max = Math.max(max, tmpMax);%0A                %7D%0A            %7D%0A        %7D%0A        return max;%0A    %7D%0A%0A    private static int connectedCells(int%5B%5D%5B%5D matrix, int maxRow, int maxCol, int row, int col) %7B%0A        if (row %3C 0 %7C%7C row %3E= maxRow %7C%7C col %3C 0 %7C%7C col %3E= maxCol) %7B%0A            return 0;%0A        %7D%0A        if (matrix%5Brow%5D%5Bcol%5D == 0) %7B%0A            return 0;%0A        %7D%0A        if (matrix%5Brow%5D%5Bcol%5D == 1) %7B%0A            matrix%5Brow%5D%5Bcol%5D = 0;%0A        %7D%0A        // up%0A        int a = connectedCells(matrix, maxRow, maxCol, row - 1, col);%0A        // upper right%0A        int b = connectedCells(matrix, maxRow, maxCol, row - 1, col + 1);%0A        // right%0A        int c = connectedCells(matrix, maxRow, maxCol, row, col + 1);%0A        // lower right%0A        int d = connectedCells(matrix, maxRow, maxCol, row + 1, col + 1);%0A        // down%0A        int e = connectedCells(matrix, maxRow, maxCol, row + 1, col);%0A        // lower left%0A        int f = connectedCells(matrix, maxRow, maxCol, row + 1, col - 1);%0A        // left%0A        int g = connectedCells(matrix, maxRow, maxCol, row, col - 1);%0A        // upper left%0A        int h = connectedCells(matrix, maxRow, maxCol, row - 1, col - 1);%0A        return a + b + c + d + e + f + g + h + 1
 ;%0A  
@@ -2046,32 +2046,32 @@
     %7D%0A        %7D%0A
-
         System.o
@@ -2102,16 +2102,22 @@
 s(matrix
+, n, m
 ));%0A    

Solve Connected Cells In A Grid