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

int main(int argc,char* argv[]){
	registerValidation(argc,argv);

    int N = 2e5;
    int M = 2e5;
    int PRODUCT = 2e5;
    int K = 100;
 
 	switch(atoi(validator.group().c_str())){
        case 1: N = 1; M = 2; break;
        case 2: N = 1; break;
        case 3: K = 1; PRODUCT = 5e3; break;
        case 4: PRODUCT = 5e3; break;
        case 5: K = 1; break;
        default: break;
    }
    
    int n = inf.readInt(1, N, "n");
    inf.readSpace();
    int m = inf.readInt(1, M, "m");
    inf.readEoln();
    ensure(n * 1LL * m <= 1LL * PRODUCT);
    
    vector<string> a;
    
    int count_k = 0;
    
    for (int i = 0; i < n; i++) {
        string s = inf.readToken("[HF.]{1,200000}", "s");
        inf.readEoln();
        a.push_back(s);
        ensure((int) s.size() == m);
        for (char c : s) {
            if (c == 'H') {
                count_k++;
            }
        }
    }
    inf.readEof();
    ensuref(count_k <= K, "too much holes");
    ensuref(1 <= count_k, "zero holes");
    
    int x = 0;
    int y = 0;
    while(a[x][y] == 'F' && x < n) {
        y++;
        if (y == m) {
            x++;
            y = 0;
        }
    }
    
    if (x == n) return 0;
    
    int X[4] = {1, 0, -1, 0};
    int Y[4] = {0, 1, 0, -1};
    
    queue<pair<int, int> > q;
    q.push(make_pair(x, y));
    a[x][y] = 'F';
    while (!q.empty()) {
        x = q.front().first;
        y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int x1 = x + X[i];
            int y1 = y + Y[i];
            if (0 <= x1 && x1 < n && 0 <= y1 && y1 < m && a[x1][y1] != 'F') {
                a[x1][y1] = 'F';
                q.push(make_pair(x1, y1));
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ensuref(a[i][j] == 'F', "not connected");
        }
    }
 
	return 0;
}
