Neighbors of a matrix element I would like a code review to make this code better, clear, and concise. 

The problem it's solving is as follows:


  Given a matrix, print the circumference of each of the items. For example:

1 2 3 
4 5 6 
7 8 9

  
  should result in 

1 : 2 4 5
2 : 1 4 5 6 3
3 : 2 5 6 
etc. 



Also, am I correct in saying the time complexity is O(n4)?

public final class PrintCircumference {

    private PrintCircumference() {
    }

    public static void printCircumference(int[][] m) {

        if (m == null) {
            throw new NullPointerException("The input matrix cannot be null");
        }
        int row = m.length;
        int col = m[0].length;

        for (int i = 0; i &lt; row; i++) {
            for (int j = 0; j &lt; col; j++) {
                System.out.print(m[i][j] + " --&gt; ");

                for (int i1 = Math.max(0, i -1); i1 &lt; Math.min(row, i + 2); i1++) {
                    for (int j1 = Math.max(0, j -1); j1 &lt; Math.min(col, j + 2); j1++) {
                        if (i1 != i || j1 != j)
                            System.out.print(m[i1][j1]);
                    }
                }
                System.out.println();
            }
        }
    }


    public static void main(String[] args) {

        int[][] m = { {1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}, };

        printCircumference(m);
    }
}
Hmm, I think there's two things I'd do:


Put the 'neighbors' loops into their own method.  
Potentially, separating the neighbor collection from the actual printing.


(Please note that I've attempted to format this slightly better for SO's display width)

public final class PrintCircumference {

    private PrintCircumference() {
    }

    public static void printCircumference(int[][] matrix) {

        if (matrix == null) {
            throw new NullPointerException("The input matrix cannot be null");
        }

        for (int currentRow = 0, 
                 rowCount = matrix.length, 
                 columnCount = matrix[0].length; currentRow &lt; rowCount; currentRow++) {
            for (int currentColumn = 0; currentColumn &lt; columnCount; currentColumn++) {
                System.out.println(matrix[currentRow][currentColumn] 
                                   + " --&gt; " 
                                   + collectNeighbors(matrix, rowCount, columnCount, currentRow, currentColumn));
            }
        }
    }

    private static String collectNeighbors(int[][] matrix, int rowCount, int columnCount, int originCellRow, int originCellColumn) {
        StringBuilder neighbors = new StringBuilder();
        for (int neighborRow = Math.max(0, originCellRow - 1), 
                 neighborRowLimit = Math.min(rowCount, originCellRow + 2), 
                 neighborColumnLimit = Math.min(columnCount, originCellColumn + 2); neighborRow &lt; neighborRowLimit; neighborRow++) {
            for (int neighborColumn = Math.max(0, originCellColumn - 1); neighborColumn &lt; neighborColumnLimit; neighborColumn++) {
                if (neighborRow != originCellRow || neighborColumn != originCellColumn) {
                    neighbors.append(matrix[neighborRow][neighborColumn]);
                }
            }
        }
        return neighbors.toString();
    }

    public static void main(String[] args) {

        int[][] m = { { 1, 2, 3 }, 
                      { 4, 5, 6 }, 
                      { 7, 8, 9 }, };

        printCircumference(m);
    }
}


I've scoped the variables about as tightly as is possible.
Also, the original row and column variables are slightly misnamed - they're the count/limit, whereas most developers are probably expecting them to be the 'current' value.  In addition, while i is fine for a single loop, when you're explicitly looping over columns/rows, please name your variables as such.  Use new methods to let you 'rename variables in context' (something like what I've done with the currentRow -> originCellRow, etc, here).  It's also usually a good idea to put brackets on every loop/condition, in case something more needs to be added at some point in the future.

One word of warning - you're not doing enough error-checking here; fixing that is left as an exercise for the reader (for instance, I was unsure if anything should be printed if the matrix wasn't completely square).