Given a table of M * N. There are total M * N squares of size 1. You have to colour each side of all squares with 3 colour Orange, Blue or Black such that each square have 2 different colours and each colour must occur twice.
It means each square have four sides, 2 of them is orange and 2 of them is Blue or 2 of them is orange and 2 of them is Black or 2 of them is Blue and 2 of them is Black.
Examples:
Input: N = 1, M = 1
Output: 18
Explanation:
We can fill the upper part of the square and left part of the square with any of the three colours. So the number of ways is 3*3,
Now for filling the right and the lower part, we have only 2 option if upper and left have the same colour.
If the upper and the left part have different colour then we can use those two colours to fill the right and lower part, it has also 2 option. The number of combination to fill right and lower is 2.
A total possible way to colour the square is 3*3*2 = 18Input: N = 3, M = 2
Output: 15552
Approach:
 Find the number of ways to fill the upper and right side of the rectangle. Upperside has M units and the right side has N units. So, The number of ways to colour the upper and the right side of the rectangle is pow(3, M + N) because each has 3 option.
 Now, For filling the lower and the left side of each square, It has 2 option for each square then the number of ways is pow(2, M * N).

Final result is:
Total count = pow(3, M + N ) * pow(2, M * N)
Below is the implementation of the above approach:
C++
// C++ program to count the number // of ways to color boundary of // each block of M*N table. #include <bits/stdc++.h> using namespace std; // Function to compute all way to // fill the boundary of all sides // of the unit square int CountWays( int N, int M) { int count = 1; // Count possible ways to fill // all upper and left side of // the rectangle M*N count = pow (3, M + N); // Count possible ways to fill // all side of the all squares // unit size count *= pow (2, M * N); return count; } // Driver code int main() { // Number of rows int N = 3; // Number of columns int M = 2; cout << CountWays(N, M); return 0; } 
Java
// Java program to count the number // of ways to color boundary of // each block of M*N table. import java.util.*; class GFG{ // Function to compute all way to // fill the boundary of all sides // of the unit square static int CountWays( int N, int M) { int count = 1 ; // Count possible ways to fill // all upper and left side of // the rectangle M*N count = ( int )Math.pow( 3 , M + N); // Count possible ways to fill // all side of the all squares // unit size count *= ( int )Math.pow( 2 , M * N); return count; } // Driver code public static void main(String args[]) { // Number of rows int N = 3 ; // Number of columns int M = 2 ; System.out.println(CountWays(N, M)); } } // This code is contributed by ANKITKUMAR34 
Python3
# Python3 program to count the number # of ways to color boundary of # each block of M*N table. # Function to compute all way to # fill the boundary of all sides # of the unit square def CountWays(N, M): count = 1 # Count possible ways to fill # all upper and left side of # the rectangle M*N count = pow ( 3 , M + N) # Count possible ways to fill # all side of the all squares # unit size count * = pow ( 2 , M * N); return count # Driver code # Number of rows N = 3 # Number of columns M = 2 print (CountWays(N, M)) # This code is contributed by ANKITKUMAR34 
15552
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Number of ways to color NK blocks using given operation
 Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
 Ways to color a skewed tree such that parent and child have different colors
 Multiplication table till N rows where every Kth row is table of K upto Kth term
 Compute the parity of a number using XOR and table lookup
 Find the number of cells in the table contains X
 Number of distinct ways to represent a number as sum of K unique primes
 Number of ways to split a binary number such that every part is divisible by 2
 Program to print multiplication table of a number
 Count number of ways to divide a number in 4 parts
 Find the number of ways to divide number into four parts such that a = c and b = d
 Recursive Program to print multiplication table of a number
 Number of ways to get a given sum with n number of mfaced dices
 Number of ways to choose a pair containing an even and an odd number from 1 to N
 Count number of ways to get Odd Sum
 Number of ways of writing N as a sum of 4 squares
 Bell Numbers (Number of ways to Partition a Set)
 Count number of ways to cover a distance  Set 2
 Number of ways to distribute N Paper Set among M students
 Number of ways to convert a character X to a string Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ANKITKUMAR34