\documentclass[border=5pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw [fill=gray] (0, 2) rectangle (1, 2.5);
    \draw [fill=gray] (7, 2) rectangle (9, 2.5);    
    
    \node[draw,align=center, minimum size = .5cm] at  (1.25, 2.25) (top1) {a};
    \node[draw,align=center, minimum size = .5cm] at  (1.75, 2.25) (top2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (2.25, 2.25) (top3) {c};
    \node[draw,align=center, minimum size = .5cm] at  (2.75, 2.25) (top4) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.25, 2.25) (top5) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.75, 2.25) (top6) {c};
    \node[draw,align=center, minimum size = .5cm] at  (4.25, 2.25) (top7) {a};
    \node[draw,align=center, minimum size = .5cm] at  (4.75, 2.25) (top8) {a};
    \node[draw,align=center, minimum size = .5cm] at  (5.25, 2.25) (top9) {c};
    \node[draw,align=center, minimum size = .5cm] at  (5.75, 2.25) (top10){a};
    \node[draw,align=center, minimum size = .5cm] at  (6.25, 2.25) (top11) {a};
    \node[draw,align=center, minimum size = .5cm] at  (6.75, 2.25) (top12) {c};

    \draw [fill=gray] (0, 0) rectangle (2, .5);
    \draw [fill=gray] (8, 0) rectangle (9, .5);    
    
    \node[draw,align=center, minimum size = .5cm] at  (2.25, .25) (bottom1) {a};
    \node[draw,align=center, minimum size = .5cm] at  (2.75, .25) (bottom2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.25, .25) (bottom3) {c};
    \node[draw,align=center, minimum size = .5cm] at  (3.75, .25) (bottom4) {a};
    \node[draw,align=center, minimum size = .5cm] at  (4.25, .25) (bottom5) {a};
    \node[draw,align=center, minimum size = .5cm] at  (4.75, .25) (bottom6) {c};
    \node[draw,align=center, minimum size = .5cm] at  (5.25, .25) (bottom7) {a};
    \node[draw,align=center, minimum size = .5cm] at  (5.75, .25) (bottom8) {a};
    \node[draw,align=center, minimum size = .5cm] at  (6.25, .25) (bottom9) {c};
    \node[draw,align=center, minimum size = .5cm] at  (6.75, .25) (bottom10) {a};
    \node[draw,align=center, minimum size = .5cm] at  (7.25, .25) (bottom11) {a};
    \node[draw,align=center, minimum size = .5cm] at  (7.75, .25) (bottom12) {c};
    
    \draw [color=green] (top6) -- (bottom3);
    \draw [color=green] (top7) -- (bottom4);
    \draw [color=green] (top8) -- (bottom5);
    \draw [color=green] (top9) -- (bottom6);
    \draw [color=green] (top10) -- (bottom7);
    \draw [color=green] (top11) -- (bottom8);
    \draw [color=green] (top12) -- (bottom9);
    
    \draw [fill=gray] (0, -2) rectangle (1, -1.5);
    \draw [fill=gray] (4, -2) rectangle (6, -1.5);    
    
    \node[draw,align=center, minimum size = .5cm] at  (1.25, -1.75) (top1-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (1.75, -1.75) (top2-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (2.25, -1.75) (top3-2) {c};
    \node[draw,align=center, minimum size = .5cm] at  (2.75, -1.75) (top4-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.25, -1.75) (top5-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.75, -1.75) (top6-2) {c};

    \draw [fill=gray] (0, -4) rectangle (2, -3.5);
    \draw [fill=gray] (5, -4) rectangle (6, -3.5);    
    
    \node[draw,align=center, minimum size = .5cm] at  (2.25, -3.75) (bottom1-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (2.75, -3.75) (bottom2-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (3.25, -3.75) (bottom3-2) {c};
    \node[draw,align=center, minimum size = .5cm] at  (3.75, -3.75) (bottom4-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (4.25, -3.75) (bottom5-2) {a};
    \node[draw,align=center, minimum size = .5cm] at  (4.75, -3.75) (bottom6-2) {c};
    
    
    \node at (-.35,2.3) {$X$:};
    \node at (-.35, .3) {$Y$:};
    \node at (-.35,-1.7) {$X'$:};
    \node at (-.35,-3.7) {$Y'$:};
    
    \node at (3.75, 2.75) {$d$};
    \node at (3.25, -.25) {$d'$};
\end{tikzpicture}
\end{document}