#include #include #include using namespace std; int R, S; char mapa[1111][1111]; int oznaceno[1111][1111]; int ans = 0; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int jidlo(int y0, int x0) { int res = 0; stack > st; st.push(make_pair(y0,x0)); while (!st.empty()) { int y = st.top().first; int x = st.top().second; st.pop(); if (y>=R || y<0 || x>=S || x<0 || oznaceno[y][x]) continue; if (mapa[y][x]=='#' || mapa[y][x]=='J') { oznaceno[y][x] = 1; if (mapa[y][x]=='J') res++; for (int i=0; i<4; i++) st.push(make_pair(y+dy[i], x+dx[i])); } } return res; } // DFS = depth-first search = prohledávání do hloubky void dfs(int y0, int x0) { stack > st; st.push(make_pair(y0,x0)); while (!st.empty()) { int y = st.top().first; int x = st.top().second; st.pop(); if (y>=R || y<0 || x>=S || x<0) continue; if (!oznaceno[y][x]) { if (mapa[y][x]=='#' || mapa[y][x]=='J') ans = max(ans, jidlo(y,x)); else { oznaceno[y][x] = 1; for (int i=0; i<4; i++) st.push(make_pair(y+dy[i], x+dx[i])); } } } } int main() { int sx, sy; scanf("%d%d", &S, &R); for (int i=0; i