import java.util.ArrayList;

public class Shape {
	static Graph makeShape(int n, int m){
		ArrayList<ArrayList<Integer>> adjacencyList = new ArrayList<ArrayList<Integer>>(n);
		
		for (int row = 0; row < n; row++){
			for (int col = 0; col < m; col++){
				ArrayList<Integer> neighbors = new ArrayList<Integer>();
				int index = row*m + col;
				if (col < m-1){
					neighbors.add(index +1);
				}
				if (row < n-1){
					neighbors.add(index + m);
				}
				adjacencyList.add(neighbors);
				
			}
		}
//		for (int i = 0; i < adjacencyList.size(); i++){
//			System.out.println(i + ": " + adjacencyList.get(i).toString());
//		}
		
		Integer[][] array = new Integer[adjacencyList.size()][];
		for (int i = 0; i < adjacencyList.size(); i++) {
		    ArrayList<Integer> row = adjacencyList.get(i);
		    array[i] = row.toArray(new Integer[row.size()]);
		}
		
		return new Graph(array);
	}
}
