Problem:
Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


// LM10: The next Ballon d'Or
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
    static class FastReader {
        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar, numChars;

        public FastReader() {
            this(System.in);
        }

        public FastReader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long l() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int i() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double d() throws IOException {
            return Double.parseDouble(next());
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public void scanIntArr(int[] arr) {
            for (int li = 0; li < arr.length; ++li) {
                arr[li] = i();
            }
        }

        public void scanLongArr(long[] arr) {
            for (int i = 0; i < arr.length; ++i) {
                arr[i] = l();
            }
        }

        public void shuffle(int[] arr) {
            for (int i = arr.length; i > 0; --i) {
                int r = (int) (Math.random() * i);
                int temp = arr[i - 1];
                arr[i - 1] = arr[r];
                arr[r] = temp;
            }
        }
    }

    static int ways;
    public static void main(String[] args) throws IOException {
        /*
Sample Input 1
Copy
3 3
.#.
..#
#..
Sample Output 1
Copy
10
*/
        PrintWriter pw = new PrintWriter(System.out);
        FastReader fr = new FastReader();
        int t = 1;
        for (int ti = 0; ti < t; ++ti)
        {
            int h=fr.i();
            int w=fr.i();
            char [][] arr=new char[h][w];
            for(int hi=0;hi<h;++hi)
            {
                arr[hi]=fr.nextLine().toCharArray();
            }
            ways=0;
            for(int hi=0;hi<h;++hi)
            {
                for(int wi=0;wi<w;++wi)
                {
                    if(arr[hi][wi]=='#')
                    {
                        boolean [][] visited=new boolean[h][w];

                        if(hi+1<h)
                        {
                            recursion(arr,hi+1,wi,'#',visited);
                        }
                        if(wi+1<w)
                        {
                            recursion(arr,hi,wi+1,'#',visited);
                        }
                    }
                }
            }
            pw.println(ways*2);
        }
        pw.flush();
        pw.close();
    }
    public static void recursion(char [][] arr,int hi,int wi,char c,boolean[][] visited)
    {
        //System.out.println("hi="+hi+" wi="+wi+" c="+c);
        if(c==arr[hi][wi] || visited[hi][wi])
        {
            return;
        }
        if(c=='#')
            c='.';
        else
            c='#';
        if(c=='.')
        {
            //System.out.println(hi+" "+wi);
            ++ways;
        }
        visited[hi][wi]=true;


        if(hi+1<arr.length)
        {
            recursion(arr,hi+1,wi,c,visited);
        }

        if(wi+1<arr[hi].length)
        {
            recursion(arr,hi,wi+1,c,visited);
        }
    }
}