Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
It's time to prepare an advent calendar, but I've only got a large sheet of rectangular paper, randomly colored red and green like this:
GRRRGRRGRG
GGRGGGRRGR
GRRGRGRGRG
GGRRGRGRGG
GRGGRGRGGG
RRGRGRGRRG
RRRGRGRGGG
RGGRGGRRRG
RGGGRRRRRG
GRGRRRGRRG
Out of this paper, I want to cut out a 5-by-5 region which is colored in a checkerboard fashion:
GRRRGRRGRG
GGRGGGRRGR
GRRGRGRGRG
GGRRGRGRGG
GRGGRGRGGG
RRGRGRGRRG
RRRGRGRGGG
RGGRGGRRRG
RGGGRRRRRG
GRGRRRGRRG
The exact coloring doesn't matter (the top left cell can be either green or red).
But if I'm unlucky, the piece of paper might not have such a region at all. In that case I will have to repaint some cells; I want to cut out a region which minimizes the amount of paint to make it checkered.
Task: Given a sheet of paper, output the minimum number of cells I need to recolor to get a checkered 5-by-5 grid.
Assume that both the width and height of the given paper are at least 5. You may choose to use any two distinct characters or numbers instead of R and G respectively.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
RGRGR
GRGRG
RGRGR
GRGRG
RGRGR => 0
GRRRGRRGRG
GGRGGGRRGR
GRRGRGRGRG
GGRRGRGRGG
GRGGRGRGGG
RRGRGRGRRG
RRRGRGRGGG
RGGRGGRRRG
RGGGRRRRRG
GRGRRRGRRG => 0
RGRGRG
GRGRGR
RGRGRG
RRRRRR
RGRGRG => 2
RRRRR
RRRRG
RRRGG
RRGGG
RGGGG => 10
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG => 12
RRGGGRRRGR
RRRGGRRRGR
RGGGGRRRGG
GRRGRRRRGG
GGGRGGRRGR
RRGGRGGGGG
GGRRRRRGRR
GGRGRRRRGR
GGRRRGGGGR
GRRGGGRGRR => 9
[2:7, 3:8]
. \$\endgroup\$