Tri-Color Puzzle
Time limit1sMemory limit1024 MB
Count the ways to color a triangular grid of hexagon cells, with some cells fixed, so every upward triple is all one color or all three colors.
Problem
A Tri-Color Puzzle is a triangular array of hexagon cells with cells on each side, for a total of cells. For example, the following is a puzzle with side 4 and 10 cells.

To solve the puzzle, each cell must be colored red, green, or blue so that for each triplet of cells with one cell above and two cells below, either all three cells are the same color or each of the three cells is a different color. For clarity, this statement uses hash patterns as well as colors:
Red =
Green =
Blue = 
In a particular puzzle, some cells are initially specified, and the remaining cells must be filled in as described above. The following example has three solutions:

This example has no solutions:

This example has exactly one solution:

Write a program that takes as input a description of a Tri-Color Puzzle and outputs the number of solutions to the puzzle.
Input
The first line contains two space-separated decimal integers, and (, ). The first line is followed by lines, each with three decimal integers: the row , the position of the cell within that row, and the color code of one initial cell (, , ). Code 0 means red, 1 means green, and 2 means blue.
Output
The output is a single line containing the number of solutions to the Tri-Color Puzzle specified by the input.