#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<60;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
  //  freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           /*
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
           */
           up(ans, dp1[i-1][j] + dp2[i][j-1] + dp3[i+1][j] + dp4[i][j+1]);
           up(ans, dp1[i][j-1] + dp2[i+1][j] + dp3[i][j+1] + dp4[i-1][j]);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<61;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
   // freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           /*
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
           */
           up(ans, dp1[i-1][j] + dp2[i][j-1] + dp3[i+1][j] + dp4[i][j+1]);
           up(ans, dp1[i][j-1] + dp2[i+1][j] + dp3[i][j+1] + dp4[i-1][j]);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<61;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
   // freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<61;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
  //  freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp1[i][j] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp2[i][j] = - inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           /*
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           if (ou != -inf && in != -inf) up(ans, in + ou);
           */
           up(ans, dp1[i-1][j] + dp2[i][j-1] + dp3[i+1][j] + dp4[i][j+1]);
           up(ans, dp1[i][j-1] + dp2[i+1][j] + dp3[i][j+1] + dp4[i-1][j]);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1005;
const ll inf = 1ll<<62;
int n,m,grid[maxn][maxn];
ll dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn], dp4[maxn][maxn];
void up(ll &x,ll y){ if (y > x) x= y;}
int main(){
   // freopen("b.in","r",stdin);
  //  freopen("b.out","w",stdout);
        scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j ++)
        scanf("%d",&grid[i][j]);
    //dp1
    for (int i = 0; i <= n; i ++)
        dp1[i][0] = - inf;
    for (int i = 0; i <= m; i ++)
       dp1[0][i] = - inf;
    dp1[1][1] = grid[1][1];
    for (int i = 1; i <= n; i ++)
      for (int j = 1; j <= n; j ++)
        if (i != 1 || j != 1){
              dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
        }

    //dp2
    for (int i = 0; i <= n; i ++)
      dp2[i][0] = -inf;
    for (int i = 0; i <= m; i ++)
       dp2[n+1][i] = -inf;
    dp2[n][1]=grid[n][1];
    for (int i = n; i >= 1; i --)
      for (int j = 1; j <= m; j ++)
          if (i != n || j != 1)
            dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + grid[i][j];

    //dp3
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
         dp3[i][j] = - inf;
    dp3[n][m]=grid[n][m];
    for (int i = n; i >= 1; i --)
      for (int j = m; j >= 1; j --)
        if (i != n || j != m)
           dp3[i][j] = max(dp3[i+1][j], dp3[i][j+1]) + grid[i][j];

    //dp4
    for (int i = 0; i <= n + 1; i ++)
      for (int j = 0; j <= m + 1; j ++)
        dp4[i][j] = - inf;
    dp4[1][m] = grid[1][m];
    for (int i = 1; i <= n; i ++)
      for (int j = m; j > 0; j --)
         if (i != 1 || j != m)
             dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + grid[i][j];
    //merge
    ll ans=-inf;
    for (int i = 1; i <= n; i ++)
       for (int j = 1; j <= m; j ++){
           ll in=-inf;
           up(in, dp1[i-1][j] + dp2[i+1][j]);
           up(in, dp1[i-1][j] + dp2[i][j-1]);
           up(in, dp1[i][j-1] + dp2[i+1][j]);
           ll ou=-inf;
           up(ou, dp3[i][j+1] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i-1][j]);
           up(ou, dp3[i+1][j] + dp4[i][j+1]);
           up(ans, in + ou);
       }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1005
int bp[M][M] , bn[M][M], gp[M][M], gn[M][M], work[M][M];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    for(int i =1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            scanf("%d",&work[i][j]);
        }
    }

    //boy prev
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            bp[i][j] = max(bp[i-1][j],bp[i][j-1])+work[i][j];
        }
    }

    //girl prev
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;++j)
        {
            gp[i][j] = max(gp[n+1][j],gp[i][j-1])+work[i][j];
        }
    }

    //boy next
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            bn[i][j] = max(bn[i+1][j],bn[i][j+1])+work[i][j];
        }
    }
    //girl next
    for(int i=1;i<=n;++i)
    {
        for(int j=m;j>=1;j--)
        {
            gn[i][j] = max(gn[i-1][j],gn[i][j+1])+work[i][j];
        }
    }
    int res = 0,f,h;
    //meeting point
    for(int i=2;i<n;++i)
    {
        for(int j=2;j<m;++j)
        {
            f = (bp[i][j-1]+bn[i][j+1]+gp[i+1][j]+gn[i-1][j]);
            h = (bp[i-1][j]+bn[i+1][j]+gp[i][j-1]+gn[i][j+1]);

            res = max(res,max(f,h));
        }
    }

    printf("%d\n",res);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1005
#define ll long long
ll bp[M][M] , bn[M][M], gp[M][M], gn[M][M];
int work[M][M];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    for(int i =1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            scanf("%d",&work[i][j]);
        }
    }

    //boy prev
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            bp[i][j] = max(bp[i-1][j],bp[i][j-1])+work[i][j];
        }
    }

    //girl prev
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;++j)
        {
            gp[i][j] = max(gp[n+1][j],gp[i][j-1])+work[i][j];
        }
    }

    //boy next
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            bn[i][j] = max(bn[i+1][j],bn[i][j+1])+work[i][j];
        }
    }
    //girl next
    for(int i=1;i<=n;++i)
    {
        for(int j=m;j>=1;j--)
        {
            gn[i][j] = max(gn[i-1][j],gn[i][j+1])+work[i][j];
        }
    }
    ll res = 0,f,h;
    //meeting point
    for(int i=2;i<n;++i)
    {
        for(int j=2;j<m;++j)
        {
            f = (bp[i][j-1]+bn[i][j+1]+gp[i+1][j]+gn[i-1][j]);
            h = (bp[i-1][j]+bn[i+1][j]+gp[i][j-1]+gn[i][j+1]);

            res = max(res,max(f,h));
        }
    }

    cout<<res;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1004
int bp[M][M];
int bn[M][M];
int gp[M][M];
int gn[M][M];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    int work[n][m];

    for(int i =1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            scanf("%d",&work[i][j]);
        }
    }

    //boy prev
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            bp[i][j] = max(bp[i-1][j],bp[i][j-1])+work[i][j];
        }
    }

    //girl prev
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;++j)
        {
            gp[i][j] = max(gp[i+1][j],gp[i][j-1])+work[i][j];
        }
    }

    //boy next
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            bn[i][j] = max(bn[i+1][j],bn[i][j+1])+work[i][j];
        }
    }
    //girl next
    for(int i=1;i<=n;++i)
    {
        for(int j=m;j>=1;j--)
        {
            gn[i][j] = max(gn[i-1][j],gn[i][j+1])+work[i][j];
        }
    }
    ll res = 0,f,h;
    //meeting point
    for(int i=2;i<n;++i)
    {
        for(int j=2;j<m;++j)
        {
            f = (bp[i][j-1]+bn[i][j+1]+gp[i+1][j]+gn[i-1][j]);
            h = (bp[i-1][j]+bn[i+1][j]+gp[i][j-1]+gn[i][j+1]);

            res = max(res,max(f,h));
        }
    }

    printf("%d",res);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1010
int bp[M][M],bn[M][M],gp[M][M],gn[M][M];
int work[M][M];
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);

    for(int i =1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            scanf("%d",&work[i][j]);
        }
    }
    //boy prev
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            bp[i][j] = max(bp[i-1][j],bp[i][j-1])+work[i][j];
        }
    }

    //girl prev
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;++j)
        {
            gp[i][j] = max(gp[i+1][j],gp[i][j-1])+work[i][j];
        }
    }

    //boy next
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            bn[i][j] = max(bn[i+1][j],bn[i][j+1])+work[i][j];
        }
    }
    //girl next
    for(int i=1;i<=n;++i)
    {
        for(int j=m;j>=1;j--)
        {
            gn[i][j] = max(gn[i-1][j],gn[i][j+1])+work[i][j];
        }
    }
    int res = 0,f,h;
    //meeting point
    for(int i=2;i<n;++i)
    {
        for(int j=2;j<m;++j)
        {
            f = (bp[i][j-1]+bn[i][j+1]+gp[i+1][j]+gn[i-1][j]);
            h = (bp[i-1][j]+bn[i+1][j]+gp[i][j-1]+gn[i][j+1]);

            res = max(res,max(f,h));
        }
    }

    printf("%d",res);

    return 0;
}


#include <bits/stdc++.h>
#define F(i,a,b) for(int i = (int)(a); i <= (int)(b); i++)
#define RF(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
#define MAX 1005
int Boy1[MAX][MAX];
int Boy2[MAX][MAX];
int Girl1[MAX][MAX];
int Girl2[MAX][MAX];
using namespace std;
int main()
{
    int N,M,ans,op1,op2;
    scanf("%d%d",&N,&M);
    int Workout[MAX][MAX];
    ans = 0;

    F(i,1,N)
        F(j,1,M)
            scanf("%d",&Workout[i][j]);

    F(i,1,N)
        F(j,1,M)
            Boy1[i][j] = max(Boy1[i-1][j],Boy1[i][j-1]) + Workout[i][j];

      RF(i,N,1)
        RF(j,M,1)
            Boy2[i][j] = max(Boy2[i+1][j],Boy2[i][j+1]) + Workout[i][j];

    RF(i,N,1)
        F(j,1,M)
            Girl1[i][j] = max(Girl1[i+1][j],Girl1[i][j-1]) + Workout[i][j];

    F(i,1,N)
        RF(j,M,1)
            Girl2[i][j] = max(Girl2[i-1][j],Girl2[i][j+1]) + Workout[i][j];

    F(i,2,N-1)
    {
        F(j,2,M-1)
        {

            op1 = Boy1[i][j-1] + Boy2[i][j+1] + Girl1[i+1][j] + Girl2[i-1][j];

            op2 = Boy1[i-1][j] + Boy2[i+1][j] + Girl1[i][j-1] + Girl2[i][j+1];

            ans = max(ans,max(op1,op2));
        }
    }

    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1004
int bp[M][M];
int bn[M][M];
int gp[M][M];
int gn[M][M];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    int work[n][m];

    for(int i =1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            scanf("%d",&work[i][j]);
        }
    }

    //boy prev
    for(int i=1;i<=n;++i)
    {
        for(int j=1;j<=m;++j)
        {
            bp[i][j] = max(bp[i-1][j],bp[i][j-1])+work[i][j];
        }
    }

    //girl prev
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;++j)
        {
            gp[i][j] = max(gp[i+1][j],gp[i][j-1])+work[i][j];
        }
    }

    //boy next
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            bn[i][j] = max(bn[i+1][j],bn[i][j+1])+work[i][j];
        }
    }
    //girl next
    for(int i=1;i<=n;++i)
    {
        for(int j=m;j>=1;j--)
        {
            gn[i][j] = max(gn[i-1][j],gn[i][j+1])+work[i][j];
        }
    }
    int res = 0,f,h;
    //meeting point
    for(int i=2;i<n;++i)
    {
        for(int j=2;j<m;++j)
        {
            f = (bp[i][j-1]+bn[i][j+1]+gp[i+1][j]+gn[i-1][j]);
            h = (bp[i-1][j]+bn[i+1][j]+gp[i][j-1]+gn[i][j+1]);

            res = max(res,max(f,h));
        }
    }

    printf("%d",res);

    return 0;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(int i=a;i<b;i++)
#define ll long long int

//STL
#define ii pair<int , int >
#define F first
#define S second
#define ic pair<int , char >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define vii vector< ii >
#define vvii vector< vii >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

//Constants
#define pi 3.141592653
#define e 2.7182818284
#define MAXP 1000000
#define MAXN 1005

vi prime ;
void generate_prime()
{
	int is_prime[MAXP+1] ;
	memset(is_prime,0,sizeof(is_prime)) ;
	forall(i,2,MAXP+1)
	{
		if(!is_prime[i])
		{
			prime.pb(i) ;
			int temp = i ;
			while(temp<=MAXP)
			{
				is_prime[temp] = 1 ;
				temp += i ;
			}
		}
	}
}

ll find_power(ll x , ll n)
{
	if(n==0)
		return 1 ;
	if(n==1)
		return x ;
	ll temp = find_power(x , n/2) ;
	temp = temp * temp ;
	if(n%2==0)
		return temp ;
	return (temp*x) ;
}

//Global Variables
int visited[MAXN][MAXN] ;
ll a[MAXN][MAXN] ;
ll f1[MAXN][MAXN] , f2[MAXN][MAXN] , f3[MAXN][MAXN] , f4[MAXN][MAXN] ;

ll find_f(ll f[MAXN][MAXN] , int x,int y,int sx,int sy,int n,int m,int cx,int cy){

	if((x<=0)||(y<=0)||(x>n)||(y>m))
		return 0 ;

	if(visited[x][y])
		return f[x][y] ;
	visited[x][y] = 1 ;

	if((x==sx)&&(y==sy))
		f[x][y] = a[x][y] ;
	else
		f[x][y] = max(find_f(f,x+cx,y,sx,sy,n,m,cx,cy) , find_f(f,x,y+cy,sx,sy,n,m,cx,cy)) + a[x][y] ;

	return f[x][y] ;
}

int main()
{
	int n,m ; s(n) ; s(m) ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			sl(a[i][j]) ;
		}
	}
	memset(visited,0,sizeof(visited)) ;
	memset(f1,0,sizeof(f1)) ;
	find_f(f1,n,m,1,1,n,m,-1,-1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f2,0,sizeof(f2)) ;
	find_f(f2,n,1,1,m,n,m,-1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f3,0,sizeof(f3)) ;
	find_f(f3,1,1,n,m,n,m,+1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f4,0,sizeof(f4));
	find_f(f4,1,m,n,1,n,m,+1,-1) ;

	/*forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f1[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f2[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f3[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f4[i][j]<<"  " ;
		}
		cout<<endl ;
	}*/

	ll ans = 0 ;
	forall(i,1,n+1){
		forall(j,1,m+1){

			if((i==1)||(j==1)||(i==n)||(j==m))
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i+1][j]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i][j+1] + f3[i+1][j]) ;

			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i][j+1] + f3[i+1][j]) ;
		}
	}

	cout<<ans ;
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(int i=a;i<b;i++)
#define ll long long int

//STL
#define ii pair<int , int >
#define F first
#define S second
#define ic pair<int , char >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define vii vector< ii >
#define vvii vector< vii >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

//Constants
#define pi 3.141592653
#define e 2.7182818284
#define MAXP 1000000
#define MAXN 1005

vi prime ;
void generate_prime()
{
	int is_prime[MAXP+1] ;
	memset(is_prime,0,sizeof(is_prime)) ;
	forall(i,2,MAXP+1)
	{
		if(!is_prime[i])
		{
			prime.pb(i) ;
			int temp = i ;
			while(temp<=MAXP)
			{
				is_prime[temp] = 1 ;
				temp += i ;
			}
		}
	}
}

ll find_power(ll x , ll n)
{
	if(n==0)
		return 1 ;
	if(n==1)
		return x ;
	ll temp = find_power(x , n/2) ;
	temp = temp * temp ;
	if(n%2==0)
		return temp ;
	return (temp*x) ;
}

//Global Variables
int visited[MAXN][MAXN] ;
ll a[MAXN][MAXN] ;
ll f1[MAXN][MAXN] , f2[MAXN][MAXN] , f3[MAXN][MAXN] , f4[MAXN][MAXN] ;

ll find_f(ll f[MAXN][MAXN] , int x,int y,int sx,int sy,int n,int m,int cx,int cy){

	if((x<=0)||(y<=0)||(x>n)||(y>m))
		return 0 ;

	if(visited[x][y])
		return f[x][y] ;
	visited[x][y] = 1 ;

	if((x==sx)&&(y==sy))
		f[x][y] = a[x][y] ;
	else
		f[x][y] = max(find_f(f,x+cx,y,sx,sy,n,m,cx,cy) , find_f(f,x,y+cy,sx,sy,n,m,cx,cy)) + a[x][y] ;

	return f[x][y] ;
}

int main()
{
	int n,m ; s(n) ; s(m) ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			sl(a[i][j]) ;
		}
	}
	memset(visited,0,sizeof(visited)) ;
	memset(f1,0,sizeof(f1)) ;
	find_f(f1,n,m,1,1,n,m,-1,-1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f2,0,sizeof(f2)) ;
	find_f(f2,n,1,1,m,n,m,-1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f3,0,sizeof(f3)) ;
	find_f(f3,1,1,n,m,n,m,+1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f4,0,sizeof(f4));
	find_f(f4,1,m,n,1,n,m,+1,-1) ;

	ll ans = 0 ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i][j+1]) ;
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i][j+1] + f3[i+1][j]) ;

			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i][j+1]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i][j+1] + f3[i+1][j]) ;
		}
	}

	cout<<ans ;
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(int i=a;i<b;i++)
#define ll long long int

//STL
#define ii pair<int , int >
#define F first
#define S second
#define ic pair<int , char >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define vii vector< ii >
#define vvii vector< vii >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

//Constants
#define pi 3.141592653
#define e 2.7182818284
#define MAXP 1000000
#define MAXN 1005

vi prime ;
void generate_prime()
{
	int is_prime[MAXP+1] ;
	memset(is_prime,0,sizeof(is_prime)) ;
	forall(i,2,MAXP+1)
	{
		if(!is_prime[i])
		{
			prime.pb(i) ;
			int temp = i ;
			while(temp<=MAXP)
			{
				is_prime[temp] = 1 ;
				temp += i ;
			}
		}
	}
}

ll find_power(ll x , ll n)
{
	if(n==0)
		return 1 ;
	if(n==1)
		return x ;
	ll temp = find_power(x , n/2) ;
	temp = temp * temp ;
	if(n%2==0)
		return temp ;
	return (temp*x) ;
}

//Global Variables
int visited[MAXN][MAXN] ;
ll a[MAXN][MAXN] ;
ll f1[MAXN][MAXN] , f2[MAXN][MAXN] , f3[MAXN][MAXN] , f4[MAXN][MAXN] ;

ll find_f(ll f[MAXN][MAXN] , int x,int y,int sx,int sy,int n,int m,int cx,int cy){

	if((x<=0)||(y<=0)||(x>n)||(y>m))
		return 0 ;

	if(visited[x][y])
		return f[x][y] ;
	visited[x][y] = 1 ;

	if((x==sx)&&(y==sy))
		f[x][y] = a[x][y] ;
	else
		f[x][y] = max(find_f(f,x+cx,y,sx,sy,n,m,cx,cy) , find_f(f,x,y+cy,sx,sy,n,m,cx,cy)) + a[x][y] ;

	return f[x][y] ;
}

int main()
{
	int n,m ; s(n) ; s(m) ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			sl(a[i][j]) ;
		}
	}
	memset(visited,0,sizeof(visited)) ;
	memset(f1,0,sizeof(f1)) ;
	find_f(f1,n,m,1,1,n,m,-1,-1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f2,0,sizeof(f2)) ;
	find_f(f2,n,1,1,m,n,m,-1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f3,0,sizeof(f3)) ;
	find_f(f3,1,1,n,m,n,m,+1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f4,0,sizeof(f4));
	find_f(f4,1,m,n,1,n,m,+1,-1) ;

	/*forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f1[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f2[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f3[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f4[i][j]<<"  " ;
		}
		cout<<endl ;
	}*/

	ll ans = 0 ;
	forall(i,1,n+1){
		forall(j,1,m+1){

			if((i==1)||(j==1)||(i==n)||(j==m))
				continue ;
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i+1][j]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i][j+1] + f3[i+1][j]) ;

			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i][j+1] + f3[i+1][j]) ;
		}
	}

	cout<<ans ;
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(int i=a;i<b;i++)
#define ll long long int

//STL
#define ii pair<int , int >
#define F first
#define S second
#define ic pair<int , char >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define vii vector< ii >
#define vvii vector< vii >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

//Constants
#define pi 3.141592653
#define e 2.7182818284
#define MAXP 1000000
#define MAXN 1005

vi prime ;
void generate_prime()
{
	int is_prime[MAXP+1] ;
	memset(is_prime,0,sizeof(is_prime)) ;
	forall(i,2,MAXP+1)
	{
		if(!is_prime[i])
		{
			prime.pb(i) ;
			int temp = i ;
			while(temp<=MAXP)
			{
				is_prime[temp] = 1 ;
				temp += i ;
			}
		}
	}
}

ll find_power(ll x , ll n)
{
	if(n==0)
		return 1 ;
	if(n==1)
		return x ;
	ll temp = find_power(x , n/2) ;
	temp = temp * temp ;
	if(n%2==0)
		return temp ;
	return (temp*x) ;
}

//Global Variables
int visited[MAXN][MAXN] ;
ll a[MAXN][MAXN] ;
ll f1[MAXN][MAXN] , f2[MAXN][MAXN] , f3[MAXN][MAXN] , f4[MAXN][MAXN] ;

ll find_f(ll f[MAXN][MAXN] , int x,int y,int sx,int sy,int n,int m,int cx,int cy){

	if((x<=0)||(y<=0)||(x>n)||(y>m))
		return 0 ;

	if(visited[x][y])
		return f[x][y] ;
	visited[x][y] = 1 ;

	if((x==sx)&&(y==sy))
		f[x][y] = a[x][y] ;
	else
		f[x][y] = max(find_f(f,x+cx,y,sx,sy,n,m,cx,cy) , find_f(f,x,y+cy,sx,sy,n,m,cx,cy)) + a[x][y] ;

	return f[x][y] ;
}

int main()
{
	int n,m ; s(n) ; s(m) ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			sl(a[i][j]) ;
		}
	}
	memset(visited,0,sizeof(visited)) ;
	memset(f1,0,sizeof(f1)) ;
	find_f(f1,n,m,1,1,n,m,-1,-1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f2,0,sizeof(f2)) ;
	find_f(f2,n,1,1,m,n,m,-1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f3,0,sizeof(f3)) ;
	find_f(f3,1,1,n,m,n,m,+1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f4,0,sizeof(f4));
	find_f(f4,1,m,n,1,n,m,+1,-1) ;

	/*forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f1[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f2[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f3[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f4[i][j]<<"  " ;
		}
		cout<<endl ;
	}*/

	ll ans = 0 ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i+1][j]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i][j+1] + f3[i+1][j]) ;

			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i][j+1] + f3[i+1][j]) ;
		}
	}

	cout<<ans ;
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(int i=a;i<b;i++)
#define ll long long int

//STL
#define ii pair<int , int >
#define F first
#define S second
#define ic pair<int , char >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define vii vector< ii >
#define vvii vector< vii >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

//Constants
#define pi 3.141592653
#define e 2.7182818284
#define MAXP 1000000
#define MAXN 1005

vi prime ;
void generate_prime()
{
	int is_prime[MAXP+1] ;
	memset(is_prime,0,sizeof(is_prime)) ;
	forall(i,2,MAXP+1)
	{
		if(!is_prime[i])
		{
			prime.pb(i) ;
			int temp = i ;
			while(temp<=MAXP)
			{
				is_prime[temp] = 1 ;
				temp += i ;
			}
		}
	}
}

ll find_power(ll x , ll n)
{
	if(n==0)
		return 1 ;
	if(n==1)
		return x ;
	ll temp = find_power(x , n/2) ;
	temp = temp * temp ;
	if(n%2==0)
		return temp ;
	return (temp*x) ;
}

//Global Variables
int visited[MAXN][MAXN] ;
ll a[MAXN][MAXN] ;
ll f1[MAXN][MAXN] , f2[MAXN][MAXN] , f3[MAXN][MAXN] , f4[MAXN][MAXN] ;

ll find_f(ll f[MAXN][MAXN] , int x,int y,int sx,int sy,int n,int m,int cx,int cy){

	if((x<=0)||(y<=0)||(x>n)||(y>m))
		return 0 ;

	if(visited[x][y])
		return f[x][y] ;
	visited[x][y] = 1 ;

	if((x==sx)&&(y==sy))
		f[x][y] = a[x][y] ;
	else
		f[x][y] = max(find_f(f,x+cx,y,sx,sy,n,m,cx,cy) , find_f(f,x,y+cy,sx,sy,n,m,cx,cy)) + a[x][y] ;

	return f[x][y] ;
}

int main()
{
	int n,m ; s(n) ; s(m) ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			sl(a[i][j]) ;
		}
	}
	memset(visited,0,sizeof(visited)) ;
	memset(f1,0,sizeof(f1)) ;
	find_f(f1,n,m,1,1,n,m,-1,-1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f2,0,sizeof(f2)) ;
	find_f(f2,n,1,1,m,n,m,-1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f3,0,sizeof(f3)) ;
	find_f(f3,1,1,n,m,n,m,+1,+1) ;

	memset(visited,0,sizeof(visited)) ;
	memset(f4,0,sizeof(f4));
	find_f(f4,1,m,n,1,n,m,+1,-1) ;

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f1[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f2[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f3[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	forall(i,0,n+2){
		forall(j,0,m+2){
			cout<<f4[i][j]<<"  " ;
		}
		cout<<endl ;
	}

	ll ans = 0 ;
	forall(i,1,n+1){
		forall(j,1,m+1){
			ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i-1][j] + f3[i+1][j]) ;
			//ans = max(ans , f1[i][j-1] + f4[i+1][j] + f2[i][j+1] + f3[i+1][j]) ;

			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i][j+1]) ;
			//ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i-1][j] + f3[i+1][j]) ;
			ans = max(ans , f1[i-1][j] + f4[i][j-1] + f2[i][j+1] + f3[i+1][j]) ;
		}
	}

	cout<<ans ;
	return 0 ;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ll cur = dp[i][j - 1][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);

		cur = dp[i - 1][j][0] + dp[i][j - 1][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);

		cur = dp[i - 1][j][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);

		cur = dp[i][j - 1][0] + dp[i][j - 1][2];
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ll cur = dp[i][j - 1][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);

		cur = dp[i - 1][j][0] + dp[i][j - 1][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);

		cur = dp[i - 1][j][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		int cur = dp[i][j - 1][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		cur = dp[i - 1][j][0] + dp[i][j - 1][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		cur = dp[i - 1][j][0] + dp[i + 1][j][2];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ans = max(ans,dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3] -
				4 * a[i][j]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		if(j > 1 && j < m && i < n && i > 1)
		ans = max(ans,dp[i][j - 1][0] + dp[i][j + 1][1] + dp[i + 1][j][2] +
				dp[i - 1][j][3]);
		if(i > 1 && j > 1 && i < n && j < m)
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i][j - 1][2] +
				dp[i][j + 1][3]);

	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ans = max(ans,dp[i][j - 1][0] + dp[i][j + 1][1] + dp[i + 1][j][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i][j - 1][2] +
				dp[i][j + 1][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		if(j > 1 && i < n)
		ans = max(ans,dp[i][j - 1][0] + dp[i][j + 1][1] + dp[i + 1][j][2] +
				dp[i - 1][j][3]);
		if(i > 1 && j > 1)
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i][j - 1][2] +
				dp[i][j + 1][3]);

	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ll cur = dp[i][j][0] + dp[i][j][2] - 2 * a[i][j];
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i - 1][j][3]);
		ans = max(ans,cur + dp[i + 1][j][1] + dp[i][j + 1][3]);
		ans = max(ans,cur + dp[i][j + 1][1] + dp[i - 1][j][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

ll dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	ll ans = 0;
	REP(i,n) REP(j,m){
		ans = max(ans,dp[i][j - 1][0] + dp[i][j + 1][1] + dp[i + 1][j][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i][j - 1][2] +
				dp[i][j + 1][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i - 1][j][2] +
				dp[i][j + 1][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i][j + 1][1] + dp[i + 1][j][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i][j + 1][1] + dp[i][j - 1][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i - 1][j][0] + dp[i + 1][j][1] + dp[i][j - 1][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i][j - 1][0] + dp[i + 1][j][1] + dp[i - 1][j][2] +
				dp[i - 1][j][3]);
		ans = max(ans,dp[i][j - 1][0] + dp[i + 1][j][1] + dp[i - 1][j][2] +
				dp[i][j + 1][3]);
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i=1;i<=(n);++i)
#define REV(i,n) for(int i=(n);i>=1;--i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)

typedef long long ll;
typedef pair<int,int> pii;
const int INF = (1 << 30) - 1;
const int MAXN = 1010;

int dp[MAXN][MAXN][4];
int n,m;
int a[MAXN][MAXN];

int main(){
	scanf("%d%d",&n,&m);
	REP(i,n) REP(j,m) scanf("%d",&a[i][j]);
	REP(i,n){
		REP(j,m){
			dp[i][j][0] = max(dp[i - 1][j][0],dp[i][j - 1][0]) + a[i][j];
		}
	}
	REV(i,n){
		REV(j,m){
			dp[i][j][1] = max(dp[i + 1][j][1],dp[i][j + 1][1]) + a[i][j];
		}
	}
	REV(i,n){
		REP(j,m){
			dp[i][j][2] = max(dp[i + 1][j][2],dp[i][j - 1][2]) + a[i][j];
		}
	}
	REP(i,n){
		REV(j,m){
			dp[i][j][3] = max(dp[i - 1][j][3],dp[i][j + 1][3]) + a[i][j];
		}
	}
	int ans = 0;
	REP(i,n) REP(j,m){
		ans = max(ans,dp[i][j][0] + dp[i][j][1] + dp[i][j][2] + dp[i][j][3] -
				4 * a[i][j]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <set>
#include <stdlib.h>

using namespace std;

int n, m, x, k;
long long a[1002][1002];
long long pl[1004][1004];
long long pr[1004][1004];
long long pd[1004][1004];
long long pp[1004][1004];

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int t;
            scanf("%d", &t);
            a[i + 1][j + 1] = t;
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
                pl[i][j] = max(pl[i - 1][j], pl[i][j - 1]) + a[i][j];
        }
    }
    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j <= m; j++)
        {
                pr[i][j] = max(pr[i + 1][j], pr[i][j - 1]) + a[i][j];
        }
    }
    for (int i = n; i >= 1; i--)
    {
        for (int j = m; j >= 1; j--)
        {
                pd[i][j] = max(pd[i + 1][j], pd[i][j + 1] + a[i][j]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = m; j >= 1; j--)
        {
                pp[i][j] = max(pp[i - 1][j], pp[i][j + 1] + a[i][j]);
        }
    }

    long long res = 0;
    for (int i = 2; i <= n - 1; i++)
    {
        for (int j = 2; j <= m - 1; j++)
        {
            res = max(res, pl[i - 1][j] + pd[i + 1][j] + pr[i][j - 1] + pp[i][j + 1]);
            res = max(res, pl[i][j - 1] + pd[i][j + 1] + pr[i + 1][j] + pp[i - 1][j]);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <set>
#include <stdlib.h>

using namespace std;

int n, m, x, k;
long long a[1002][1002];
long long pl[1004][1004];
long long pr[1004][1004];
long long pd[1004][1004];
long long pp[1004][1004];

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%I64d", &a[i + 1][j + 1]);
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
                pl[i][j] = max(pl[i][j], pl[i][j - 1] + a[i][j]);
                pl[i][j] = max(pl[i][j], pl[i - 1][j] + a[i][j]);
        }
    }

    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j <= m; j++)
        {
                pr[i][j] = max(pr[i][j], pr[i][j - 1] + a[i][j]);
                pr[i][j] = max(pr[i][j], pr[i + 1][j] + a[i][j]);
        }
    }
    for (int i = n; i >= 1; i--)
    {
        for (int j = m; j >= 1; j--)
        {
                pd[i][j] = max(pd[i][j], pd[i][j + 1] + a[i][j]);
                pd[i][j] = max(pd[i][j], pd[i + 1][j] + a[i][j]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = m; j >= 1; j--)
        {
                pp[i][j] = max(pp[i][j], pp[i][j + 1] + a[i][j]);
                pp[i][j] = max(pp[i][j], pp[i - 1][j] + a[i][j]);
        }
    }

    long long res = 0;
    for (int i = 2; i <= n - 1; i++)
    {
        for (int j = 2; j <= m - 1; j++)
        {
            res = max(res, pl[i - 1][j] + pd[i + 1][j] + pr[i][j - 1] + pp[i][j + 1]);
            res = max(res, pl[i][j - 1] + pd[i][j + 1] + pr[i + 1][j] + pp[i - 1][j]);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <set>
#include <stdlib.h>

using namespace std;

int n, m, x, k;
long long a[1002][1002];
long long pl[1004][1004];
long long pr[1004][1004];
long long pd[1004][1004];
long long pp[1004][1004];

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int t;
            scanf("%d", &t);
            a[i + 1][j + 1] = t;
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
                pl[i][j] = max(pl[i][j], pl[i][j - 1] + a[i][j]);
                pl[i][j] = max(pl[i][j], pl[i - 1][j] + a[i][j]);
        }
    }

    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j <= m; j++)
        {
                pr[i][j] = max(pr[i][j], pr[i][j - 1] + a[i][j]);
                pr[i][j] = max(pr[i][j], pr[i + 1][j] + a[i][j]);
        }
    }
    for (int i = n; i >= 1; i--)
    {
        for (int j = m; j >= 1; j--)
        {
                pd[i][j] = max(pd[i][j], pd[i][j + 1] + a[i][j]);
                pd[i][j] = max(pd[i][j], pd[i + 1][j] + a[i][j]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = m; j >= 1; j--)
        {
                pp[i][j] = max(pp[i][j], pp[i][j + 1] + a[i][j]);
                pp[i][j] = max(pp[i][j], pp[i - 1][j] + a[i][j]);
        }
    }

    long long res = 0;
    for (int i = 2; i <= n - 1; i++)
    {
        for (int j = 2; j <= m - 1; j++)
        {
            res = max(res, pl[i - 1][j] + pd[i + 1][j] + pr[i][j - 1] + pp[i][j + 1]);
            res = max(res, pl[i][j - 1] + pd[i][j + 1] + pr[i + 1][j] + pp[i - 1][j]);
        }
    }
    cout << res << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
#define inf 2e9
map<char,vector<int> > poss;
template<typename TYPE>
	void operator+=(vector<TYPE> &x,TYPE y)
	{
		x.push_back(y);
	}
vector<ll> ans;
map<char,map<int,int> > mem;
int fin(char aim,int pos,string s)
{
	if(poss[aim].empty())
		return s.size();
	if(mem[aim][pos])
		return mem[aim][pos];
	int mndif=inf;
	for(int i=0;i<poss[aim].size();i++)
	{
		if(abs(pos-poss[aim][i])>mndif)
			break;
		mndif=abs(pos-poss[aim][i]);
	}
	return mem[aim][pos]=mndif;
}
int main()
{
	int n,len,i;
	string cor;
	cin>>n>>len>>cor;
	for(i=0;i<len;i++)
		poss[cor[i]]+=i;
	for(i=1;i<=n;i++)
	{
		string err;
		cin>>err;
		ll cost=0;
		for(int j=0;j<err.size();j++)
			cost+=fin(err[j],j,err);
		ans+=cost;
	}
	for(i=0;i<n;i++)
		cout<<ans[i]<<"\n";
	return 0;
}
/*1
2 10
codeforces
codeforces
codehorses
*/
/*2
9 9
vkontakte
vcontacte
vkontrakte
vkollapse
vkrokodile
vtopke
vkapuste
vpechke
vk
vcodeforcese
*/


#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define itr(type) type::iterator
map<char,vector<int> > poss;
const int inf=(1<<30)-1+(1<<30);
template<typename TYPE>
	void operator+=(vector<TYPE> &x,TYPE y)
	{
		x.push_back(y);
	}
vector<ll> ans;
map<char,map<int,int> > mem;
int fin(char aim,int pos,string s)
{
	if(poss[aim].empty())
		return s.size();
	if(mem[aim][pos])
		return mem[aim][pos];
	int mndif=inf;
	itr(vector<int>) beg=poss[aim].begin(),end=poss[aim].end();
	itr(vector<int>) lwb=lower_bound(beg,end,pos),upb=upper_bound(beg,end,pos)
	-1;
	if(lwb<end)
		mndif=*lwb-pos;
	if(upb>=beg)
		mndif=min(mndif,pos-*upb);
	return mem[aim][pos]=mndif;
}
int main()
{
	int n,len,i;
	string cor;
	cin>>n>>len>>cor;
	for(i=0;i<len;i++)
		poss[cor[i]]+=i;
	for(i=1;i<=n;i++)
	{
		string err;
		cin>>err;
		ll cost=0;
		for(int j=0;j<err.size();j++)
			cost+=fin(err[j],j,err);
		ans+=cost;
	}
	for(i=0;i<n;i++)
		cout<<ans[i]<<"\n";
	return 0;
}
/*1
2 10
codeforces
codeforces
codehorses
*/
/*2
9 9
vkontakte
vcontacte
vkontrakte
vkollapse
vkrokodile
vtopke
vkapuste
vpechke
vk
vcodeforcese
*/


#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
#define inf 2e9
map<char,vector<int> > poss;
template<typename TYPE>
	void operator+=(vector<TYPE> &x,TYPE y)
	{
		x.push_back(y);
	}
vector<ll> ans;
map<char,map<int,int> > mem;
int fin(char aim,int pos,string s)
{
	if(poss[aim].empty())
		return s.size();
	if(mem[aim][pos])
		return mem[aim][pos];
	int mndif=inf;
	for(int i=0;i<poss[aim].size();i++)
		mndif=min(abs(pos-poss[aim][i]),mndif);
	return mem[aim][pos]=mndif;
}
int main()
{
	int n,len,i;
	string cor;
	cin>>n>>len>>cor;
	for(i=0;i<len;i++)
		poss[cor[i]]+=i;
	for(i=1;i<=n;i++)
	{
		string err;
		cin>>err;
		ll cost=0;
		for(int j=0;j<err.size();j++)
			cost+=fin(err[j],j,err);
		ans+=cost;
	}
	for(i=0;i<n;i++)
		cout<<ans[i]<<"\n";
	return 0;
}
/*1
2 10
codeforces
codeforces
codehorses
*/
/*3
9 9
vkontakte
vcontacte
vkontrakte
vkollapse
vkrokodile
vtopke
vkapuste
vpechke
vk
vcodeforcese
*/


#include<bits/stdc++.h>
using namespace std;
int m,n;
string s,s1[100005];
int coss[200005][30];
bool vis[200005];
int maxx;
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<maxx;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	break;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<n;i++)
	{
	cin>>s1[i];
	int k=s1[i].size();
	maxx=max(maxx,k);
	}
	for(int i=0;i<200001;i++)
	vis[i]=false;
	for(int i=0;i<200001;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	int ans=0;
	for(int i=0;i<n;i++)
	{
	ans=0;
	cin>>s1[i];
	int k=s1[i].size();
	//cout<<k<<endl;
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[i][j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[i][j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n;
string s;
int coss[200005][30];
bool vis[200005];
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<m;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	break;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<200001;i++)
	vis[i]=false;
	for(int i=0;i<200001;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	for(int i=0;i<n;i++)
	{
	int ans=0;
	string s1;
	cin>>s1;
	int k=s1.size();
	//cout<<k<<endl;
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n,a[100005],b[100005];
string s;
int coss[2000500][260000];
bool vis[200005];
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<m;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<200005;i++)
	vis[i]=false;
	for(int i=0;i<200005;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	for(int i=0;i<n;i++)
	{
	int ans=0;
	string s1;
	cin>>s1;
	int k=s1.size();
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n,a[100005],b[100005];
string s;
int coss[200005][26];
bool vis[200005];
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<m;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<200005;i++)
	vis[i]=false;
	for(int i=0;i<200005;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	for(int i=0;i<n;i++)
	{
	int ans=0;
	string s1;
	cin>>s1;
	int k=s1.size();
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mm=2e6+9;
class node
{
public:char s;
int id;
}f[mm];
bool vis[777];
long long sum;
int n,m;
char s[mm],ss[mm];
bool cmp(node a,node b)
{
if(a.s!=b.s)
     return a.s<b.s;
    else return a.id<b.id;
    }
    inline int aabs(int x)
    {  return x<0?-x:x;}
    int blook(char z,int num)
    {  int l=0,r=m-1,mid;
    int ret=999999;
    while(l<=r)
{
    mid=(l+r)/2;
    if(f[mid].s>z)r=mid-1;
    else if(f[mid].s<z)l=mid+1;
	else
	{
	int zz=aabs(num-f[mid].id);
	if(ret>zz)ret=zz;
	if(f[mid].id>num)r=mid-1;
	else l=mid+1;
	}
	}
	return ret;
	}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
    memset(vis,0,sizeof(vis));
    scanf("%s",s);
	for(int i=0;s[i];++i)
	{
	vis[s[i]]=1;
	f[i].s=s[i];f[i].id=i;
	}
	sort(f,f+m,cmp);
	for(int i=0;i<n;++i)
	{
	sum=0;
	scanf("%s",ss);
	long long len=strlen(ss);
	for(int j=0;ss[j];++j)
	if(vis[ss[j]])
	sum+=blook(ss[j],j);
	else sum+=len;
	cout<<sum<<"\n";
	}
	}
	}


#include<bits/stdc++.h>
using namespace std;
int m,n;
string s,s1[100005];
int coss[200005][30];
bool vis[200005],viss[30];
int maxx;
void dfs(int sitt,int cha)
{
        //char e='a'+cha;
        //cout<<sitt<<" "<<e<<endl;
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<maxx;i++)
	{
	    //cout<<i<<endl;
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	     continue;
	}
	//vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;

	dfs(i,cha);
	return;
	break;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;

	for(int i=0;i<n;i++)
	{
	cin>>s1[i];
	int k=s1[i].size();
	maxx=max(maxx,k);
	}
	//cout<<maxx<<endl;
	for(int i=0;i<200001;i++)
	vis[i]=false;
	for(int i=0;i<200001;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(viss[s[i]])continue;
	viss[s[i]]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	     //cout<<endl;
	}
	/*for(int i=0;i<26;i++)
	for(int j=0;j<15;j++)
	if(coss[j][i]!=-1)
	{
	   char p=i+'a';
	   cout<<coss[j][i]<<" "<<p<<" "<<j<<endl;
	}*/

	int ans=0;
	for(int i=0;i<n;i++)
	{
	ans=0;
	int k=s1[i].size();
	//cout<<k<<endl;
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[i][j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[i][j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n,a[100005],b[100005];
string s;
int coss[2000050000][26];
bool vis[200005];
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<m;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<200005;i++)
	vis[i]=false;
	for(int i=0;i<200005;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	for(int i=0;i<n;i++)
	{
	int ans=0;
	string s1;
	cin>>s1;
	int k=s1.size();
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mm=2e6+9;
class node
{
public:char s;
int id;
}f[mm];
bool vis[777];
long long sum;
int n,m;
char s[mm],ss[mm];
bool cmp(node a,node b)
{
if(a.s!=b.s)
     return a.s<b.s;
    else return a.id<b.id;
    }
    inline int aabs(int x)
    {  return x<0?-x:x;}
    int blook(char z,int num)
    {  int l=0,r=m-1,mid;
    int ret=999999;
    while(l<=r)
{
    mid=(l+r)/2;
    if(f[mid].s>z)r=mid-1;
    else if(f[mid].s<z)l=mid+1;
	else
	{
	int zz=aabs(num-f[mid].id);
	if(ret>zz)ret=zz;
	if(f[mid].id>num)r=mid-1;
	else l=mid+1;
	}
	}
	return ret;
	}
int main()
{   while(scanf("%d%d",&n,&m)!=EOF)
{
    memset(vis,0,sizeof(vis));
    scanf("%s",s);
	for(int i=0;s[i];++i)
	{
	vis[s[i]]=1;
	f[i].s=s[i];f[i].id=i;
	}
	sort(f,f+m,cmp);
	for(int i=0;i<n;++i)
	{
	sum=0;
	scanf("%s",ss);
	long long len=strlen(ss);
	for(int j=0;ss[j];++j)
	if(vis[ss[j]])
	sum+=blook(ss[j],j);
	else sum+=len;
	cout<<sum<<"\n";
	}
	}
	}


#include<bits/stdc++.h>
using namespace std;
int m,n;
string s,s1[100005];
int coss[200005][30];
bool vis[200005];
int maxx;
void dfs(int sitt,int cha)
{
        //char e='a'+cha;
         //cout<<sitt<<" "<<e<<endl;
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<maxx;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	     return;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	break;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<n;i++)
	{
	cin>>s1[i];
	int k=s1[i].size();
	maxx=max(maxx,k);
	}
	for(int i=0;i<200001;i++)
	vis[i]=false;
	for(int i=0;i<200001;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[s[i]])continue;
	vis[s[i]]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	     //cout<<endl;
	}
	/*for(int i=0;i<10;i++)
	for(int j=0;j<26;j++)
	if(coss[i][j]!=-1)
	cout<<coss[i][j]<<" "<<i+1<<" "<<j<<endl;*/
	int ans=0;
	for(int i=0;i<n;i++)
	{
	ans=0;
	int k=s1[i].size();
	//cout<<k<<endl;
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[i][j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[i][j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n;
string s,s1[100005];
int coss[200005][30];
bool vis[200005],viss[30];
int maxx;
void dfs(int sitt,int cha)
{
        //char e='a'+cha;
        //cout<<sitt<<" "<<e<<endl;
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<maxx;i++)
	{
	    //cout<<i<<endl;
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	     continue;
	}
	//vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;

	dfs(i,cha);
	return;
	break;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;

	for(int i=0;i<n;i++)
	{
	cin>>s1[i];
	int k=s1[i].size();
	maxx=max(maxx,k);
	}
	//cout<<maxx<<endl;
	for(int i=0;i<200001;i++)
	vis[i]=false;
	for(int i=0;i<200001;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(viss[s[i]])continue;
	viss[s[i]]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-1;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	     //cout<<endl;
	}
	/*for(int i=0;i<26;i++)
	for(int j=0;j<15;j++)
	if(coss[j][i]!=-1)
	{
	   char p=i+'a';
	   cout<<coss[j][i]<<" "<<p<<" "<<j<<endl;
	}*/

	long long ans=0;
	for(int i=0;i<n;i++)
	{
	ans=0;
	int k=s1[i].size();
	//cout<<k<<endl;
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[i][j]-'a']==-1)
	ans+=k;
	else
	ans+=coss[j][s1[i][j]-'a'];
	//cout<<ans<<" "<<j<<endl;
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n,a[100005],b[100005];
string s;
int coss[200005][26];
bool vis[200005];
void dfs(int sitt,int cha)
{
	coss[sitt][cha]=0;
	for(int i=sitt+1;i<m;i++)
	{
	if(cha!=s[i]-'a')
	{
	coss[i][cha]=i-sitt;
	continue;
	}
	vis[i]=true;
	for(int j=i-1;j>=(i+sitt+1)/2;j--)
	coss[j][cha]=i-j;
	dfs(i,cha);
	return;
	}
	return;
}
int main()
{
	cin>>n>>m;
	cin>>s;
	for(int i=0;i<200005;i++)
	vis[i]=false;
	for(int i=0;i<200005;i++)
	for(int j=0;j<26;j++)
	coss[i][j]=-1;
	for(int i=0;i<m;i++)
	{
	if(vis[i])continue;
	vis[i]=true;
	int pr=s[i]-'a';
	coss[i][pr]=0;
	for(int j=i-i;j>=0;j--)
	coss[j][pr]=i-j;
	dfs(i,pr);
	}
	//for(int i=0;i<10;i++)
	//for(int j=0;j<10;j++)
	//cout<<coss[i][j]<<" "<<i<<" "<<j<<endl;
	for(int i=0;i<n;i++)
	{
	int ans=0;
	string s1;
	cin>>s1;
	int k=s1.size();
	for(int j=0;j<k;j++)
	{
	if(coss[j][s1[j]-'a']==-1)ans+=k;
	else
	ans+=coss[j][s1[j]-'a'];
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
using namespace std;
int n,k,ls,lc;
string c,s;
long long ans,temp;
set<int> se[27];
set<int>::const_iterator li,ui;
const int maxx=114514893;
int main()
{
	scanf("%d%d",&n,&k);
	cin>>c;
	lc=c.length();
	for(register int i=0;i<=26;++i)
	{
		se[i].insert(-maxx);
		se[i].insert(maxx);
	}
	for(register int i=0;i<lc;++i)
	{
		se[c[i]-'a'+1].insert(i);
	}
	for(register int i=1;i<=n;++i)
	{
		cin>>s;
		ans=0;
		ls=s.length();
		for(register int j=0;j<ls;++j)
		{
			ui=se[s[j]-'a'+1].upper_bound(j);
			li=ui;
			--li;
			temp=min(j-*li,*ui-j);
			if(temp<=200000)
			{
				ans+=temp;
			}
			else
			{
				ans+=ls;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,k;
string c,s;
vector<int>g[26];

int main(){
	cin>>n>>k>>s;
	for(int i=0;i<s.size();i++)g[s[i]-'a'].push_back(i);
	while(n--){
		cin>>c;
		vector<int>::iterator it;
		int ans=0;
		for(int i=0;i<c.size();i++){
			int d=c[i]-'a';
			if(g[d].empty()){
				ans+=c.size();
			}
			else{
				it=lower_bound(g[d].begin(),g[d].end(),i);
				if(it==g[d].end()){
					ans+=abs(*(it-1)-i);
				}
				else{
					int x=abs(*it-i),y=abs(*(it-1)-i);
					if(x>y)ans+=y;
					else ans+=x;
				}
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,k;
string c,s;
vector<int>g[26];

int main(){
	cin>>n>>k>>s;
	for(int i=0;i<s.size();i++)g[s[i]-'a'].push_back(i);
	while(n--){
		cin>>c;
		vector<int>::iterator it;
		long long int ans=0;
		for(int i=0;i<c.size();i++){
			int d=c[i]-'a';
			if(g[d].empty()){
				ans+=c.size();
			}
			else{
				it=lower_bound(g[d].begin(),g[d].end(),i);
				if(it==g[d].end()){
					ans+=abs(*(it-1)-i);
				}
				else{
					int x=abs(*it-i),y=abs(*(it-1)-i);
					if(x>y)ans+=y;
					else ans+=x;
				}
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


//Be Name Khoda
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 7;
const ll inf = (ll)1e18;
const int infint = (int)2e9;
const int alpha = 26;
int f(char c)
{
	return c - 'a';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n, len;
	cin >> n >> len;
	string cur;
	cin >> cur;

	int left[len + 5][alpha], right[len + 5][alpha];
	for (int i = 0; i < len; i++)
		for (int j = 0; j < alpha; j++)
			left[i][j] = infint, right[i][j] = infint;
	for (int i = 0; i < len; i++)
	{
		left[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != 0)
				left[i][j] = min(left[i - 1][j] + 1, left[i][j]);
	}
	for (int i = len - 1; i >= 0; i--)
	{
		right[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != len - 1)
				right[i][j] = min(right[i + 1][j] + 1, right[i][j]);
	}
	for (int i = 0; i < n; i++)
	{
		ll ans = 0;
		string s;
		cin >> s;
		for (int j = 0; j < s.size(); j++)
		{
			if(j >= len)
			{
				ll cnt = left[len - 1][f(s[j])] + (j - (len - 1));
				if(cnt >= infint)
					ans += (int)s.size();
				else
				ans += cnt;
			}
			else
			{
				ll cnt = min(left[j][f(s[j])], right[j][f(s[j])]);
				if(cnt >= infint)
					ans += (int)s.size();
				else
					ans += cnt;
			}
		}
		cout << ans << endl;
	}
}


//Be Name Khoda
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 7;
const ll inf = (ll)1e18;
const int infint = (int)2e9;
const int alpha = 26;
int f(char c)
{
	return c - 'a';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n, len;
	cin >> n >> len;
	string cur;
	cin >> cur;

	int left[len + 5][alpha], right[len + 5][alpha];
	for (int i = 0; i < len; i++)
		for (int j = 0; j < alpha; j++)
			left[i][j] = infint, right[i][j] = infint;
	for (int i = 0; i < len; i++)
	{
		left[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != 0)
				left[i][j] = min(left[i - 1][j] + 1, left[i][j]);
	}
	for (int i = len - 1; i >= 0; i--)
	{
		right[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != len - 1)
				right[i][j] = min(right[i + 1][j] + 1, right[i][j]);
	}
	for (int i = 0; i < n; i++)
	{
		ll ans = 0;
		string s;
		cin >> s;
		for (int j = 0; j < s.size(); j++)
		{
			if(j >= len)
			{
				ll cnt = right[len - 1][f(s[j])] + (j - (len - 1));
				if(cnt >= infint)
					ans += (int)s.size();
				else
				ans += cnt;
			}
			else
			{
				ll cnt = min(left[j][f(s[j])], right[j][f(s[j])]);
				if(cnt >= infint)
					ans += (int)s.size();
				else
					ans += cnt;
			}
		}
		cout << ans << endl;
	}
}


//Be Name Khoda
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 7;
const ll inf = (ll)1e18;
const int infint = (int)2e9;
const int alpha = 26;
int f(char c)
{
	return c - 'a';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n, len;
	cin >> n >> len;
	string cur;
	cin >> cur;

	int left[len + 5][alpha], right[len + 5][alpha];
	for (int i = 0; i < len; i++)
		for (int j = 0; j < alpha; j++)
			left[i][j] = infint, right[i][j] = infint;
	for (int i = 0; i < len; i++)
	{
		left[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != 0)
				left[i][j] = min(left[i - 1][j] + 1, left[i][j]);
	}
	for (int i = len - 1; i >= 0; i--)
	{
		right[i][f(cur[i])] = 0;
		for (int j = 0; j < alpha; j++)
			if(i != len - 1)
				right[i][j] = min(right[i + 1][j] + 1, right[i][j]);
	}

	for (int i = 0; i < n; i++)
	{
		ll ans = 0;
		string s;
		cin >> s;
		for (int j = 0; j < s.size(); j++)
		{
			if(j >= len)
				ans += min(right[len - 1][f(s[j])] + (j - (len - 1)), (int)s.size());
			else
				ans += min(min(left[j][f(s[j])], right[j][f(s[j])]), (int)s.size());
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define INF 1000000009
#define MOD 1000000007
#define ll long long

using namespace std;

int n, k;
string s, t;
set<int> S[26];
bool us[26];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++)
        S[s[i] - 97].insert(i), us[s[i] - 97] = 1;
    while(n--) {
        cin >> t;
        int ans = 0;
        for (int i = 0; i < t.length(); i++) {
            if (!us[t[i] - 97]) ans += t.length();
            else {
                set<int>::iterator it = S[t[i] - 97].lower_bound(i);
                int x = abs((*it) - i);
                if (it !=S[t[i] - 97].begin()) it--;
                ans += min(x, abs(i - (*it)));
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define INF 1000000009
#define MOD 1000000007
#define ll long long

using namespace std;

int n, k;
string s, t;
vector <int> v[26];
bool us[26];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++)
        v[s[i] - 97].pb(i), us[s[i] - 97] = 1;
    while(n--) {
        cin >> t;
        int ans = 0;
        for (int i = 0; i < t.length(); i++) {
            if (!us[t[i] - 97]) ans += t.length();
            else {
                int l = -1, r = v[t[i] - 97].size();
                while(r - l > 1) {
                    int mid = (l + r);
                    if (v[t[i] - 97][mid] >= i) r = mid;
                    else l = mid;
                }
                int x = INF;
                if (r != v[t[i] - 97].size()) x = v[t[i] - 97][r] - i;
                l = -1, r = v[t[i] - 97].size();
                while(r - l > 1) {
                    int mid = (l + r);
                    if (v[t[i] - 97][mid] <= i) l = mid;
                    else r = mid;
                }
                if (l != -1) x = min(x, i - v[t[i] - 97][l]);
                ans += x;
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define INF 1000000009
#define MOD 1000000007
#define ll long long

using namespace std;

int n, k;
string s, t;
vector <int> v[26];
bool us[26];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++)
        v[s[i] - 97].pb(i), us[s[i] - 97] = 1;
    while(n--) {
        cin >> t;
        int ans = 0;
        for (int i = 0; i < t.length(); i++) {
            if (!us[t[i] - 97]) ans += t.length();
            else {
                int l = -1, r = v[t[i] - 97].size();
                while(r - l > 1) {
                    int mid = (l + r);
                    if (v[t[i] - 97][mid] >= i) r = mid;
                    else l = mid;
                }
                int x = INF;
                if (r != v[t[i] - 97].size()) x = v[t[i] - 97][r] - i;
                l = r - 1;
                if (l != -1) x = min(x, i - v[t[i] - 97][l]);
                ans += x;
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define INF 1000000009
#define MOD 1000000007
#define ll long long

using namespace std;

int n, k;
string s, t;
vector <int> v[26];
bool us[26];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++)
        v[s[i] - 97].pb(i), us[s[i] - 97] = 1;
    while(n--) {
        cin >> t;
        ll ans = 0;
        for (int i = 0; i < t.length(); i++) {
            if (!us[t[i] - 97]) ans += t.length();
            else {
                int l = -1, r = v[t[i] - 97].size();
                while(r - l > 1) {
                    int mid = (l + r);
                    if (v[t[i] - 97][mid] >= i) r = mid;
                    else l = mid;
                }
                int x = INF;
                if (r != v[t[i] - 97].size()) x = v[t[i] - 97][r] - i;
                l = r - 1;
                if (l != -1) x = min(x, i - v[t[i] - 97][l]);
                ans += x;
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s,s1;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		cin>>s1;
		int res=0;
		for(int j=0;j<s1.size();j++)
		{
			int minlen=9999;
			for(int k=0;k<s.size();k++)
			{
				if(s[k]==s1[j])minlen=min(minlen,abs(k-j));
			}
			if(minlen==9999)res+=s1.size();
			else res+=minlen;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k,temp,len;
	long long int ans;
	string s,c;
	vector<int>pos[26];
	vector<int>::iterator u;
	cin>>n>>k>>s;
	for(int i=0;i<k;i++)pos[s[i]-'a'].push_back(i);
	for(int i=0;i<n;i++)
	{
		ans=0;
		cin>>c;
		len=(int)c.size();
		for(int j=0;j<len;j++)
		{
			if(pos[c[j]-'a'].empty())ans+=len;
			else
			{
				u=lower_bound(pos[c[j]-'a'].begin(),pos[c[j]-'a'].end(),j);
				if(u==pos[c[j]-'a'].end())
				{
					u--;
					ans+=j-*u;
				}
				else if(u==pos[c[j]-'a'].begin())ans+=*u-j;
				else
				{
					temp=*u-j;
					u--;
					temp=min(temp,j-*u);
					ans+=temp;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char s[210000],c[210000];
int n,k;
vector<int>p[50];
long long add(int v,int k)
{
	int l=0,r=p[v].size()-1;
	int res=abs(p[v][0]-k);
	res=min(res,abs(p[v].back()-k));
	while(r-l>1)
	{
		int key=(l+r)/2;
		if(p[v][key]<=k)l=key;
		else r=key;
	}
	res=min(res,abs(p[v][l]-k));
	res=min(res,abs(p[v][r]-k));
	return res;
}
long long solve()
{
	long long res=0;
	int n=strlen(c);
	for(int i=0;i<n;i++)
	{
		int v=c[i]-'a';
		if(p[v].size()==0)res+=(long long)(n);
		else res+=add(v,i);
	}
	return res;
}
int main()
{
	scanf("%d%d\n",&n,&k);
	gets(s);
	for(int i=0;i<k;i++)p[s[i]-'a'].push_back(i);
	for(int i=1;i<=n;i++)
	{
		gets(c);
		printf("%I64d\n",solve());
	}
	return 0;
}


#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define qi queue<int>
#define mii map<int,int>
#define msi map<string,int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define rep3(i,a,n) for(ll i=(a);i<(n);i++)
#define rep4(i,a,n) for(ll i=(n)-1;i>=a;i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n,0,sizeof(n));
#define SZ(n) n.size()
#define ss st.st
#define sn st.nd
#define mod 1000000007
/****************************/
int n,m;
string cor;
vi v[27];
ll f(string s,string t)
{
	ll ans=0;
	if(t.size()>s.size())
		rep(i,t.size()-s.size())
		{
			s+=' ';
		}
	rep(i,t.size())
	{
		bool h=false;
		ll cnt=100000000000000;
		rep(j,v[t[i]-'a'].size())
		{
			cnt=min(cnt,abs(i-v[t[i]-'a'][j]));
		}
		if(cnt==100000000000000)
		    ans+=t.size();
		else
			ans+=cnt;

	}
	return ans;
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	cin>>cor;
	rep(i,cor.size())
	{
		v[cor[i]-'a'].pb(i);
	}
	rep(i,n)
	{
		string t;
		cin>>t;
		cout<<f(cor,t)<<endl;
	}
	return 0;
}


#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define qi queue<int>
#define mii map<int,int>
#define msi map<string,int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)
#define rep3(i,a,n) for(ll i=(a);i<(n);i++)
#define rep4(i,a,n) for(ll i=(n)-1;i>=a;i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n,0,sizeof(n));
#define SZ(n) n.size()
#define ss st.st
#define sn st.nd
#define mod 1000000007
/****************************/
int n,m;
string cor;
ll f(string s,string t)
{
	ll ans=0;
	if(t.size()>s.size())
		rep(i,t.size()-s.size())
		{
			s+=' ';
		}
	rep(i,t.size())
	{
		bool h=false;
		rep(j,max(i+1,s.size()-i))
		{
			if(i-j>=0&&s[i-j]==t[i]||i+j<s.size()&&s[i+j]==t[i])
			{
				ans+=j;
				h=true;
				break;
			}
		}
		if(!h)
		    ans+=t.size();
	}
	return ans;
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	cin>>cor;
	rep(i,n)
	{
		string t;
		cin>>t;
		cout<<f(cor,t)<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define INF (1 << 29)

string buf;
int low[30][200005], high[30][200005];
string s;

long long func()
{
	int i;
	string t=buf;
	long long ans=0;

	for (i=0; i<t.size(); i++){
		int c=t[i]-'a';
		int tmp=min (i-low[c][i], high[c][i]-i);

		if (tmp > INF / 2)
			ans += t.size();
		else
			ans += tmp;

//		cout << ans << endl;
	}

	return ans;
}

int main()
{
	int n, k, i, j;
	cin >> n >> k;
	cin >> buf;

	s = buf;
	for (i=0; i<26; i++){
		char ch='a'+i;
		for (j=0; j<200000; j++){
			if (!j)
				low[i][j] = -INF;
			else
				low[i][j] = low[i][j-1];

			if (j < s.size() && s[j] == ch)
				low[i][j] = j;
		}

		for (j=199999; j>=0; j--){
			if (j == 199999)
				high[i][j] = INF;
			else
				high[i][j] = high[i][j+1];

			if (j < s.size() && s[j] == ch)
				high[i][j] = j;
		}
	}

	for (i=1; i<=n; i++){
		cin >> buf;
		cout << func() << endl;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100005

typedef long long ll;

const ll mxgap=10000000;

vector<int> occ[130];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int i=0;i<130;i++)
        occ[i].push_back(-mxgap);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;i++)
    {
        char c;
        cin>>c;
        occ[c].push_back(i);
    }
    for(int i=0;i<130;i++)
        occ[i].push_back(mxgap);
    for(int i=0;i<n;i++)
    {
        string s;
        cin>>s;
        ll ans=0;
        for(int j=0;j<s.size();j++)
        {
            char c=s[j];
            int idx=upper_bound(occ[c].begin(),occ[c].end(),j)-occ[c].begin();
            ll mn=min(abs(j-occ[c][idx]),abs(j-occ[c][idx-1]));
            mn=min(mn,(ll)k);
            ans+=mn;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100005

typedef long long ll;

const ll mxgap=10000000;

vector<int> occ[130];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int i=0;i<130;i++)
        occ[i].push_back(-mxgap);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;i++)
    {
        char c;
        cin>>c;
        occ[c].push_back(i);
    }
    for(int i=0;i<130;i++)
        occ[i].push_back(mxgap);
    for(int i=0;i<n;i++)
    {
        string s;
        cin>>s;
        ll ans=0;
        for(int j=0;j<s.size();j++)
        {
            char c=s[j];
            int idx=upper_bound(occ[c].begin(),occ[c].end(),j)-occ[c].begin();
            ll mn=min(abs(j-occ[c][idx]),abs(j-occ[c][idx-1]));
            mn=min(mn,(ll)s.size());
            ans+=mn;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 100005

typedef long long ll;

const ll mxgap=10000000;

vector<int> occ[130];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int i=0;i<130;i++)
        occ[i].push_back(-mxgap);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;i++)
    {
        char c;
        cin>>c;
        occ[c].push_back(i);
    }
    for(int i=0;i<130;i++)
        occ[i].push_back(mxgap);
    for(int i=0;i<n;i++)
    {
        string s;
        cin>>s;
        ll ans=0;
        for(int j=0;j<s.size();j++)
        {
            char c=s[j];
            int idx=upper_bound(occ[c].begin(),occ[c].end(),j)-occ[c].begin();
            ll mn=(occ[c].size()==2?s.size():min(abs(j-occ[c][idx]),abs(j-occ[c][idx-1])));
            ans+=mn;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
set<int> se[27];
set<int>::iterator it;
int main()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    for(int i=0;i<k;i++)
    {
        se[s[i]-'a'].insert(i);
    }
    for(int i=0;i<n;i++)
    {
        string s1;
        cin>>s1;
        ll sum=0;
        for(int i=0;i<s1.size();i++)
        {
            int p=100000000,q=-10000000;
            if(se[s1[i]-'a'].empty())
            {
                sum=sum+s1.size();
                continue;
            }
            it=se[s1[i]-'a'].lower_bound(i);
            p=*it;

            if(it!=se[s1[i]-'a'].begin())
            {
                it--;
                q=*it;

            }
            //cout<<p-i<<" "<<i-q<<'\n';
            sum=sum+min(abs(i-q),abs(p-i));

        }
        cout<<sum<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
set<int> se[27];
set<int>::iterator it;
int main()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    for(int i=0;i<k;i++)
    {
        se[s[i]-'a'].insert(i);
    }
    for(int i=0;i<n;i++)
    {
        string s1;
        cin>>s1;
        ll sum=0;
        for(int i=0;i<s1.size();i++)
        {
            ll p=100000000,q=-10000000;
            if(se[s1[i]-'a'].empty())
            {
                sum=sum+s1.size();
                continue;
            }
            it=se[s1[i]-'a'].lower_bound(i);
            if(it==se[s1[i]-'a'].end()) it--;
            p=*it;

            if(it!=se[s1[i]-'a'].begin())
            {
                it--;
                q=*it;

            }

            sum=sum+min(abs(i-q),abs(p-i));

        }
        cout<<sum<<'\n';
    }
}


#include <bits/stdc++.h>
#define IOS { ios::sync_with_stdio(false); cin.tie(nullptr); }

using namespace std;

const int MAXN = 1e5+5;
const int MAXK = 1e5+5;
const int MAXL = 2e5+5;

int n, k;
string u, v;

int d[26][MAXL];

void preprocess() {
    deque<int> dq[26];
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(-1);
    for ( int i = 0; i < k; i++)
        dq[ u[i] - 'a' ].push_back(i);
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(MAXL);
    for ( int i = 0; i < MAXL; i++) {
        for ( int c = 0; c < 26; c++)
            if ( dq[c][1] < i )
                dq[c].pop_front();
        for ( int c = 0; c < 26; c++) {
            int ans = INT_MAX;
            if ( dq[c][0] != -1 )
                ans = min(ans, i - dq[c][0]);
            if ( dq[c][1] != MAXL )
                ans = min(ans, dq[c][1] - i);
            d[c][i] = ans;
        }
    }
}

int main()
{
    IOS;
    cin >> n >> k;
    cin >> u;
    preprocess();
    while ( n-- ) {
        cin >> v;
        int64_t ans = 0;
        for ( int i = 0; i < v.size(); i++) {
            if ( d[v[i]-'a'][i] == INT_MAX )
                ans += v.size();
            else
                ans += d[v[i]-'a'][i];
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS { ios::sync_with_stdio(false); cin.tie(nullptr); }

using namespace std;

const int MAXN = 1e5+5;
const int MAXK = 1e5+5;

int n, k;
string u, v;

int d[26][MAXK];

void preprocess() {
    deque<int> dq[26];
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(-1);
    for ( int i = 0; i < k; i++)
        dq[ u[i] - 'a' ].push_back(i);
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(MAXK);
    for ( int i = 0; i < MAXK; i++) {
        for ( int c = 0; c < 26; c++)
            if ( dq[c][1] < i )
                dq[c].pop_front();
        for ( int c = 0; c < 26; c++) {
            int ans = INT_MAX;
            if ( dq[c][0] != -1 )
                ans = min(ans, i - dq[c][0]);
            if ( dq[c][1] != MAXK )
                ans = min(ans, dq[c][1] - i);
            d[c][i] = ans;
        }
    }
}

int main()
{
    IOS;
    cin >> n >> k;
    cin >> u;
    preprocess();
    while ( n-- ) {
        cin >> v;
        int ans = 0;
        for ( int i = 0; i < v.size(); i++) {
            if ( d[v[i]-'a'][i] == INT_MAX )
                ans += v.size();
            else
                ans += d[v[i]-'a'][i];
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS { ios::sync_with_stdio(false); cin.tie(nullptr); }

using namespace std;

const int MAXN = 1e5+5;
const int MAXK = 1e5+5;
const int MAXL = 2e5+5;

int n, k;
string u, v;

int d[26][MAXL];

void preprocess() {
    deque<int> dq[26];
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(-1);
    for ( int i = 0; i < k; i++)
        dq[ u[i] - 'a' ].push_back(i);
    for ( int c = 0; c < 26; c++)
        dq[c].push_back(MAXL);
    for ( int i = 0; i < MAXL; i++) {
        for ( int c = 0; c < 26; c++)
            if ( dq[c][1] < i )
                dq[c].pop_front();
        for ( int c = 0; c < 26; c++) {
            int ans = INT_MAX;
            if ( dq[c][0] != -1 )
                ans = min(ans, i - dq[c][0]);
            if ( dq[c][1] != MAXL )
                ans = min(ans, dq[c][1] - i);
            d[c][i] = ans;
        }
    }
}

int main()
{
    IOS;
    cin >> n >> k;
    cin >> u;
    preprocess();
    while ( n-- ) {
        cin >> v;
        int ans = 0;
        for ( int i = 0; i < v.size(); i++) {
            if ( d[v[i]-'a'][i] == INT_MAX )
                ans += v.size();
            else
                ans += d[v[i]-'a'][i];
        }
        cout << ans << '\n';
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int A, B;
    scanf("%d %d", &A, &B);
    char G;
    string Y;
    scanf("%c", &G);
    vector<vector<int> >MAT(28, vector<int>());
    for (int I=0;I<B;I++)
    {
        scanf("%c", &G);
        MAT[G-'a'].push_back(I);
    }
    scanf("%c", &G);
    for (int I=0;I<A;I++)
    {
        getline(cin, Y);
        int VV=Y.size();
        int sum=0;
        for (int I2=0;I2<VV;I2++)
        {
            int U=Y[I2]-'a';
            int VV2=MAT[U].size();
            if (VV2>1)
            {
                int left=0, right=VV2-1;
                for (int medio=(left+right)/2;left!=right;medio=(left+right)/2)
                {
                    if (MAT[U][medio]<I2)
                    {
                        left=medio+1;
                    }
                    else
                        right=medio;
                }
                if (left>0)
                    sum+=(min(abs(MAT[U][left]-I2), abs(MAT[U][left-1]-I2)));
                else
                    sum+=(abs(MAT[U][left]-I2));
            }
            else if (VV2==1)
                sum+=(abs(MAT[U][0]-I2));
            else
                sum+=VV;
        }
        printf("%d\n", sum);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int A, B;
    cin >> A >> B;
    char G;
    string Y;
    scanf("%c", &G);
    vector<vector<long long int> >MAT(28, vector<long long int>());
    for (long long int I=0;I<B;I++)
    {
        scanf("%c", &G);
        MAT[G-'a'].push_back(I);
    }
    scanf("%c", &G);
    for (long long int I=0;I<A;I++)
    {
        getline(cin, Y);
        long long int VV=Y.size();
        long long int sum=0;
        for (long long int I2=0;I2<VV;I2++)
        {
            long long int U=Y[I2]-'a';
            long long int VV2=MAT[U].size();
            if (VV2>1)
            {
                int left=0, right=VV2-1;
                for (long long int medio=(left+right)/2;left!=right;medio=(left+right)/2)
                {
                    if (MAT[U][medio]<I2)
                    {
                        left=medio+1;
                    }
                    else
                        right=medio;
                }
                if (left>0)
                    sum+=(min(abs(MAT[U][left]-I2), abs(MAT[U][left-1]-I2)));
                else
                    sum+=(abs(MAT[U][left]-I2));
            }
            else if (VV2==1)
                sum+=(abs(MAT[U][0]-I2));
            else
                sum+=VV;
        }
        printf("%I64d\n", sum);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
using namespace std;

int main()
{
    int A, B;
    scanf("%d %d", &A, &B);
    char G;
    string Y;
    scanf("%c", &G);
    vector<vector<int> >MAT(28, vector<int>());
    for (int I=0;I<B;I++)
    {
        scanf("%c", &G);
        MAT[G-'a'].push_back(I);
    }
    scanf("%c", &G);
    for (int I=0;I<A;I++)
    {
        getline(cin, Y);
        int VV=Y.size();
        int sum=0;
        for (int I2=0;I2<VV;I2++)
        {
            int U=Y[I2]-'a';
            int VV2=MAT[U].size();
            if (VV2>1)
            {
                int left=0, right=VV2-1;
                for (int medio=(left+right)/2;left!=right;medio=(left+right)/2)
                {
                    if (MAT[U][medio]<I2)
                    {
                        left=medio+1;
                    }
                    else
                        right=medio;
                }
                if (left>0)
                    sum+=(max(MAT[U][left]-I2, MAT[U][left-1]-I2));
                else
                    sum+=(MAT[U][left]-I2);
            }
            else if (VV2==1)
                sum+=(MAT[U][0]-I2);
            else
                sum+=VV;
        }
        printf("%d\n", sum);
    }
    return 0;
}