public ArrayList<Integer> spiralOrder(int[][] matrix) {

    ArrayList<Integer> result = new ArrayList<Integer>();
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
      return result;
    }

    boolean[][] visited = new boolean[matrix.length][matrix[0].length];
    helper(matrix, visited, result, 0, 0, RIGHT);

    return result;
  }