﻿//Description

//Write a program that reads a rectangular matrix of size N x M and finds
//in it the square 3 x 3 that has maximal sum of its elements.Print that sum.

namespace _02.MaximalSum
{
    using System;

    class FindMaxSum
    {
        static void Main()
        {
            string[] size = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int rows = int.Parse(size[0]);
            int cols = int.Parse(size[1]);

            int[,] matrix = new int[rows, cols];

            for (int i = 0; i < rows; i++)
            {

                string[] lines = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = int.Parse(lines[j]);
                }
            }

            int bestSum = int.MinValue;

            for (int row = 0; row < matrix.GetLength(0) - 2; row++)
            {
                for (int col = 0; col < matrix.GetLength(1) - 2; col++)
                {
                    int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
                              matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
                              matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];

                    if (sum > bestSum)
                    {
                        bestSum = sum;
                    }
                }
            }

            Console.WriteLine(bestSum);

        }
    }
}
