On a grid of N months by M days, choose white or blue for each '?' cell to maximize total happiness, where each blue day scores 4 minus its blue neighbors.
Hard8Dynamic programmingGraphMatrixGreedyNo attempts yetTime limit5sMemory limit512 MBThe year 2008 is remembered as a year of change and transition, the start of a new era: the new Code Jam format. So many good programming contests were packed into that single year that people started calling it The Year of Code Jam.
Sphinny loves contests. Looking at her calendar for the year, she finds that a large number of programming contests are already scheduled. She has marked every day of the year on the calendar in one of three ways.
To keep the problem simple, assume there is no qualification. Taking part in one contest is never required in order to enter another one.
Sphinny lives in a world that is a little different from ours, so her calendar is different too. The year has N months, and every month has exactly M days.
The picture below shows a calendar with 5 months, 8 days per month, 15 blue days, and 5 question marks.

Every day in the year has at most 4 neighbors: the previous day in the same month, the next day in the same month, the same day in the previous month, and the same day in the next month.
Sphinny measures the effect of the contests on her happiness as the sum of one value per blue day. The value of a single blue day is computed like this.
Sphinny likes contests, but taking part on two days in a row makes her a little tired. Taking part on the same day in two consecutive months does not look good on the calendar either.
Sphinny now plans her year. For every day marked with a question mark she decides whether it stays white or becomes blue, and her goal is to maximize the total happiness value.
The next picture shows one answer for the calendar above. Turning two question marks into blue days and leaving the other three white gives a happiness value of 42.

The first line contains the number of cases T. Then T cases follow, each in the format below.
The first line of a case is "N M", where N is the number of months and M is the number of days per month.
Each of the next N lines contains one string of length M. The j-th character of the i-th string is one of '#', '.', '?' and gives the status of the j-th day of the i-th month. '#' is a blue day, '.' is a white day, and '?' is a day marked with a question mark.
Limits
For each case, print one line in the format
Case #X: Y
where X is the 1-based case number and Y is the maximum happiness value.
The second calendar in the sample input is the calendar drawn in the pictures above.