using System; using System.IO; using System.Linq; using System.Text; using System.Collections.Generic; using System.Diagnostics; using System.Numerics; using Enu = System.Linq.Enumerable; class Program { int H, W; string[] grid; bool[] seen; UnionFind uf; Dictionary> components; List[] G; public void Solve() { H = Reader.Int() + 2; W = Reader.Int() + 2; grid = Surround(Reader.StringArray(H - 2), '.'); seen = new bool[H * W]; uf = new UnionFind(H * W); memo = new int?[H * W, 2]; G = new List[H * W]; for (int i = 0; i < G.Length; i++) G[i] = new List(); for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) for (int dy = -1; dy <= 1; dy++) for (int dx = -1; dx <= 1; dx++) { int ny = y + dy, nx = x + dx; if (IsInside(ny, nx) && grid[y][x] == grid[ny][nx] && (dx == 0 || dy == 0 || grid[y][x] == 'x')) uf.Unite(y * W + x, ny * W + nx); } components = uf.Components(); CheckChild(uf.Root(0)); int ans = Rec(uf.Root(0), 1, true); Console.WriteLine(ans); } private void CheckChild(int id) { seen[id] = true; var nexts = new HashSet(); foreach (int cell in components[id]) { int y = cell / W, x = cell % W; for (int dy = -1; dy <= 1; dy++) for (int dx = -1; dx <= 1; dx++) { int ny = y + dy, nx = x + dx; if (!IsInside(ny, nx)) continue; int next = uf.Root(ny * W + nx); if (IsInside(ny, nx) && grid[ny][nx] != grid[y][x] && !seen[next]) nexts.Add(next); } } foreach (int next in nexts) { G[id].Add(next); CheckChild(next); } } bool IsInside(int y, int x) { return y >= 0 && y < H && x >= 0 && x < W; } int?[,] memo; private int Rec(int i, int canUse, bool empty) { if (memo[i, canUse].HasValue) return memo[i, canUse].Value; int res = 0; if (empty) { foreach (int child in G[i]) res += Rec(child, canUse, !empty); return res; } foreach (int child in G[i]) res += Rec(child, 1, !empty); if (canUse == 1) { int use = uf.NumElements(i); foreach (int child in G[i]) use += Rec(child, 0, !empty); res = Math.Max(res, use); } memo[i, canUse] = res; return res; } string[] Surround(string[] grid, char c) { int H = grid.Length + 2, W = grid[0].Length + 2; var res = new string[H]; for (int i = 0; i < H; i++) if (i == 0 || i == H - 1) res[i] = new string(c, W); else res[i] = c + grid[i - 1] + c; return res; } class UnionFind { private int[] parent; private int[] rank; private int[] numElements; public UnionFind(int N) { parent = new int[N]; rank = new int[N]; numElements = new int[N]; for (int i = 0; i < N; i++) { parent[i] = i; numElements[i] = 1; } } public int Root(int x) { if (parent[x] == x) return x; return parent[x] = Root(parent[x]); } public bool Same(int x, int y) { return Root(x) == Root(y); } public int NumElements(int x) { return numElements[Root(x)]; } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x == y) return false; if (rank[x] > rank[y]) { var t = x; x = y; y = t; } if (rank[x] == rank[y]) rank[y]++; parent[x] = y; numElements[y] += numElements[x]; return true; } public Dictionary> Components() { var dic = new Dictionary>(); for (int i = 0; i < parent.Length; i++) { int root = Root(i); if (!dic.ContainsKey(root)) dic[root] = new List(); dic[root].Add(i); } return dic; } } } class Entry { static void Main() { new Program().Solve(); } } class Reader { private static TextReader reader = Console.In; private static readonly char[] separator = { ' ' }; private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; private static string[] A = new string[0]; private static int i; private static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); } public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); } public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); } public static string Line() { return reader.ReadLine().Trim(); } private static string[] Split(string s) { return s.Split(separator, op); } private static string Next() { CheckNext(); return A[i++]; } private static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }