#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>

using namespace std;

int m[3000][3000];
int sums[3000][3001];
int n, k;

inline bool eq(int a, int b, int c, int d) {
    return (a == b && a == c && a == d);
}

inline int down_left(int i, int j) {
    return ((i > 0 && j > 0 && eq(m[i - 1][j - 1], m[i][j - 1], m[i - 1][j], m[i][j])) ? 1 : 0);
}

inline int down_right(int i, int j) {
    return ((i > 0 && j < 2 * k && eq(m[i][j], m[i - 1][j], m[i][j + 1], m[i - 1][j + 1])) ? 1 : 0);
}

inline int up_left(int i, int j) {
    return ((i < 2 * n && j > 0 && eq(m[i][j], m[i + 1][j], m[i][j - 1], m[i + 1][j - 1])) ? 1 : 0);
}

inline int up_right(int i, int j) {
    return ((i < 2 * n && j < 2 * k && eq(m[i][j], m[i + 1][j + 1], m[i][j + 1], m[i + 1][j])) ? 1 : 0);
}

int main()
{
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < k; j++) {
            m[i][j] = s[j] - '1';
            m[i][j + k] = m[i][j];
            m[i + n][j] = m[i][j];
            m[i + n][j + k] = m[i][j];
        }
    }
   /* for (int i = 0; i < 2 * n; i++) {
        for (int j = 0; j < 2 * k; j++) {
            //printf("i = %d, j = %d:\n", i, j);
            printf("%d ", m[i][j]);
            //printf("ul = %d, ur = %d, dl = %d, dr = %d\n", up_left(i, j), up_right(i, j), down_left(i, j), down_right(i, j));
        }
        printf("\n");
    }
    printf("\n");
    printf("I got here\n");*/
    for (int j = 0; j <= 2 * k; j++) sums[0][j] = 0;
    for (int i = 0; i <= 2 * n; i++) sums[i][0] = 0;
    for (int i = 1; i <= 2 * n; i++) {
        //sums[i][0] = 0;
        for (int j = 1; j <= 2 * k; j++) {
            sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] + up_left(i - 1, j - 1) + up_right(i - 1, j - 1) + down_left(i - 1, j - 1) + down_right(i - 1, j - 1);
            //printf("i = %d, j = %d, sum = %d\n", i, j, sums[i][j]);
            //cout << sums[i][j] << " ";
        }
        //cout << "\n";
    }
    //cout << "\n";
    //printf("I got here\n");
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            int ans = 0;
            //for (int q = i + 1; q < i + n - 1; q++) {
              //  ans += sums[q][j + k - 1] - sums[q][j + 1];
            //}
            //printf("i = %d, j = %d\n", i, j);
            ans = ans + sums[i + n - 1][j + k - 1] - sums[i + n - 1][j + 1] - sums[i + 1][j + k - 1] + sums[i + 1][j + 1];
            ans = ans + up_right(i, j) + up_left(i, j + k - 1) + down_left(i + n - 1, j) + down_left(i + n - 1, j + k - 1);
            //printf("%d\n", ans);
            for (int q = i; q < i + n; q++) {
                ans = ans + up_right(q, j) + down_right(q, j) + up_left(q, j + k - 1) + down_left(q, j + k - 1);
            }
            //printf("%d\n", ans);
            for (int q = j + 1; q < j + k - 1; q++) {
                ans = ans + down_left(i + n - 1, q) + down_right(i + n - 1, q) + up_left(i, q) + up_right(i, q);
            }
            res = max(res, ans);
            //printf("i = %d, j = %d, ans = %d\n", i, j, ans);
        }
    }
    printf("%d\n", res / 4);
    return 0;
}
