#include<iostream>
#include<vector>
using namespace std;
using ll=long long;

int main(){
  int n,k;cin>>n>>k;
  vector<vector<ll>>f(n+1,vector<ll>(n+1,-1));

  ll right=0,left=-1;
  for(int y=0;y<n;y++){
    for(int x=0;x<n;x++){
      cin>>f.at(x+1).at(y+1);
      right=max(right,f.at(x+1).at(y+1));
    }
  }

  while(right-left>1){
    ll mid=(right+left)/2;
    vector<vector<ll>>s(n+1,vector<ll>(n+1,0));
    bool judge=false;

    for(int y=1;y<=n;y++){
      for(int x=1;x<=n;x++){
	s.at(x).at(y)=s.at(x-1).at(y)+s.at(x).at(y-1)-s.at(x-1).at(y-1);
	if(f.at(x).at(y)>mid)s.at(x).at(y)++;
      }
    }

    for(int y=1;y+k-1<=n;y++){
      for(int x=1;x+k-1<=n;x++){
	ll tmp=s.at(x+k-1).at(y+k-1)-s.at(x-1).at(y+k-1)-s.at(x+k-1).at(y-1)+s.at(x-1).at(y-1);
	if(tmp<(k*k)/2+1)judge=true;
      }
    }
    
    if(judge)right=mid;
    else left=mid;
  }

  cout<<right<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb pushback
#define fr(i,n) for(int i=0;i<n;i++)
#define fro(i,j,n) for(int i=j;i<n;i++)
#define ifr(i,n) for(int i=n-1;i>=0;i--)

int main() {
    int h,w;
	cin >> h>>w;
    string s[h];
  int ans=0;
    fr(i,h){
      cin >> s[i];
    }
    fr(i,h-1){
      fr(j,w-1){
        int count=0;
        if(s[i][j]=='#')count++;
        if(s[i+1][j]=='#')count++;
        if(s[i][j+1]=='#')count++;
        if(s[i+1][j+1]=='#')count++;
        if(count==1||count==3)ans++;
      }
    }
      cout << ans << endl;
}
