using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        StreamReader read = new StreamReader(new FileStream("video.in", FileMode.Open));
        StreamWriter write = new StreamWriter(new FileStream("video.out", FileMode.Create));
        int[] nm = read.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
        string[] s = new string[nm[0]];
        for (int i = 0; i < nm[0]; i++)
            s[i] = read.ReadLine();
        int[,] a = new int[nm[0] * 2, nm[1] * 2];
        for (int i = 0; i < nm[0] * 2; i++)
            for (int j = 0; j < nm[1] * 2; j++)
            {
                int eq = 0;
                for (int k = 0; k < 2; k++)
                    for (int l = 0; l < 2; l++)
                    {
                        int ind1 = i + k, ind2 = j + l;
                        if (ind1 >= nm[0])
                            ind1 %= nm[0];
                        if (ind2 >= nm[1])
                            ind2 %= nm[1];
                        if (s[ind1][ind2] == s[i % nm[0]][j % nm[1]])
                            eq++;
                    }
                if (eq == 4)
                    a[i, j] = 1;
            }
        int[,] sum1 = new int[nm[0] * 2, nm[1] * 2], sum2 = new int[nm[0] * 2, nm[1] * 2];
        int ans = 0;
        for (int i = 0; i < nm[0] * 2; i++)
            sum1[i, 0] = a[i, 0];
        for (int i = 0; i < nm[0] * 2; i++)
            for (int j = 1; j < nm[1] * 2; j++)
                sum1[i, j] = sum1[i, j - 1] + a[i, j];
        for (int i = 0; i < nm[1] * 2; i++)
            sum2[0, i] = sum1[0, i];
        for (int i = 0; i < nm[1] * 2; i++)
            for (int j = 1; j < nm[0] * 2; j++)
                sum2[j, i] = sum2[j - 1, i] + sum1[j, i];
        ans = sum2[nm[0] - 2, nm[1] - 2];
        for (int i = 1; i < nm[1]; i++)
            ans = Math.Max(ans, sum2[nm[0] - 2, nm[1] - 2 + i] - sum2[nm[0] - 2, i - 1]);
        for (int i = 1; i < nm[0]; i++)
            ans = Math.Max(ans, sum2[nm[0] - 2 + i, nm[1] - 2] - sum2[i - 1, nm[1] - 2]);
        for (int i = 1; i < nm[0]; i++)
            for (int j = 1; j < nm[1]; j++)
                ans = Math.Max(ans, sum2[nm[0] - 2 + i, nm[1] - 2 + j] - sum2[i - 1, nm[1] - 2 + j] - sum2[nm[0] - 2 + i, j - 1] + sum2[i - 1, j - 1]);
        write.WriteLine(ans);
        read.Close();
        write.Close();
    }
}