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.

vamsikrishna55 | 162 |

Vagesh Verma | 158 |

UrGuru | 133 |

abducodes | 127 |

Akhil varma | 114 |

abducodes | 683 |

harshtyagi36 | 322 |

Akhil varma | 267 |

arpit_anshuman | 258 |

highway_to_hell | 252 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.