マトリックス内の描画ボックスのサイズをカスタマイズする

マトリックス内の描画ボックスのサイズをカスタマイズする
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{frame}{Matrix}

This is an example of a matrix.

\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),ampersand replacement=\&] (m)
{
  8 \& 8 \& 1 \& 6 \\
  3 \& 8 \& 5 \& 7 \\
  4 \& 8 \& 9 \& 5 \\
};
\draw[color=red, thick] (m-1-1.north west) -- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-1.south west) -- (m-1-1.north west);
\draw[color=blue, thick] (m-2-1.north west) -- (m-2-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-2-1.north west);
\end{tikzpicture}
\end{frame}
\end{document}

マトリックスに 2 つのボックスを描画しました (Beamer を使用)。2 つのボックスが部分的に重なり、青いボックスが赤いボックスの上に部分的に重なる点を除けば、うまく動作しますが、あまり見栄えがよくありません。

重なり合わないように、青いボックスを大きくしたり小さくしたりする方法はありますか? (重なりを避ける他の方法も歓迎します。)

答え1

次のようなことを期待しているとします。

ここに画像の説明を入力してください

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{amsmath,tikz}
\usetikzlibrary{fit,matrix}
\tikzset{
mN/.style = {%myNode
    draw=#1, thick, inner sep=0pt}
             }

\begin{document}
\begin{frame}{Matrix}

This is an example of a matrix.

    \begin{tikzpicture}
\matrix [matrix of math nodes, column sep=1pt,
         left delimiter=(,right delimiter=),ampersand replacement=\&] (m)
{
  8 \& 8 \& 1 \& 6 \\
  3 \& 8 \& 5 \& 7 \\
  4 \& 8 \& 9 \& 5 \\
};
\node[mN=red, xshift= 2pt, fit=(m-1-1) (m-2-3)] {};
\node[mN=blue,xshift=-2pt, fit=(m-2-1) (m-3-3)] {};
\end{tikzpicture}
\end{frame}
    \end{document}

編集:コードを少し最適化しました。

関連情報