# include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 7;
int a[MAXN][MAXN];
int n;
int dfs_1(int x1, int y1, int x2, int y2, int ans)
{
if (a[x1][y1] == a[x2][y2])
{
ans += 2;
if (x1-1 >= 1 && x1-1 <= n && x2+1 >= 1 && x2+1 <= n && y1 >= 1 && y1 <= n && y2 >= 1 && y2 <= n)
return dfs_1(x1-1, y1, x2+1, y2, ans);
}
return ans;
}
int dfs_2(int x1, int y1, int x2, int y2, int ans)
{
if (a[x1][y1] == a[x2][y2])
{
ans += 2;
if (x1 >= 1 && x1 <= n && x2 >= 1 && x2 <= n && y1-1 >= 1 && y1-1 <= n && y2-1 >= 1 && y2-1 <= n)
return dfs_2(x1, y1-1, x2, y2+1, ans);
}
return ans;
}
int dfs_3(int x1, int y1, int x2, int y2, int ans)
{
if (a[x1][y1] == a[x2][y2])
{
ans += 2;
if (x1+1 >= 1 && x1+1 <= n && x2-1 >= 1 && x2-1 <= n && y1+1 >= 1 && y1+1 <= n && y2-1 >= 1 && y2-1 <= n)
return dfs_3(x1+1, y1+1, x2-1, y2-1, ans);
}
return ans;
}
int dfs_4(int x1, int y1, int x2, int y2, int ans)
{
if (a[x1][y1] == a[x2][y2])
{
ans += 2;
if (x1-1 >= 1 && x1-1 <= n && x2+1 >= 1 && x2+1 <= n && y1+1 >= 1 && y1+1 <= n && y2-1 >= 1 && y2-1 <= n)
return dfs_4(x1-1, y1+1, x2+1, y2-1, ans);
}
return ans;
}
int main()
{
int t;
cin >> n >> t;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
for (int j = 0; j < n; j++)
a[i][j+1] = s[j]-'a'+1;
}
while (t--)
{
int x, y;
cin >> x >> y;
int ans = dfs_1(x, y, x, y, -1);
ans = max(ans, dfs_2(x, y, x, y, -1));
ans = max(ans, dfs_3(x, y, x, y, -1));
ans = max(ans, dfs_4(x, y, x, y, -1));
cout << ans << endl;
}
return 0;
}