/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package matrice; import java.util.*; import java.util.Scanner; /** * * @author cteam030 */ public class Matrice { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] split = sc.nextLine().split(" "); int h = Integer.parseInt(split[0]); int w = Integer.parseInt(split[1]); char[][] arr = new char[h][]; for (int i = 0; i < h; i++) { arr[i] = sc.nextLine().toCharArray(); } int left[][] = new int[h][w]; int right[][] = new int[h][w]; int left2[][] = new int[h][w]; int right2[][] = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { left[i][j] = 1; right[i][j] = 1; left2[i][j] = 1; right2[i][j] = 1; } } int count = 0; for (int i = 1; i < h; i++) { for (int j = 0; j < w; j++) { // Look left if (j > 0) { if (arr[i][j] == arr[i - 1][j - 1] && arr[i][j] == arr[i - 1][j]) { int min = Math.min(left[i - 1][j - 1], left[i - 1][j]); count += min; min++; left[i][j] = min; } } // Look right if (j < w - 1) { if (arr[i][j] == arr[i - 1][j] && arr[i][j] == arr[i - 1][j + 1]) { int min = Math.min(right[i - 1][j], right[i - 1][j + 1]); count += min; min++; right[i][j] = min; } } } } for (int i = h - 2; i >= 0; i--) { for (int j = 0; j < w; j++) { // Look bottom left if (j > 0) { if (arr[i][j] == arr[i + 1][j - 1] && arr[i][j] == arr[i + 1][j]) { int min = Math.min(left2[i + 1][j - 1], left2[i + 1][j]); count += min; min++; left2[i][j] = min; } } // Look bottom right if (j < w - 1) { if (arr[i][j] == arr[i + 1][j + 1] && arr[i][j] == arr[i + 1][j]) { int min = Math.min(right2[i + 1][j + 1], right2[i + 1][j]); count += min; min++; right2[i][j] = min; } } } } // for (int i = 0; i < h; i++) { // for (int j = 0; j < w; j++) { // System.out.print(right[i][j] + " "); // } // System.out.println(); // } // // for (int i = 0; i < h; i++) { // for (int j = 0; j < w; j++) { // System.out.print(left[i][j] + " "); // } // System.out.println(); // } System.out.println(count); } }