Today is 10th class Maths Pre-Board exam and the students are arranged in a classroom forming N rows and M columns. There are two sets in Maths paper : SET-A and SET-B. The invigilator of the class being a new teacher, doesn't know the rules of distributing papers. So, he distributed the question papers (by mixing both the sets) randomly. Soon, he is being informed by one of the students the rule of distributing the paper which is that no adjacent students should get the same set of paper i.e. if a student gets a SET-A then the other students adjacent to him should get SET-B to avoid cheating.

Now since the paper is already distributed. The invigilator will have to exchange the sets of some students to ensure that no two adjacent students get the same set. Find the minimum number of students whose sets will be exchanged by the invigilator so that no two adjacent students have the same sets.

Two students are adjacent if they share one of their common sides (left,right,front or back if exists).

**Input:**

First line of the input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two lines. First Line contains the Integers N and M. N line follows each of which contains M spaced letters either A or B (or u can say a character array A of N rows and M columns is given input which consist of either A or B). A denotes SET-A and B denotes SET-B.

**Output:**

The output of each test case consist of a single line denoting the minimum number of students whose sets will be exchanged by the invegilator so that no two adjacent students have the same sets.

**Constraints:**

1<=T<=100

1<=N,M<=100

**Example :**

**Input :**

1

3 4

A A B A

B A B A

B B A B

**OUTPUT :**

4

**Explanation: **Minimum number of students whose sets got changed are 4 (indexes: ((0,1),(0,2),(0,3),(2,0))). The final alloted sets are:

A B A B

B A B A

A B A B

Author: Riya Bhardwaj

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.