static void flip_color(boolean[][] A, int x, int y) {
		int[][] dir = new int[][] { new int[] { 0, 1 }, new int[] { 0, -1 },
				{ 1, 0 }, new int[] { -1, 0 } };
		boolean color = A[x][y];

		Queue<Pair<Integer, Integer>> q = new LinkedList<Pair<Integer, Integer>>();
		A[x][y] = !A[x][y]; // flips.
		q.add(new Pair<Integer, Integer>(x, y));
		while (!q.isEmpty()) {
			Pair<Integer, Integer> curr = q.element();
			for (int[] d : dir) {
				Pair<Integer, Integer> next = new Pair<Integer, Integer>(curr.getFirst() + d[0], curr.getSecond() + d[1]);
				if (next.getFirst() >= 0 && next.getFirst() < A.length
						&& next.getSecond() >= 0 && next.getSecond() < A[next.getFirst()].length
						&& A[next.getFirst()][next.getSecond()] == color) {
					// Flips the color.
					A[next.getFirst()][next.getSecond()] = !A[next.getFirst()][next.getSecond()];
					q.add(next);
				}
			}
			q.remove();
		}
	}