/******************************
Author: jhnah917(Justice_Hui)
g++ -std=c++14 -DLOCAL
******************************/

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;

int N, K, A[9][9], R;

int main(){
    cin >> N >> K;
    for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) cin >> A[i][j];
    vector<int> ord(N);
    iota(all(ord), 1);
    do{
        int now = 0, flag = 1;
        for(int i=1; i<N; i++){
            if(!A[ord[i-1]][ord[i]]) flag = 0;
            now += A[ord[i-1]][ord[i]];
        }
        if(!A[ord.back()][ord[0]]) flag = 0;
        now += A[ord.back()][ord[0]];
        if(flag && now == K) R++;
    }while(next_permutation(ord.begin()+1, ord.end()));
    cout << R;
}
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };

char mp[1000][1000];
vector<int> gph[2000];
bool chk[2000];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int r, c;
	cin >> r >> c;
	gph[0].push_back(r);
	gph[r].push_back(0);
	gph[0].push_back(r + c - 1);
	gph[r + c - 1].push_back(0);
	gph[r - 1].push_back(r);
	gph[r].push_back(r - 1);
	gph[r - 1].push_back(r + c - 1);
	gph[r + c - 1].push_back(r - 1);
	for (int i = 0; i < r; ++i)
		for (int j = 0; j < c; ++j) {
			cin >> mp[i][j];
			if (mp[i][j] == '#') {
				gph[i].push_back(r + j);
				gph[r + j].push_back(i);
			}
		}

	int cnt0 = 0, cnt1 = 0;
	for (int i = 0; i < r + c; ++i)
		if (!chk[i]) {
			queue<int> q;
			chk[i] = true;
			q.push(i);
			
			bool chk0 = false, chk1 = false;
			while (!q.empty()) {
				int now = q.front();
				q.pop();

				if (now < r)
					chk0 = true;
				else
					chk1 = true;

				for (int nxt : gph[now])
					if (!chk[nxt]) {
						chk[nxt] = true;
						q.push(nxt);
					}
			}

			if (chk0)
				cnt0++;
			if (chk1)
				cnt1++;
		}

	cout << min(cnt0, cnt1) - 1;

	return 0;
}