#include <iostream>
#include <fstream>

using namespace std;

const int N = 1001;
int n, m, tot, mi = 1e9, gr[N], gc[N];
char a[N][N];

int main()
{
    ifstream cin("video.in");
    ofstream cout("video.out");
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
        cin >> a[i][j];
    for (int j = 0; j < m; ++j)
        a[n][j] = a[0][j];
    for (int i = 0; i < n; ++i)
        a[i][m] = a[i][0];
    a[n][m] = a[0][0];
    for (int i = 0; i < n; tot += gr[i++])
    for (int j = 0; j < m; ++j)
    if (a[i][j] == a[i][j + 1] &&
         a[i][j] == a[i + 1][j + 1] &&
          a[i][j] == a[i + 1][j])
        ++gr[i], ++gc[j];
    for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
        mi = min(mi, gr[i] + gc[j] - (a[i][j] == a[i][j + 1] &&
                                       a[i][j] == a[i + 1][j + 1] &&
                                        a[i][j] == a[i + 1][j]));
    cout << tot - mi;
    return 0;
}
