Here you can find the source of neighbors(int[][] source, int x, int y)
public static int neighbors(int[][] source, int x, int y)
//package com.java2s; public class Main { public static int neighbors(int[][] source, int x, int y) { int maxX = Math.min(source[0].length - 1, x + 1); int maxY = Math.min(source.length - 1, y + 1); int counter = 0; for (int i = Math.max(y - 1, 0); i <= maxY; i++) { for (int j = Math.max(x - 1, 0); j <= maxX; j++) { if (j != x || i != y) { counter += source[i][j]; }/*from w ww . j a va 2s . c o m*/ } } return counter; } }