public ArrayList<Integer> spiralOrder(final List<List<Integer>> a) {
		 ArrayList<Integer> result = new ArrayList<Integer>();
		 boolean left = true;
		 boolean right = false;
	     boolean top = false;
	     boolean down = false;
	     int lci = 0;
	     int rci = a.get(0).size() -1;
	     int tri = 0;
	     int bri = a.size() -1;
	     int N = a.size() * a.get(0).size();
	     
	     int scanCount = 0;
	     while(scanCount < N) {
	    	 if(left == true) {
	    		 List<Integer> row = a.get(tri);
	    		 for(int i=lci; i <= rci;i++) {
	    			 Integer val = row.get(i);
	    			 result.add(val);
	    			 scanCount++;
	    		 }
	    		 left = false;
	    		 down = true;
	    		 tri++;
	    	 } else if(down == true) {
	    		 for(int i = tri; i <= bri; i++) {
	    			 List<Integer> row = a.get(i);
	    			 Integer val = row.get(rci);
	    			 result.add(val);
	    			 scanCount++;
	    		 }
	    		 right = true;
	    		 down = false;
	    		 rci--;
	    	 } else if(right == true) {
	    		 List<Integer> row = a.get(bri);
	    		 for(int i=rci; i >= lci; i--) {
	    			 Integer val = row.get(i);
	    			 result.add(val);
	    			 scanCount++;
	    		 }
	    		 right = false;
	    		 top = true;
	    		 bri--;
	    	 } else if(top == true) {
	    		 for(int i= bri; i >= tri; i--) {
	    			 List<Integer> row = a.get(i);
	    			 Integer val = row.get(lci);
	    			 result.add(val);
	    			 scanCount++;
	    		 }
	    		 top = false;
	    		 left = true;
	    		 lci++;
	    	 }
	     }
		 return result;
	}