The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. 

In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 × 2 square, such that from the four letters of this square you can make word "face". 

You need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.
The first line contains two space-separated integers, *n* and *m* (1 ≤ *n*, *m* ≤ 50) — the height and the width of the image, respectively.

Next *n* lines define the image. Each line contains *m* lowercase Latin letters.
import sys

rows, cols = map(int, input().split())

matrix = []
counter = 0

if rows==1 or cols ==1:
    print(0)
    sys.exit()

for i in range(0,rows):
    lst = list(str(input()))
    matrix.append(lst)

for i in range(0,rows):
    for j in range(0,cols):

        if matrix[i][j] == 'f':
            # CODE if 'f' is found within borders and not on the BORDER.

            if (i<rows-1 and i>=1) and (j<cols-1 and j>=1):

                lstBuffer=[]
                lstBuffer.append(matrix[i][j-1])
                lstBuffer.append(matrix[i-1][j-1])
                lstBuffer.append(matrix[i-1][j])
                x = sorted(lstBuffer)
                if x[0]=='a' and x[1]=='c' and x[2]=='e':
                    counter +=1

                lstBuffer = []
                lstBuffer.append(matrix[i][j + 1])
                lstBuffer.append(matrix[i - 1][j + 1])
                lstBuffer.append(matrix[i-1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j-1])
                lstBuffer.append(matrix[i+1][j-1])
                lstBuffer.append(matrix[i+1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j+1])
                lstBuffer.append(matrix[i + 1][j+1])
                lstBuffer.append(matrix[i + 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1



            # if 'f' is found in the first cell of the matrix TOP LEFT CORNER

            elif i==0 and j==0:

                lstBuffer = []
                lstBuffer.append(matrix[i][j + 1])
                lstBuffer.append(matrix[i + 1][j + 1])
                lstBuffer.append(matrix[i + 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1


            # if 'f' is found in TOP RIGHT CORNER

            elif i==0 and j==(cols-1):

                lstBuffer = []
                lstBuffer.append(matrix[i][j - 1])
                lstBuffer.append(matrix[i + 1][j - 1])
                lstBuffer.append(matrix[i + 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1



            # if 'f' is found in BOTTOM LEFT CORNER

            elif i==(rows-1) and j==0:

                lstBuffer = []
                lstBuffer.append(matrix[i][j + 1])
                lstBuffer.append(matrix[i - 1][j + 1])
                lstBuffer.append(matrix[i - 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1


            # if 'f' is found in BOTTOM RIGHT CORNER

            elif i==(rows-1) and j==(cols-1):

                lstBuffer = []
                lstBuffer.append(matrix[i][j - 1])
                lstBuffer.append(matrix[i - 1][j - 1])
                lstBuffer.append(matrix[i - 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1


            # if 'f' is between i==0 and j>=1 and j<(cols-1)

            elif i==0 and (j>=1 and j<(cols-1)):

                lstBuffer = []
                lstBuffer.append(matrix[i][j - 1])
                lstBuffer.append(matrix[i+1][j - 1])
                lstBuffer.append(matrix[i+1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j+1])
                lstBuffer.append(matrix[i + 1][j+1])
                lstBuffer.append(matrix[i + 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1


            # if 'f' is between j==0 and i>=1 and i<(rows-1)

            elif (i>=1 and i<(rows-1)) and j==0:

                lstBuffer = []
                lstBuffer.append(matrix[i][j+1])
                lstBuffer.append(matrix[i-1][j+1])
                lstBuffer.append(matrix[i-1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j + 1])
                lstBuffer.append(matrix[i + 1][j + 1])
                lstBuffer.append(matrix[i + 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1



            # if 'f' is between i==(rows-1) and j>=1 and j<(cols-1)

            elif i==rows-1 and (j>=1 and j<(cols-1)):

                lstBuffer = []
                lstBuffer.append(matrix[i][j - 1])
                lstBuffer.append(matrix[i-1][j - 1])
                lstBuffer.append(matrix[i-1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j + 1])
                lstBuffer.append(matrix[i-1][j + 1])
                lstBuffer.append(matrix[i-1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1



            # if 'f' is between j==(cols-1) and i>=1 and i<(rows-1)

            elif (i>=1 and i<(rows-1)) and j==(cols-1):

                lstBuffer = []
                lstBuffer.append(matrix[i][j - 1])
                lstBuffer.append(matrix[i+1][j - 1])
                lstBuffer.append(matrix[i+1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1

                lstBuffer = []
                lstBuffer.append(matrix[i][j-1])
                lstBuffer.append(matrix[i - 1][j-1])
                lstBuffer.append(matrix[i - 1][j])
                x = sorted(lstBuffer)
                if x[0] == 'a' and x[1] == 'c' and x[2] == 'e':
                    counter += 1




print(counter)