public int[][] generateMatrix(int n) {
        int[][]  array= new int[n][n];
        int index = 1;
        for (int i=0;index <=n*n ;i++ ){
            //up
            for (int j=0;j < n ;j++ ) {
                if(array[i][j] == 0)
                    array[i][j] = index ++;
            }
            //right
            for (int j=0;j < n ;j++ ) {
                if(array[j][n-1-i] == 0)
                    array[j][n-1-i] = index ++;
            }
            //down
            for (int j=n-1;j >=0 ;j-- ) {
                if(array[n-1-i][j] == 0)
                    array[n-1-i][j] = index ++;
            }
            //left
            for (int j=n-1;j >=0 ;j-- ) {
                if(array[j][i] == 0)
                    array[j][i] = index ++;
            }

        }
        return array;
    }